Skip to main content
gamedev.net
🔒 Locked

Constraint Physics

Started by digital_phantom Dec 9, 2005 at 2:21 AM 1 replies 850+ views
Original Post
digital_phantom
digital_phantom
I just a source code of what somebody wroten of a constaint physics demo in LUA. Earlier today I ported the code over to Visual Basic for I can learn how it's done, but there are several things going on it I don't understand, so I'm hoping someone body here can give more information on these things. The demo uses a points list and a connections list. In area in the loop code does this: d.x = (point1.x - point2.x) d.y = (point1.y - point2.y) speed = Sqr((d.x * d.x) + (d.y * d.y)) f = ((speed - connection.speed) / connection.speed) * 0.45 point1.x = point1.x - f * d.x point1.y = point1.y - f * d.y point2.x = point2.x + f * d.x point2.y = point2.y + f * d.y What exactly is going on here? Any help is very welcomed and thanks in advance.
someusername
someusername
hi.

It looks like it's a code snippet that tries to keep two points at relative distances with each other (constrained as you said)
Quote:

d.x = (point1.x - point2.x)
d.y = (point1.y - point2.y)
speed = Sqr((d.x * d.x) + (d.y * d.y))

It finds the distance between the 2 points but mistakenly (?) calls it speed!
(This could be interpreted as speed, if-f point1 was point2's position in the previous frame, but then again, it would have to be divided by dt, the time delta between two consecutive frames)

Quote:

f = ((speed - connection.speed) / connection.speed) * 0.45

It calculates an arbitrary linear factor, sth like the amount of a force, to apply later on to the points. If the two points originally coincided, this is 0.45
If the distance between the points was 10 (speed==10) the it is 4.5*speed/connection.speed (== const*speed, thus linear to 'speed'), etc.

Quote:

point1.x = point1.x - f * d.x
point1.y = point1.y - f * d.y
point2.x = point2.x + f * d.x
point2.y = point2.y + f * d.y

It pulls the points closer along their direction, based on the factor f.

As I said the name "speed" is somewhat baffling, but it's pretty much an implementation of a "spring" attached to those two points (since the force is linear to their displacement)

If 'f' is supposed to be force- it shouldn't be added to the points' positions just like that.

This could only be conceptually correct, if 'speed' was speed indeed and the '0.45' factor was time delta.
Then f would be sth like a "displacement delta" between the objects , and it could be added their positions *if turned to the appropriate direction* with the appopriate unit vector ( normalize(d.x,d.y) )
Still, though, f is multiplied by a vector of arbitrary length (d.x, d.y) and is then added to the positions.
digital_phantom
digital_phantom
That would be my fault on calling it speed, because when I seen the 'Sqr((d.x * d.x) + (d.y * d.y))' part, to me that's the formula which calculates the magnitude which is used for speed.

I was playing around with the 0.45 value, and if I set it too much, the polygon is stretched all over the screen, if I set it really low, the polygon's points are bouncy and unstable.

Topic Locked

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

Sign in to reply to this topic.