Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411977 Posts in 69438 Topics- by 58486 Members - Latest Member: Fuimus

June 15, 2024, 01:38:11 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Elastic physics - help
Pages: [1]
Print
Author Topic: Elastic physics - help  (Read 4156 times)
ChevyRay
Guest
« on: June 24, 2010, 08:24:03 PM »

Alright math people, time to help Chevy out. I'm not labelling this under Flash because I could probably get a solution in any language and easily port the code, but just in case anybody is wondering, I am coding this in AS3 with Box2D.

- I have object A and object B.
- I want them to be attached together with an elastic.
- The more this elastic is stretched, the more I want these bodies to be pulled back together.
- So basically, at a certain point, the strength of the elastic will balance with the force being applied, and the elastic will be stretched to its "limit".
- I also want to be able to "snap" the elastic when too much force has been applied, for certain cased, but I could probably figure that out on my own if I can get the first part of this working.



If you can help me, the string to my heart that has always been secretly attached to yours will pull tight, and our bosoms will be reunited forever. If you can do it without linking to a page that has mathematic equations from jupiter that I know not how to turn into code, then my heart will not break under the pressure.

Choose carefully, my white knights! I am waiting for you <3
Logged
Chris Z
Level 7
**



View Profile WWW
« Reply #1 on: June 24, 2010, 08:44:29 PM »

Sounds like a mass-spring-damper system, I've never implemented it unfortunately so no code from me but it seems like there should be some examples on the web.  Apologies if you already went down this road.

The elastic rope aspect is just a matter of rendering based on how relaxed the spring system is.

Equations from Jupiter
Logged

ChevyRay
Guest
« Reply #2 on: June 24, 2010, 08:53:58 PM »

Yeah, I can handle the rendering, it's just the actual physics I'm trying to work out. I tried google, but since I didn't exactly know what it was called, I thought I'd just ask directly and get an answer tailored to what I need. Thanks for the link, I'll check that out now and see if I can make sense of it.

EDIT: Oh yes, font spaghetti time!



But what you linked to (at least the GIF on that page anyways) looks like exactly what I need, so at least I have a name for it now, which will help me in my search.
Logged
Zachary Lewis
Level 1
*


Professional by day, indie by night.


View Profile WWW
« Reply #3 on: June 24, 2010, 08:57:54 PM »

You should be looking at Hooke's Law. It states that the force is equal to the displacement in the system times a constant.

F = -kx

What you'll want is a distance before the "spring-ness" begins. After that distance, you'll apply a force to the characters based on how far away it is.

Psuedo-code idea:
Code:
const int ROPE_LENGTH
const int K
Entity a, b;

function gameLoop()
{
  //Check our distance.
  if(distance(a,b) > ROPE_LENGTH)
  {
    //Figure out the force on the system.
    //Get our displacement.
    int x = (distance(a, b) - ROPE_LENGTH)
    //Apply Hooke's Law
    int f = K * x
    //Now, slap equal but opposite forces on the bitches.
    a.v += f/2
    b.v -= f/2
  }
}

That's where I'd start. I don't think you need damping, just a simple spring should work. You just need to keep the rope-length in mind.

Also, if the objects are of differing mass, you'll want to vary the forces applied to them based on their masses (asses, heh).
« Last Edit: June 24, 2010, 09:10:26 PM by Zachary Lewis » Logged

Farbs
Level 10
*****


Pew pew! Videogames!


View Profile WWW
« Reply #4 on: June 24, 2010, 09:03:16 PM »

Okay. The first thing to consider is that simple spring/elastic models just scale force with distance. Ignoring elastic slack for now you could model it like this:
Code:
Get vector from A->B
Multiply it by some constant (springiness)
Apply it as a force to A
Invert it
Apply it as a force to B

In AS3/Box2D this might look something like:
Code:
var disp:b2Vec2 = B.GetPosition().Copy()
disp.Subtract( A.GetPosition() );
disp.Multiply( 0.5 );
A.ApplyForce( disp );
disp.Invert();
B.ApplyForce( disp );

Note that I probably got some function names wrong in there. Next up we want to add some slack, so that the force is only relative to the distance between A and B over a given amount. The pseudo could look something like:
Code:
Get vector from A->B
Get length of vector - we'll call this C
Subtract some constant from C (slack distance)
Multiply C by some constant (springiness)
Clamp C value to >=0
Normalize displacement vector
Multiply displacement vector by C
Apply vector as a force to A
Invert it
Apply vector as a force to B

...which in Box2D/AS3 would look something like:
Code:
var disp:b2Vec2 = B.GetPosition().Copy()
disp.Subtract( A.GetPosition() );
var spring:Number = disp.Length();
spring -= 10.0;
spring *= 0.5;
spring = Max( spring, 0 );
disp.Normalize();
disp.Multiply( spring );
A.ApplyForce( disp );
disp.Invert();
B.ApplyForce( disp );

Chris is probably right though that you'll want to dampen the motion a little. Things tend to get a bit zany otherwise.
Logged
ChevyRay
Guest
« Reply #5 on: June 24, 2010, 09:07:00 PM »

@Zachary: Yeah, the rope length is something I'm going to need to be able to edit and tweak specifically, so that sound about right. Your code looks retardedly more simple than I thought this was going to be. Epileptic

Could you explain to me what each of these values stand for? F is a force equal to k and x which are... what? And what exactly is the K constant, the restitution/springiness of the string? What does changing K have on the effect of this system? Last question: what is v?

Thanks!

EDIT: Aaaand thanks too, Farbs! I think I've got more than enough info to rip myself up some code now, you guys are awesome, thanks for the quick replies!
Logged
Zachary Lewis
Level 1
*


Professional by day, indie by night.


View Profile WWW
« Reply #6 on: June 24, 2010, 09:15:45 PM »

@Zachary: Yeah, the rope length is something I'm going to need to be able to edit and tweak specifically, so that sound about right. Your code looks retardedly more simple than I thought this was going to be. Epileptic

Could you explain to me what each of these values stand for? F is a force equal to k and x which are... what? And what exactly is the K constant, the restitution/springiness of the string? What does changing K have on the effect of this system? Last question: what is v?

Thanks!

EDIT: Aaaand thanks too, Farbs! I think I've got more than enough info to rip myself up some code now, you guys are awesome, thanks for the quick replies!

K is the spring constant. This will be constant (derp).
x is the displacement from rest. Stretch a spring 2 inches, x will be 2 inches.

The higher K is, the more force will be applied per displacement. So, if K is higher, the spring is stiffer.

V is the velocity of the objects, but I skipped some steps: Since force = mass x acceleration (and the masses are considered to be constant), you can simplify it down to setting the acceleration to the force. Then, you apply acceleration each tick.

Code:
a = f / m
v += a

Logged

bateleur
Level 10
*****



View Profile
« Reply #7 on: June 24, 2010, 10:09:13 PM »

Chris is probably right though that you'll want to dampen the motion a little. Things tend to get a bit zany otherwise.

The grappling hook in The Unicyclist works using a system like the one you describe (see

, from about 25s in). Rather than applying damping there I found I preferred the results achieved by simply putting a hard cap on the maximum force.

The advantage of doing this is that the elastic still feels really springy at short extensions but you can't get it to do completely mad things like accelerating objects through walls.
Logged

muku
Level 10
*****


View Profile
« Reply #8 on: June 25, 2010, 12:36:39 AM »

The grappling hook in The Unicyclist works using a system like the one you describe (see

, from about 25s in).

Aaah, I had the idea to make a unicycling game quite a bit like that a while ago. I guess that idea's out of the window Sad
Logged
slembcke
Level 3
***



View Profile WWW
« Reply #9 on: June 25, 2010, 06:25:58 AM »

This page has a nice interactive applet as well as copious explanations of the math involved.
Logged

Scott - Howling Moon Software Chipmunk Physics Library - A fast and lightweight 2D physics engine.
Toeofdoom
Level 2
**



View Profile WWW
« Reply #10 on: June 26, 2010, 04:03:52 AM »

If you're using box2d, I think the distance joint can function as a spring (it has a frequency value iirc) but it will also apply force when it's shorter than natural length. It could be something to base it on, though.
Logged

st33d
Guest
« Reply #11 on: June 29, 2010, 02:28:10 PM »

I made a sort of springy cord in Rubble Trouble by simply connecting a bunch of rectangles with revolute joints. This cord I filtered out of the collision but attached to a helicopter object and a wrecking ball.

Okay, so it was the least popular tool in the game according to some.

But this was because the joints in Box2D are a bit springy in holding their position. The more joints you have in a chain, the more spongy Box2D gets. I tried to make a wrecking ball on a chain, what I got was a wrecking ball on an elastic band.

You could try out this "bug" for yourself and see if it's the effect you're looking for. It could even allow you to drape the chain over scenery.
Logged
B_ill
Level 0
***


View Profile WWW
« Reply #12 on: June 29, 2010, 03:40:04 PM »

Another cool thing to note about Hooke's Law:

F = - (k * x)

F is a force vector (the mathematical type, not the data structure type), which means it has both a magnitude and a direction. x, the position, is also a vector. The - in the equation has the fancy effect of pointing the force always in the opposite direction of the displacement. That might be obvious to some people, but others may be wondering about it.
Logged

Game Programmer and Designer
Latest Release: Chemical Cubes for Android and Kindle Fire (iOS coming soon)
ChevyRay
Guest
« Reply #13 on: June 29, 2010, 04:14:28 PM »

Actually, that clears things up quite a bit for me. Thanks for the explanation, Bill N, I think I've got a clearer grasp on what's happening now, and this seems quite simple.
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic