0
votes

I have a dynamic body in my game (which is 2D sidescrolling platformer) and I want it to move back and forth along a platform, but not be affected by any vertical forces.

I can't make it a kinematic body because I need to do collision detection using it, and box2d doesn't support collision detection for kinematic bodies.

I also can't make a prismatic joint because there's there is no 2nd body to attach the joint to.

So in this case, how do I make a dynamic body move strictly horizontally without making it a kinematic body and without using joints?

1

1 Answers

2
votes

It would be better, if you give some more information, about what you exactly need. I can suggest next solutions, according to your description:

1) >> I also can't make a prismatic joint because there's there is no 2nd body to attach the joint to.

You can create somewhere static body without fixtures, and attach to it. Pay attention, this body can be placed anywhere on the scene (for example, at position (0,0)).

2) Set b2BodyDef::gravityScale to zero and move as you like (forces, impulses, b2MouseJoint, SetTrasform).

3) Use b2MouseJoint to move body in any way. Joint also can be attached to any static body on scene. There it may be useful to set fixedRotation of the body, to prevent rotating.

4) >> I can't make it a kinematic body because I need to do collision detection using it, and box2d doesn't support collision detection for kinematic bodies.

To be certain, Box2d support collision detection for kinematic bodies, but it collide kinematic bodies only with dynamic bodies. If you need to catch collision between kinematic body and static or another kinematic body, you can attach to this kinematic body some dynamic body via b2WeldJoint, and listen contacts of this dynamic body.