• Create Account

# Need help with spring constraint

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1Killroy  Members   -  Reputation: 108

Like
0Likes
Like

Posted 26 December 2013 - 08:54 AM

Hi. I'm trying to implement a spring/elastic constraint into a game and am having trouble getting a certain part to work. Right now, 'Part B' (Pb) is attached to 'Part A' (Pa), and it works fine. The problem is that when Part B is moved, Part A does not move. I've tried applying an equal and opposite force on to Part A but that causes them both to fly off and go crazy at a high speed, so I guessed that was wrong.

Here's the code I have so far: (It's in lua, but is self-explanatory)

k = 64
b = 0.1
length = 10

dist = (Pa.Position - Pb.Position).Magnitude
unit = (Pa.Position - Pb.Position).Unit
force = (dist - length) * k
resist = Pb.Velocity * b * Pb:GetMass()
vector = (force * unit) - resist

applyForce(Pb, vector)


Don't know if this is the right place to post this, but Idk where else I could. Thanks

### #2Álvaro  Crossbones+   -  Reputation: 9949

Like
1Likes
Like

Posted 26 December 2013 - 09:06 AM

The code strikes me as strangely asymmetric. The `resist' term should probably depend on the difference of the velocities of Pb and Pa, not just the velocity of Pb. Also, I would either not mention the mass of Pb or I would use something like the sum of the masses of Pb and Pa. And you should definitely apply the opposite force to Pa.

### #3Killroy  Members   -  Reputation: 108

Like
0Likes
Like

Posted 26 December 2013 - 10:02 AM

Thank you. I made those changes and it works.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS