Home > Blockchain >  How make a vector2 relative to other vector2 properly?
How make a vector2 relative to other vector2 properly?

Time:09-09

Im making a 2D game with box2D physics, and i want to implement a parent-child system between the objects. The child's position will be relative to it parent. For example, father object position is (10, 0), the relative child's position is (0, 1) and the result child's position is (10, 1).

I was thinking about how to implement something like that, and I came to the conclusion that adding to each component of the child's position vector the parent's current position minus parent's previous frame position, this effect was achieved.

That worked fine in a normal game, but with box2d physics, there is some "delay" and children position seems to update slower than the father.

There is a proper way to achieve this effect more accurately?

CodePudding user response:

What you're describing is known in the physics simulation world as a constraint. It simply means that the constrained object isn't free to move like it wants to, but instead there are some restrictions. A special case of a constraint is a joint that links two bodies together - and Box2D supports them. In your specific case, if you want the child to always be exactly in the same spot as the parent, you probably want a "weld" joint.

That being said, if you want a 100% interlock between parent and child, you might want to create one compound shape by either composing Fixtures or using ChainShape; that will guarantee that the system will never consider a possibility of them moving relative to one another, at least not with regards to dynamic forces.

  • Related