Proper way of moving physics object in path Box2d?

I am creating a game where there are needs for obstacles but these obstacles have to move in a path which I don’t know the proper way to make.
The obstacle has the be a box2d physics object and should move up to a specific position and then down to the starting position and then just keep doing that.
Here’s an illustration that should clarify my thoughts better.


But what is the most effective way of doing this? Joints, impulses, what?

Thank you!

Hi.
the best way for moving box2d objects to the specified position (or path) is create a mouse joint.
my solution for moving mouse joint in a path is create a node and run action (in your picture 2 sequences of MoveTo action) by it. then in the update method of this node you can get position from it and set mouse joint position from this.

But don’t you have to move the physics object using the physics engine and not MoveTo if you what them to be able to collide?

And yea, thank you!

As amin13a said, you must update your b2Body’s position according to your sprite’s position in a update method.

If you don’t want to use a mouse joint you can also use b2Body->SetTransform in a update method, where you set your physics’s body position according to you sprite’s, so you can set the sprite’s position in a classic cocos way(by using Actions such as MoveTo and MoveBy etc).

An inverse way would be to make a cocos sequence where you do a body->ApplyLinearImpulse(upImpulse), than a Delay, than a body->ApplyLinearImpulse(downImpulse) and another Delay. In this case in an update method you must set your sprite’s position according to your body’s position. Also, you should probably set the linear velocity of the body to (0, 0) right before you apply the impulse.

Note that these methods will all provide with different behavior, so you must choose the one that you think it suits best to your needs.

1 Like

Okay thanks! i will try that in a moment

But what of these methods do you think is the best one as the solution for a physics object that is moving up and Down in a sequence and still check if another physics body has touched it.

Thank you so much

Well as I said, it depends on the feel of the game you want to achieve.

The SetTransform method will force your object at that location, so it may overlap other static bodies and probably dynamic bodies as well(or just move them).

Using LinearImpulse, your object won’t overlap other bodies, it will stop when it hits static bodies such as walls, but you’ll also have to implement a mechanism to not allow the your object to be moved on the x-axis, something like setting it’s linear velocity to Vec2(0, body->getLinearVelocity().y) on each frame.

Speaking of linear velocity there might also be another method, almost the same as the Impulse method but with setting linear velocity instead.
In an update method you set your body’s linear velocity to Vec2(0, yourLinearVelocity), where yourLinearVelocity is a positive number if you want it to go up and a negative one if you want it to go down. If you will always have walls at the top and bottom and you want your object to move up or down until it hits the walls, than this method might be the best, since it won’t overlap the walls. If you don’t have walls at the ends of your path, you will also need to manually check if your object went further than you wanted and bring it back where you wanted it to be using SetTransform.

Also, it may be better to set your body as a BodyType.KinematicBody, so it won’t be affected by other world forces, like gravity.

And you can take a look here http://www.emanueleferonato.com/2012/05/11/understanding-box2d-kinematic-bodies/

I think this does exactly what you want(just that it moves horizontally and not vertically) and it uses LinearVelocity on kinematic bodies.

Wow! Thank you so much! I really appreciate your time

No problem, by the way, how are showing the debug draw? I can’t manage to display the debug data over my sprites, it always goes behind my sprites and I have to change their opacity in order to see something.

Oh I am SO sorry that I didn’t see your message :frowning: Have you fixed it yet?

I haven’t, bu no problem, I’ve finished with the box2d part for the project I’m working on, though I was curious for future games.