使 Box2d 对象遵循预定路径
我正在制作一个游戏,其中某个对象(建模为 box2d 主体)必须遵循固定路径。有没有一种方法可以指定路径坐标并使对象在每个 dt 上前进?
谢谢
I'm making a game in which a certain object (modelled as a box2d body) has to follow a fixed path. Is there a way by which I can specify the path coordinates and make the object advance over it for each dt?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
另一种选择:
Another option:
您应该使用运动体,但不能手动更改其位置,必须更改其速度才能正确应用动力学和碰撞。
我建议采用以下算法:
第一 - 计算下一个 dt 时身体应位于的轨道上的位置。
第二步 - 创建一个从身体所在位置到下一个位置的向量。
第三 - 标准化。
第四步 - 计算需要多少速度才能使身体在下一个循环中处于该位置,并将该速度乘以矢量。
5th - 将此矢量应用于物体的线速度。
注意:确保运动体的阻力为零,以便计算第 4 步更容易。
我从来没有做过这样的事情,我认为可以这样做。
希望它有帮助:)
You should use a Kinematic body, but you can't change its position manually, you have to change its speed for the dynamics and collisions to be applied correctly.
I suggest the following algorithm:
1st - Calculate the position on the track that the body should be in on the next dt.
2nd - Make a vector going from the position where the body is to the next position.
3rd - Normalize it.
4rd - Calculate how much speed you need so that the body will be in that position on the next loop, and multiply that speed on the vector.
5th - Apply this vector to the Linear Velocity of the body.
Note: make sure the kinematic body has zero drag so that calculating the 4th step is easier.
I never did something like this, I think it can be done this way.
Hope it helps :)