简体   繁体   中英

box2d , libgdx - Player Body have relative velocity of spaceship but still be able to move around

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!

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*);
       }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM