Create realistic magnet

Topics: Developer Forum, User Forum
Sep 21, 2012 at 4:02 PM

Hi there, thanks to the Farseer folks, I'm loving your engine!

I'm currently working on my senior project in my computer science major. We are using Farseer to make an XNA game for the XBOX 360.

Our game is all about magnets and I'm trying to get some realistic magnet effects going. 

If I have the player body "pushing" (we're currently faking it with impulses applied directly to a target body) on another body, that body moves until it hits a surface at which time it stops. Great! Now, when it stops we would then like that force to push the player away. I'll try to illustrate:

___PLAYER ---> Object__________|

___PLAYER ------------> Object |

Now object has nowhere to go so...

PLAYER----------------->Object |

 

I am having trouble determining when the target object is stuck against a wall. I was thinking of trying to capture the normal force being applied to the object due to the repeated impulses we are applying and then transferring that force to the player. However, I don't know if that is possible and if so how to do it. 

Does anyone have any ideas on how to create this effect?

Thanks!

Sep 21, 2012 at 7:54 PM
Edited Sep 21, 2012 at 7:54 PM

I figured it out! For those interested, here are some snippets that I used to get normal forces and figure out which body should be moving:

 

 

 protected bool ApplyNormalForce(Fixture fixtureA, Fixture fixtureB, FarseerPhysics.Dynamics.Contacts.Contact contact)
        {
            if (contact.Manifold.LocalNormal.X != 0)
            {
                _normalX._normal = contact.Manifold.LocalNormal.X;
                _normalX._fix = fixtureB;
            }
            if (contact.Manifold.LocalNormal.Y != 0)
            {
                _normalY._normal = contact.Manifold.LocalNormal.Y;
                _normalY._fix = fixtureB;
            }

            return true;
        }

This breaks down the normal force into X and Y components, which I then test to see if they are going the opposite direction of my "magnet pull." If so, I move the player instead of the object.