Grappling Hook/Rope Swing Farseer 3.0

Topics: User Forum
Feb 11, 2011 at 9:45 PM

Hello again community,

It's been so long since I've been back here and man did things change!  The new engine is looking sweet as can be, awesome work guys.

I'm trying to do something that could possibly be extremely basic but I just can't wake the gerbil in my head up.

I'd like to simulate a grappling hook by:

1.  Casting a ray in the thumbstick direction and taking the first body it comes in contact with.
2.  Then creating a joint connecting my player to the ray casted body so I can swing across any impending pit-o-doom etc etc.

I'm using Farseer 3 and using the Camera2D functionality (way awesome by the way), however my attempts to do the above are pathetic at best so bad that I'm too embarrassed to post code since I'm grabbing at straws here.  I think I'm thrown by anchor points and pixel to meter conversions blah blah blah, I really have no idea.

Once I can do the basic 1 and 2 I'll have a better understanding and move onto turning it into a slider joint or rope path or whatever.
I looked at some postings in the past and I'm not certain I took enough away from them, the people involved seemed to be way ahead of me in general physics (not hard to be).

Can anyone help me please?  How can a ninja be a ninja without a grappling hook :)

Thanks in advance!


Feb 12, 2011 at 12:45 AM


You cast a ray from p1 to p2 like this:

World.RayCast((fixture, point, normal, fraction) =>
                        _fixture = fixture;
                        _point = point;
                        _normal = normal;

                        return fraction;
                    }, p1, p2);

the value you return determines the behavior of the ray:

return -1: ignore this fixture and continue
return 0: terminate the ray cast
return fraction: clip the ray to this point
return 1: don't clip the ray and continue


You could create something like a distance joint:

JointFactory.CreateDistanceJoint(world, bodyA, bodyB, Vector2.Zero, Vector2.Zero);

The two anchors are in local coordinates; that means relative to the position of the body.

Feb 12, 2011 at 12:49 AM
Edited Feb 12, 2011 at 12:54 AM

You havent really told us where you're sticking. Have you got ray-casting working?

First make sure ray-casting is working by drawing a big circle wherever the ray hits. Do this first without worrying about the joints. Look at RaycastTest.cs in the tesbed for an example.

Drawing the circle will be the only time you have to worry about pixel/meter conversions, so this will also let you know if that is working.

The next step is to get the anchor point by calling Body.GetLocalPoint with the point of the ray, and then you can create your joint. But make sure that all the previous steps are working first.

EDIT: Genbox posted while I was typing! :) And luckily he mentioned the return values, theyre really important. In fact, Genbox, I think they should be put in a comment in RaycastTest.cs, just in case someone uses that as their model without reading up.

Feb 12, 2011 at 1:01 AM

Thank you both for the quick reply!

For the record I had both wrong, yay me!  I was close on the raycast but didn't understand the fraction, and the joint I didn't treat the anchors as local coordinates.

Genbox your code example was perfect, thank you so much for that!!!
Now my ninja can go save the princess or whatever ninjas do after grappling!

Can't wait to make a generous donation, keep up the awesomeness guys!