Creating a magnet effect in Farseer

Topics: Developer Forum, User Forum
Apr 11, 2009 at 2:44 PM

I'd like to create a magnet effect that pulls physics objects toward the player - seems pretty simple but since I'm new to using Farseer, I wanted to run this approach by the community to make sure I wasn't setting myself up for a problem down the line.

Here's what I'm doing at the moment (This seems to work but I want to make sure there isn't a better way to handle this):

foreach(PhysicsObject o in physicsObjects)
{

 

 

    o.PhysicsBody.ClearImpulse();

 

 

    // Generate a vector to the player
    Vector2 vecObjectToPlayer = player.PhysicsBody.Position - o.PhysicsBody.Position;

 

 

    vecObjectToPlayer.Normalize();
    
    // Apply a force along that vector
    c.PhysicsBody.ApplyImpulse(vecObjectToPlayer * 50);

 

 

}

Apr 11, 2009 at 6:15 PM
You could apply a gravity controller to the player,
I know they have a gravity thing like that for circular gravity(like planets)
Or perhaps you could do the inverse of the grenade sample(It might be similar to what your already doing)
I dont have much experience with the seperate gravity stuff but it seems like that would do what you want and cut out referencing all other objects in a foreach, im not sure haha : P
Apr 12, 2009 at 6:11 AM
This is something I am trying to do as well.

The gravity controller is only in svn, and isn't yet released.  RogueComanderIX might be able to tell us more.  I haven't yet used it and can't say whether or not it works.

Inversing the grenade doesn't work.  It crashes the entire system :P.  I've tried it already.

As far as doing the foreach, that is a common way, because you need it to check all the blocks as they move closer to the area in which the gravity should be applied.
Apr 12, 2009 at 8:00 AM
Edited Apr 12, 2009 at 8:01 AM
If you use the patch provided in the patch section, it will work for what you're going for. Just install the patch (copy the files included in it to the directories where the old files are) and create a gravity controller/joint/whatever you want. The gravity contoller will suck all non static objects towards it (and it gets slow with large groups of objects) so make sure to anchor your platforms.
Apr 12, 2009 at 9:57 PM
RogueCommanderIX, does it allow you to set a radius (distance) wherby any non-static body within the radius gets sucked towards it, and those too far would not?  That is similar to a fall-off of gravity, I suppose.  If not, it doesn't really do what a magnet would, and in my case, doesn't meet my needs.  Thanks for the heads up though.  I was looking through Physics2D.net's implementation, and they have a gravityfield, a gravitypointfield and a gravitypointmass.  The latter being the effect I want to use.  I may port it over if you haven't already covered it.

BTW, I did what you did vsouders, but I had to add a couple lines to avoid a NaN error. 

With your code above, the new lines would be entered at the top of the foreach loop:

    if (o.PhysicsBody == player.PhysicsBody)
        continue;

There is no need to apply an impulse to the body where the gravity is originating from.
Apr 12, 2009 at 10:52 PM
Edited Apr 12, 2009 at 10:55 PM
That is correct (you can set a max radius). Hang on lemme check what the property is.... Thanks for spotting the bug BTW.
Edit: use the maxDistance property.