3
votes

So i have some bodies(which i can create more and more at runtime), and i need them to be magnetized to the screen center. Let it be a (480/2 / WORLD_SCALE, 320/2 / WORLD_SCALE) in the world space. I am new to box2d so maybe there is an easy way to achieve this, but i am trying to do it as following:

  1. I subtract each bodie's position and screen center position - to get a direction to where i need to move my body to.
  2. Normalize the direction vector(which i've found in p.1)
  3. Multiply this normalized vector to my gravity power value
  4. Add the vector(that i've got in the p. 3) to the current bodie's speed

So after that i get what i want - bodies do magnetize to the screen center, i also add some frictions, and dampings so that bodies could ever stop. But, as i assume, because of my gravity vector to be constant length - i cannot force the bodies to stop moving completely. It always tries to move itself to the screen center and the smallest possible speed for them - is the length of this gravity vector.

If i only had one body, i would not perform steps 2,3 when the body is closer to the screen center then the gravity size is. But i can not bind bodie's position to the magnetic field center because the two bodies will never take the exact middle screen place since they will collide with each other. So i cannot make my gravity vector variable, and playing around with any frictions do not do the trick, maybe some step hack could help, but i think what i need is to implement some sort of bouncing counter force or something of that kind.

I am new to box2d, and not very good with physics.

What may also be important is that i set the bodies speed with the SetLinearVelocity method.

Maybe i am just missing something, maybe there is some native for box2d way to achieve this. Any help appreciated.

1

1 Answers

2
votes

Did solve this issue by changing world scale. "Box2D also uses inelastic collisions when the collision velocity is small. This is done to prevent jitter." http://www.box2d.org/manual.html After reading that i figured out that i should make my acceleration vector small enough so that box2d used this inelastic collisions and prevent its jittering in "native" way. So increasing WORLD_SCALE by 4 times made the distances between bodies and the screen center big enough. Playing around with new numbers for gravitation and friction powers made the trick.

Regards, Igor