我的游戏中有一个动态的身体(这是 2D 横向滚动平台游戏),我希望它沿着平台来回移动,但不受任何垂直力的影响。
我不能让它成为运动体,因为我需要使用它进行碰撞检测,而 box2d 不支持运动体的碰撞检测。
我也无法制作棱柱关节,因为没有第二个身体可以连接关节。
那么在这种情况下,如何让动态体严格水平移动而不使其成为运动体并且不使用关节?
我的游戏中有一个动态的身体(这是 2D 横向滚动平台游戏),我希望它沿着平台来回移动,但不受任何垂直力的影响。
我不能让它成为运动体,因为我需要使用它进行碰撞检测,而 box2d 不支持运动体的碰撞检测。
我也无法制作棱柱关节,因为没有第二个身体可以连接关节。
那么在这种情况下,如何让动态体严格水平移动而不使其成为运动体并且不使用关节?
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.