Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » 2d only rope physics

Credits go to Birdeeoh, Evert, lambik, and Neil Walker for helping out!
This thread is locked; no one can reply to it. rss feed Print
2d only rope physics
Ceagon Xylas
Member #5,495
February 2005
avatar

I've read NeHe's tutorial on rope physics, and it's a little over my head... I was wondering where I could get started on strictly 2d rope physics.

Here's what I have in mind
image16dt.gif
This is a lamp attatched to a rope which is also attatched to the ceiling. Here's what I want to happen:
image26lc.gif
The red vector would be the bullet, and the yellow path is just the result of the bullet falling after it has hit the lamp. I'm trying to figure out the physics for the rope and lamp.

Where do I even begin with mounting the lamp on to the rope? I can see that the length, x, and y all stay constant during.

FMC
Member #4,431
March 2004
avatar

I think you should cheat :P
What i mean is that you should try a simpler approach, no need for true rope physics! In the example of the lamp you must ask yourself, how would i expect it to react?
Most probably you would expect the lamp to go back and forth, much like a pendulum... so just make it follow a pendulum's movement!

[FMC Studios] - [Caries Field] - [Ctris] - [Pman] - [Chess for allegroites]
Written laws are like spiders' webs, and will, like them, only entangle and hold the poor and weak, while the rich and powerful will easily break through them. -Anacharsis
Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover. -Mark Twain

Birdeeoh
Member #6,862
February 2006
avatar

Most probably you would expect the lamp to go back and forth, much like a pendulum... so just make it follow a pendulum's movement!

And, indeed after the initial impact, it will act just like a pendulum. However the difference between "rope physics" and pendulum physics is that pendulums are assumed to be stiff. The "fun" in the rope physics is the flexibility and how neat it will look upon initial impact when the "pendulum weight" is moving independently of the "pendulum arm" because it's a rope. :)

I don't pretend to know how to implement this in code without studying up on it...

[url http://developer.berlios.de/projects/openlayer/]OpenLayer[/url is an OpenGL accelerated 2D library for fast and easy graphics development under Allegro

FrankyR
Member #243
April 2000
avatar

Well, I know nothing about rope physics, but off the top of my head I have an idea for an approach.

1)When the rope is slack treat the lamp as it if is just a freefalling object (should be easy to do).

2)When the rope goes tight (i.e. the distance between the lamp and the ceiling where the rope is attached == the length of the rope) apply a normal force to the lamp. That is a force along the rope toward the ceiling to cancel out any movement in the direction along the rope (plus any you want to add for the sake of elasticity in the rope).

If you're in a situation where the rope is staying taut at all times, then giving the lamp pendulum motion sounds like a good idea.

Evert
Member #794
November 2000
avatar

The easiest way that I know of to simulate a rope is to approximate it as a sequence of connected weights.
In zero-th order approximation, the rope would be a stiff rod.
In first order approximation, it would be two rods connected together halfway through the length of the rope and allowed any motion relative to eachother as long as the ends stay connected.
The second order is three connected rods, et cetera ad infinitum.

Instead of stiff rods, you can use weights connected by massless springs. By varying the spring constant you can change how the rope reacts to being pulled and twisted - you can even vary it along the length of the rope to simulate a weak spot in the rope. In the limit where the spring constant becomes infinite you should get back the stiff rod.

Be warned though that this approach gives rise to a rather bothersome set of coupled equations that will need to be solved (because the force on the first mass element produces a torque and a force on the second mass element, which produces a force and a torque on the third... etc).

X-G
Member #856
December 2000
avatar

Also beware of the ghastly effects that occur if you don't integrate with a fine enough timestep or don't account for friction properly.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Neil Walker
Member #210
April 2000
avatar

Couldn't you do something like plot three points, one at the top, one half way down and one at the bottom. Make the bottom one move further than the middle in a slight arc then join the top to the two points using catmull-rom spline?

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

nonnus29
Member #2,606
August 2002
avatar

I think for something like this it would be simpler in the end to implement some actual physics:

Quote:

Be warned though that this approach gives rise to a rather bothersome set of coupled equations that will need to be solved (because the force on the first mass element produces a torque and a force on the second mass element, which produces a force and a torque on the third... etc).

Verlet! Verlet! This is the type of thing verlet integration excels at from what I've been able to gather....

Neil Walker
Member #210
April 2000
avatar

Or you could download newton dynamic library or ODE and create a 2d rope rather easily with some hinged joints :)

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

Ceagon Xylas
Member #5,495
February 2005
avatar

Ew ew ew, sorry guys. I stupidly posted this the day before Oblivion arrived at my front door. Of course I've been doing that with all my free time. Back to programming classic games! =D
Lemme check this stuff out...

The reason I don't want a pendulum effect is precisely because of the stiffness. My game offers 360 degree directional shooting... So if you hit the lamp from the bottom, I want it to move up-wards into the ceiling and bounce off.

Quote:

The easiest way that I know of to simulate a rope is to approximate it as a sequence of connected weights.

Yes, this sounds like the best choice to me. Would you know where I could see some code for it?

Quote:

Or you could download newton dynamic library or ODE

Can I get some links for those? I searched and bleh nothing useful came up after a while of sifting through google pickups.

lambik
Member #899
January 2001
avatar

I don't know if this is what you are looking for but just on the off chance:http://freespace.virgin.net/hugo.elias/models/m_main.htm

Ceagon Xylas
Member #5,495
February 2005
avatar

Ooo...This is handy!

[edit]
Yes, this is very much what I was looking for! Thank you!

[edit again]
Okay, I've got my rope working almost perfectly. The only problem is that both ends of the rope are static... I want only one static point [mounted to the ceiling] and the other to interact with the lamp. Here's my code.

1class rope{
2public:
3 class joint{
4 public:
5 float x1,y1,x2,y2;
6 float velX,velY;
7 }joint[10];
8 
9 float seg_length; //length of segments between joints
10 float air_resistance; //air resistance [negating factor on gravity {9.8}]
11 float damping; //self-explanitory
12 float speed; //didn't know what else to name this
13
14 void deploy(); //initializes the rope
15 void update(); //called every game-cycle
16}rope[20];
17 
18void rope::deploy() {
19 for(short i=0; i<10; i++) {
20 joint<i>.x1=320+i; joint<i>.y1=i*3;
21 joint<i>.x2=320; joint<i>.y2=i*3;
22 joint<i>.velX=0; joint<i>.velY=0;
23 }
24 seg_length=1;
25 air_resistance=0.1;
26 damping=0.95;
27 speed=0.01;
28};
29 
30void rope::update() {
31 for(short i=0; i<10; i++) {
32 if(i>0 && i<10) {
33 float vx1=joint[i-1].x1-joint<i>.x1; //vector 1: diffrence between x's
34 float vy1=joint[i-1].y1-joint<i>.y1; //vector 1: diffrence between y's
35 int vm1=sqrt(ceil(vx1*vx1+vy1*vy1)); //vector 1: magnitude
36 int ve1=vm1-seg_length; //vector 1: extention
37
38 float vx2=joint[i+1].x1-joint<i>.x1; //vector 2: diffrence between x's
39 float vy2=joint[i+1].y1-joint<i>.y1; //vector 2: diffrence between y's
40 int vm2=sqrt(ceil(vx2*vx2+vy2*vy2)); //vector 2: magnitude
41 int ve2=vm2-seg_length; //vector 2: extention
42
43 float vx=(vx1/vm1*ve1)+(vx2/vm2*ve2); //x's reactant
44 float vy=(vy1/vm1*ve1)+(vy2/vm2*ve2)+9.8-air_resistance; //y's reactant
45
46 joint<i>.velX=joint<i>.velX*damping+(vx*speed); //set velocity x accordingly
47 joint<i>.velY=joint<i>.velY*damping+(vy*speed); //set velocity y accordingly
48 joint<i>.x2=joint<i>.x1+joint<i>.velX; //buffer x
49 joint<i>.y2=joint<i>.y1+joint<i>.velY; //buffer y
50 }
51 joint<i>.x1=joint<i>.x2; //copy buffer to physical
52 joint<i>.y1=joint<i>.y2; //copy buffer to physical
53
54 //draw joint and lines between
55 }
56};

http://img93.imageshack.us/img93/1467/rope0ue.gif

My brain can almost grasp it... joint[0]'s x and y are controlled by up, down, left, and right; and will be assigned a static point later on in the coding of deploy();. So I need to make joint[max]'s x and y controllable by the lamp. But how?!

fuzinavl
Member #4,105
December 2003
avatar

looks just like the old lamps in elevator action! :D

__________________________
fuzinavl@hotmail.com (Pittsburgh)
__http://fuzinavl.tripod.com/__

Go to: