Box2d object creation is performed in the specified path

I am making a game in which a specific object (modeled as a box2d body) must follow a fixed path. Is there a way by which I can specify the coordinates of the path and make an object above it for each dt?

thanks

+4
source share
2 answers

Another option:

  • Attach the mouse to your body.
  • Use the setTarget method of the mouse joint to move the body.
+6
source

You must use the kinematic body, but you cannot change its position manually, you need to change its speed for the dynamics and collisions to work correctly.

I propose the following algorithm:

1st - Calculate the position on the track where the body should be on the next dt.

2nd - Make a vector going from the position where the body is in the next position.

3rd - normalize it.

4th - calculate what speed you need so that the body is in this position in the next cycle and multiplies this speed by a vector.

5th - apply this vector to the linear velocity of the body.

Note: make sure the kinematic body has zero resistance, so it’s easier to calculate the 4th step.

I have never done anything like this, I think it can be done like this. Hope this helps :)

+1
source

Source: https://habr.com/ru/post/1387935/


All Articles