This project has moved and is read-only. For the latest updates, please go here.

Swinging movement - rope of springs

Mar 7, 2009 at 1:06 PM
Hey there. I was wondering if people could help me on implementing an idea I have. I want my character to swing around, from rock to rock, by firing and releasing a sort of grabling hook with a rope or elastic attached. A bit like spiderman if you want.

The main point is how the create the rope. I start off using a single spring, attached to the rock and the character, and that's working allright. But then the rope would of course not collide with the rocks, which it must. Creating a number of smaller springs with geoms in between them should deal with this, but it ruins the effect of the spring. The rope gets way too weak, no matter how high the springconstant is or how much I lower the restlength of the spring. That brings me to a question btw:
Will a spring pull with greater force, as the difference between it's current length and the restlength increase?

I would be greatly helped if people would spit out ideas they might have on how to do this. What I need is a rope which is a little elastic (so it speeds up the character), but stiff enough to 'comfortably' swing through the level, plus it cannot penetrate objects.
Mar 8, 2009 at 12:47 AM
@jordos: Springs do pull harder the farther they are stretched pass their rest length. Is your rope supposed to be springy? You could use the Path generator to create a linked set of bodies and geoms connected with any joint or spring you choose. You could also render that set of bodies as a continuous curve. Examples of how to use the Path class are in the advanced examples. Please give this method a try and let me know if you have any problems as I'm the author of the Path class.

Also to touch on your last statements. If you create a path with say 30 bodies and connect them with Revolute joints there will still be a good amount of springiness in your rope. In fact the shorter it is the less spring it would have just like a real rope.

May I ask how your planning on finding where the rope will attach to the rock?
Mar 8, 2009 at 6:26 AM
Have you tried this? create a chain, but make each of the links be connected to the next/previous 2 geoms (so that the chain is stiffer). You would have to modify the chain factory to apply the additional springs but it should make the rope much stiffer. The only downside is that it also makes the rope resistant to bending (because when the objects bend, the springs that connect bodies that are 2 links apart are compressed and then they try to extend to compensate).
Mar 8, 2009 at 11:36 AM
Thanks for your reactions.
Matt, I didn't knew the path generator could use springs as well, that's a great function! I'll give it a try. What do you mean with a continuous curve exactly? Oh and another stupid question, what's the technical difference between a rope and a chain?
About the rope, can you explain me how it can be springy, if it's connected with revolute joints? And would I be able to stretch it out when it's created?
You see, the reason I want the rope to be a little springy, is that that would insert energy to the world. If the rope would be totall stiff, the character would end up hanging or lying still, because he's out of energy (you cannot move or jump the character with keys). My idea was that every time he attaches a rope, that rope is stretched out a little bit, so it pulls him up, providing new energy. On the other hand, the rope needs to be stiff enough to make a good 'swing-move', without ending up on the bottom of the level.
About your question; for now the character fires a little body with a geom, which gives a signal to a controller as soon a it collides with a rock, the controller destroys that body and creates the rope between the character and the rock, using the position of that body. This needs to be improved also, but that will be a problem for later.

Rogue, I will surely give your idea a try. It sounds interesting!
Mar 12, 2009 at 12:19 PM
Matt, I tried using your chainfactory and I think its a great function. I've got a question: The factory creates square boxes and stretches them up if the number of links is decreased. Now, I'm wondering if there is a particular reason for not using another shape, like a sphere. I think I'd prefer using (small) spheres, so they slide easier against walls.

Anyway, to get back to the topic, the chainfactory doesn't solve my problem. The problem is, the rope gets too weak when the number of bodies is increased. Untill like 5, everything is ok; the rope is stiff and doesn't stretch out much, provides the player with a fast burst of energy. When the number gets higher, the rope gets wors. Increasing the stringconstant and decreasing the restlength does some good, but not enough, plus the rope gets instable (because of the high springconstant, the bodies cannot move too close to eachother).

I think Rogue's solution is gonna help, but won't solve the problem entirely (I still have to try it), because when the rope gets longer, it will get weaker anyway. Maybe I will try creating a spring from any body to any body. But that would need a truckload of springs for a large rope...

Help! :(

Mar 13, 2009 at 12:36 AM
Unfortunately, there's not too much you can do. If you go with connecting a given body to, say, 3 or more ones next to it might work. If you write a modified chain creator that properly calculates the number of links needed (so it adds more links for each body as the chain gets longer), that should work. But you will have to be careful; get too long and the chain will slow down the engine too much. sorry about not being able to find a solution! PS: have you tried setting the weight of each link to really low so they don't have an effect on the chain?
Mar 13, 2009 at 9:49 AM
Hey thanks for your reply. My own sort of chaincreator already does that. In one variable I can set the max length of the links, so the longer the rope needs to be, the more links it create, all with the same length.
Your point about the weight was also in my mind. I have set the mass to 1, while my character is set to 70. If I lower the mass any more, the engine craches. At the end I need them to be sort of gravity-less. I saw some other topics about that.

I'll give your solution of more springs a go anyway. If that doesn't give the right result, I will try implement a variable springconstant (so the springconstant is increased when the parts are pulled away further from eachother).

As Matt said, joints can be springy too and I played around with it yesterday. I almost got what I need, but sometimes the rope loses all springforce. Maybe I'll post some more details about this also.

Thanks for your help :)
Mar 13, 2009 at 10:36 AM
Edited Mar 13, 2009 at 10:38 AM
You're welcome. Sorry i couldn't help more! on a side note, how are you controlling the character if you can't move or jump him?
Mar 13, 2009 at 2:42 PM
Don't worry, I appreciate your help.
About your question; using springy ropes ;-) You can fire and release such a rope any time. That way I hope there is enough control over the character.
Mar 13, 2009 at 9:15 PM
Hmm.... Interesting... Do i hear a tarzan game?
Mar 14, 2009 at 12:27 PM
Yeah tarzan, spiderman, call it what you want ;)
The gameconcept still needs to be proven though. I'm not sure if it's gonna work anyway. It has to wait untill my ropes are the way I want them. If everything works, I reckon it will be something original, although I'm really interested in any game that uses the same sort of idea. If you know about them, please let me know. I already found 2 rope-arms man or something, a flash game which comes close.
Mar 14, 2009 at 10:14 PM
Nope, haven't heard of anything like it. What's your plans (assuming you finish it)? sell it on Xbox Live Community Games, or just put it out on the web for people to play?
Mar 15, 2009 at 1:30 PM
Don't have that sort of plans yet. First see if it works and whether it's fun to play. I'll let it know when the game happends to be finished and can be played :)
Mar 16, 2009 at 11:02 PM
Sounds interesting, ill keep my eye out.
Apr 12, 2009 at 3:10 PM
Edited Apr 12, 2009 at 3:31 PM
Remarkable, I've just been trying to implement something extremely similar. I even fire off a body and use it's collision event to get the attach point. It's working reasonably well for me now, using the RevoluteJoint approach. Luckily my character can jump of their own accord so I don't need the rope to give them extra energy. It would have been nice, and I'd like to use the springs, but I just couldn't get them working as expected. Plus they seem to be a lot less stable than the revolute joints - I had the same problem as jordo - reducing the link wieght always crashed the sim (usually in the FixedAngleJoint.Update() of my character's upright holding joint).

Anyhow, I'm happy with the behaviour of revolute joints, but I have a couple of questions, mainly aimed @Matt Bettcher

1) What do you think of using Pools with the Paths? Maybe the Path could have an optional Pool<T> for each type of object (body, geom, spring, joint)... It's just that when you are using the Path like a ninja rope, you want to constantly add paths of different link count and remove them from the simulation. Obviously I could create that myself but I think I'd have to ditch the Path class altogether, which would be a shame.

2) By "render a continuous curve", I assume you mean create a 2D Curve with the actual bodies (or joints) as control points? That's a really cool idea, that way my rope can look as bendy as I like but still use minimum links for performance's sake.

[Edit] A third question I forgot to add, for anyone with any ideas:

3) It would be quite useful to be able to shorten the rope, whilst swinging on it - in other words, reeling yourself in. Anybody remember the ninja rope from Worms? I had endless fun swinging and shortening the rope at the right moment to create a worm flinging effect. The only way I can think of replicating this with farseer is to use LinearSprings and dynamically modifying the RestLength of each spring... but unfortunately springs seem to have the problems listed above. Any other ideas would be great.
Apr 12, 2009 at 11:57 PM
for 3, The only thing i can think of would tie into your pooled path idea... Sorry.
Apr 15, 2009 at 2:27 PM

I have something that may meet your needs. I'm using a modified version of the farseer Path() class with Linear Springs. I give the path a new field called _linkWidth which is used in place of _width when determining the distance between links. The bodies are still created using _width. After the path is built (with _linkWidth = _width * 2), I go through all the springs and set their rest length to a pretty small value. This gives the effect of creating a stretched out rubber band that springs back, as opposed to the default behaviour of a compressed rubber band that unfurls. As for the rope colliding with the terrain, this should work by default (assuming you use the correct collision categories), but I set the last geom's HandlesCollisions to false since it's the one that sits in the terrain with a FixedRevoluteJoint. It works really rather well, at least until the dreaded NaN's creep in. I'd like to try to fix that issue but I really have no idea where to start.

Perhaps if people are interested I'll get around to submitting a patch. Oh yeah, you may come across other wierdness with Linear Spring paths as described here:

Apr 15, 2009 at 4:41 PM
Hi Roonda! Thanks for your replies. How cool someone else got the same sort of idea (and how frustrating ;-)). I think my plan came out of the worms ninjarope as well. I saw some crazy movies about it on Youtube (you know them?) which inspired me.
I tried springs, I tried revolute joints, but there are always problems. I think the rope needs to be created differently for different lenghts. Like more springs when the rope is longer, or increasing strength / decreasing rest length. The problems always arise when the number of links goed up. But I'm sure it can be done, lot's of possibilities, just need to find the right combination.

BTW, I now create the rope from a collision line (which doesnt work completely yet, but is definitely the way to go). I asked a question about it here and was greatly helped by genbox, who made a demo about it. You should try it out.

Could luck on your project, keep me updated :)
Apr 15, 2009 at 6:07 PM
Edited Apr 15, 2009 at 6:16 PM
Hey Jordos - I wouldn't worry, I doubt our ideas are too similar. My ninja rope is only one component of the game, but if I get it working reliably (read: not causing NaNs), it might be important for some levels.

Yeah I can imagine how a rope using a line segment might work, but I can't really see how you'd manage to make it bend around the terrain. I'd say in worms they used line-terrain intersection and they managed it so it must be possible (however, *they* were Team17!). Maybe as it collides you store the point and angle, then pivot a new line from that collision point. When the rope swings back past the last angle, you put the pivot back to the previous collision point. That way, moving the player up and down the rope would be easy too... By Jove, it might just work!.

Thanks for getting me thinking Jordos, and good luck too.

[edit] I just checked out those worms vids... funny stuff! Back in my day you couldn't ninja rope out of the air! That is sweet.