1
votes

I'm creating a Top-down 2D game and I'm using Box2D to simulate physics, my question is this:

How do I keep the player at a relative velocity to my spaceship, and still be able to move around my player with the ship also moving?

I've put an illustration below.
illustration

What I have tried so far:

Setting the linear velocity of the player body to be the same as the ship, this makes the player pretty much attached to the ship, without me able to move the player, since I was setting the linear velocity after every update tick.

Trying out joints does not seem to be what I am looking for, I might be wrong, I've tried the WeldJoint and the FrictionJoint adding them when I enter the ship. , however, with the weld joint, I couldn't move the player since I'm welded to the ship.

Thank you in advance for your help!

1

1 Answers

0
votes

What about "Setting the linear velocity of player body to be the same as the ship" only when your player controller isn't pressed.

If you use scene2d for your UI, these Actors have isTouched method. Or if you just use buttons, you can create boolean field and set, it true if any of your control buttons touched and false if not.

So, your player controller method may looks like this.

void playerController() {
     if (!playerControllerBottonTouched) { // or actor.isTouched()
        playerBody.setLinearVelocity(shipBody.getLinearVelocity().x, 
                                     shipBody.getLinearVelocity().y);
     } else {
           playerBody.applyLinearImpulse(*impulse that you want*);
       }
}