Skip to main content
gamedev.net
🔒 Locked

Rope physics

Started by Doctor Shinobi Jun 14, 2010 at 8:06 PM 4 replies 1.1k views
Original Post
Doctor Shinobi
Doctor Shinobi
Ehh well, i am not sure it's called rope physics,but here's what i want to do :


I have a point somewhere, and I want to connect the player with that dot to force the player to move around it. A pretty good example of this could be the flying rings from rayman. you could attach yourself to those rings and move in a spherical way around it


So i tried thinking about how to do it. First thing i came up with was predefining an up vector(0,1,0) and getting the angle between that vector and the vector which is between the player and the dot the player will move around. Then use sin and cos to get some kind of a direction vector. But thinking about this a bit further made me understand that multiplying that vector with the player's current velocity vector just won't work,so eh.. I am kind of stuck now.

So what would be a good way to do it? especially in 3D?
Hodgman
Hodgman
An easy way to achieve this is to implement a "constraint".

You run your physics as normal (letting the player move away from the "anchor point"), and then your apply your constraints afterwards to snap the player back into the right position.
For a rope, the constraint is "distance between player and anchor must be less than rope length".

Or in code form, something like:
difference = playerPos - anchorPos;length = difference.length();if( length > ropeLength ){  direction = difference / length;  playerPos = anchorPos + direction * ropeLength;}
Doctor Shinobi
Doctor Shinobi
Thank you both for the answers.
so if my anchor is at point 0,0(simplifying it to 2D for now) and the player is at 10,5,and we say that it's enough for the player to be connected to the anchor,then :
difference = -10,5
length = square 10*10 + 5*5 = 11.180339887498948482045868343656(woo!)
direction = -10 / length,5 / length = -0.894...,0.447...
Once the player is attached, i think it's better to make the rope length the same as the length here(and do it once). That way,i won't need to brutally force the player to move to the edge.

So did i do the calculation right? Would it really make a good solution?

edit : Considering i don't actually change the player's velocity, i think this could be a bit problematic
h4tt3n
h4tt3n
A simple and robust way to do this is by applying a damped spring between the point (optionally several points) and the free moving body.

Calculate spring force with hooke's law of elasticity:

f = -k * (d - dr)

where f = force, k = spring stiffnes coefficient, d = distance, dr = rest distance (the desired distance)

You also need a velocity based damping, but unfortunately I haven't the time to go into detail with that right now.

Cheers,
Mike
Doctor Shinobi
Doctor Shinobi
Quote:
Original post by h4tt3n
A simple and robust way to do this is by applying a damped spring between the point (optionally several points) and the free moving body.

Calculate spring force with hooke's law of elasticity:

f = -k * (d - dr)

where f = force, k = spring stiffnes coefficient, d = distance, dr = rest distance (the desired distance)

You also need a velocity based damping, but unfortunately I haven't the time to go into detail with that right now.

Cheers,
Mike


That does seem simple :). But i am not sure i understand the difference between d and dr. is d the distance of the spring without forces applied to it?


Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.