Making a Box2d object follow a predetermined path

吃可爱长大的小学妹 提交于 2019-12-05 17:04:29

Another option:

  • Attach a mouse joint to your body
  • Use setTarget method of the mouse joint to move the body

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 :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!