Simulating a ( sort of ) magnetic attractor

Jul 18, 2010 at 5:45 PM

Hi everibody.

First of all, I think OOPS is areally well done library, in comparison to other engines in term of object roeined design.

Then the question: I would like to simulate an object to be attracted by some other with some kind of field: What is the best way to achiebve this ?

I'm think on just adding to the rigid body my "field" component in the update function, but no idea if there is some better way.

Thanks,

Felix

Jul 24, 2010 at 8:57 PM

Hi felixpollan,  Here's one way you could do it:

Subtract the positions of the rigidbodoes:

Vector3 normal = rigidBody1.Position - rigidBody2.Position;

Normalize the vector:

normal.Normalize();

 

Then, use this normal to apply a force to both rigid bodies:

normal *= <some factor>;

rigidBody1.ApplyForce(-normal);

rigidBody2.ApplyForce(normal);

Jul 24, 2010 at 10:41 PM

hanks you very much,

I will try