Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411500 Posts in 69373 Topics- by 58428 Members - Latest Member: shelton786

April 25, 2024, 12:16:18 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Pseudo-physics for segmented whip?
Pages: [1]
Print
Author Topic: Pseudo-physics for segmented whip?  (Read 1412 times)
Hydorah
Level 0
**


Improved Push Movement (Final?)


View Profile
« on: June 18, 2015, 02:09:55 AM »





What would be some possible pseudo code for segmented whip physics? I'm not even asking for being able to control it, just the physics of having the whip fall down and keep together.

Simple physics formulas would do for putting this thing together, some formulas I could apply to every segment. Everything on wikipedia is a bunch of math jargain, and every other place tells me to use a physics extension/dll, which I would rather stay really far away from. I would hate having to add an entire library just to add a dangling whip.
Logged
Allen Chou
Level 0
**



View Profile WWW
« Reply #1 on: June 18, 2015, 11:34:46 AM »

The simplest thing I can think of is enforcing a distance constraint starting from the root node, after all the nodes have been affected by other physical factors like gravity.

Code:
for (int i = 1; i < numNodes; ++i)
{
  Node& prevNode = nodes[i - 1];
  Node& currNode = nodes[i];
  currNode.pos = segmentDistance * Normalize(currNode.pos - prevNode.pos);
}

This is a positional correction. You can also consider applying the same principle to correct velocity instead for smoother visuals.
Logged

RandyGaul
Level 1
*

~~~


View Profile WWW
« Reply #2 on: June 18, 2015, 11:47:16 AM »

You're right, you don't need a whole library.

You can try what Allen suggested and it'll probably look ok, but I'd recommend just doing it well to start with. You can do the whole rope simulation in just a few lines of code:



(source in description)

Here's the pieces of source you care about most:

Code:
void Solve(real dt)
{
for (u32 i = 0; i < g_verts.size() - 1; ++i)
{
RopeVert& a = g_verts[i];
RopeVert& b = g_verts[i + 1];

Vec2 v = b.position - a.position;
real length = v.Len();

if (length != real(0.0))
{
real error = kMaxDist / length - real(1.0);
Vec2 correction = v * error;

if (i != 0)
{
real invMass = a.invMass + b.invMass;
a.position -= correction * (a.invMass / invMass);
b.position += correction * (b.invMass / invMass);
}
else
{
b.position += correction;
}
}
}
}

void Integrate(real dt)
{
for (u32 i = 1; i < g_verts.size(); ++i)
{
RopeVert& a = g_verts[i];

a.velocity += Vec2(0.0, 9.8) * dt;
a.position += a.velocity * dt;
}
}

void VelocityFixup(real inv_dt)
{
for (u32 i = 1; i < g_verts.size(); ++i)
{
RopeVert& a = g_verts[i];

a.velocity = (a.position - a.oldPosition) * inv_dt;
a.oldPosition = a.position;
}
}

void SolveRope(real dt)
{
Integrate(dt);

for (u32 i = 0; i < 8; ++i)
Solve(dt);

VelocityFixup(real(1.0) / dt);
}

The idea, like Allen was suggesting, is to loop over each piece of the change and shorten/grow it if it's too long/short. You do this a few times in a loop, and you're almost done. The trick to making it look nicer is to keep track of the "velocity" of each particle in the rope, which is just currentPos-prevPos. Then you can use the velocity for integration.

Edit: If the rope looks floaty just raise the gravity constant (and also probably increase iterations to prevent rope from stretching).
Logged
Hydorah
Level 0
**


Improved Push Movement (Final?)


View Profile
« Reply #3 on: June 18, 2015, 01:29:07 PM »

Thanks for the code, Randy! I'll look over it.

Edit: It's just very... Very cloth-like. Floaty. Not metallic feeling at all. I think I need a system that uses "damping", or whatever it's called.
« Last Edit: June 19, 2015, 01:33:04 AM by Hydorah » Logged
oahda
Level 10
*****



View Profile
« Reply #4 on: June 19, 2015, 01:45:40 AM »

Edit: If the rope looks floaty just raise the gravity constant (and also probably increase iterations to prevent rope from stretching).
Logged

RandyGaul
Level 1
*

~~~


View Profile WWW
« Reply #5 on: June 19, 2015, 12:01:03 PM »

Damping will make it more floaty  Shocked

If you take a little time to learn about velocity and integration, then you can make your rope do whatever you want.

Edit:
Try: raise gravity a lot, increase iterations, use less particles but longer segments, render chain however you like (can use splines to make it look like you have many particles, or more segmented and metallic).
Logged
Hydorah
Level 0
**


Improved Push Movement (Final?)


View Profile
« Reply #6 on: June 20, 2015, 03:54:35 AM »





Thanks for all the help, everyone. I got the exact results that I needed.
If you need the GM:S code, PM me.
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic