Skip to main content
gamedev.net
🔒 Locked

Pendulum Motion C#

Started by Owen Deery May 27, 2009 at 12:56 PM 6 replies 6k views
Original Post
Owen Deery
Owen Deery
I'm working towards creating a mass + spring system in C# right now, but I got stuck on a piece of code which I thought would be trivial. To test out the spring and mass behavior I created a fixed mass and a free mass which is only affected by gravity and attached both masses by a rigid spring. The part I'm having trouble with is making the free mass swing like a pendulum. I was under the impression I would be able to solve the problem using circular motion and angular force equations but I can't seem to get it to work out. I've scoured for every piece of related information I can find on the internet, but was only able to find pages describing mathematical study of force equations. Also, it seems there are about a dozen different ways to solve pendulum forces. Does anyone have any tips for which direction I should be heading in, or equations I should be focusing on? Thanks.
Nypyren
Nypyren
With that setup (minus the angular stuff), it should act at least SOMEWHAT like a pendulum.

If your spring is too weak, the free mass won't follow a nice pendulum arc (it'll stretch the spring too easily and get into a chaotic path).

Alternatively you could instantaneously constrain the connection between the free mass and the fixed point, which would give you a perfect pendulum arc and wouldn't require a spring at all.
Owen Deery
Owen Deery
Here's my current equation for calculating the velocity of the free mass based on it's connection to the spring (not the actual code):

freeMass.velocityX += -spring.K * ((freeMass.X - rigidMass.X) - spring.Length);
freeMass.velocityY += -spring.K * ((freeMass.Y - rigidMass.Y) - spring.Length);

Right now I've got both masses set up in a an arrangement like this:

O--------O
Rigid Free

The free mass moves along the Y-axis due to a small gravity force I've added but there's no change whatsoever in the X-axis.

PS: I'm still very much a game dev student, so feel free to to tell me I'm doing everything wrong.
Zbychs
Zbychs
You can read about physics in Hitman (it's based on masses + springs to create rigid bodies, ragdolls, plants, cloths etc.). The article is very easy to understand: Hitman Physics.

Also you don't have to take angular velocities and forces into account - only linear velocities and forces + spring constraints. The angular components would be needed ONLY if your masses were not "points", but "blobs" attached not in their center of mass.

Another source for easy to understand introductory physics for games stuff are Chris Heckers physics articles.
Zbychs
Zbychs
About your code: it's generally ok, but you can't really compute it separatelly for x and y. You have to compute it at the same time.

So:

dist = sqrt( (x2 - x1)^2 + (y2 - y1)^2 )
a = - k * (dist - springLength) / mass
velocityVector += directionVec * a * time

Where directionVector is a normalized vector from obj1 to obj2, and I assume mass and time are both 1 in your simplification.
gpu_fem
gpu_fem
In your equation, the force due to the spring is -k(sqrt(x*x+y*y)-L0), where x and y are the position of the mass, and L0 the length; i.e., you have to look at the extended length of the spring. The resultant force is directed along the length of the spring, so that:

F = -k(sqrt(x*x+y*y)-L0)
Ls = sqrt(x*x+y*y)
dx = x/Ls
dy = y/Ls
Fx = F*dx // Force in x direction
Fy = F*dy // Force in y direction
gpu_fem
gpu_fem
Zbychs beat me to it

Sorry
Owen Deery
Owen Deery
Amazing, its working now.

Thank you all so much.

Topic Locked

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

Sign in to reply to this topic.