-
-
- (0)
-
premium membership
-
Posts
13
|
|
I'm trying to introduce a simple 'rope swing' mechanic into my game, and I was wondering if anyone had a methodology for converting rotational concepts into code. I'm only interested in pendulum like motion for now (that is, the rope will remain 'rigid'), and I'm hoping to be able to deal in simply the standard XNA model variables (velocity, etc) without dealing with things like Potential engery (though if this is the only way, that's okay too).
If anyone has anytips, that'd be great.
Thanks.
|
|
-
|
|
Re: Swinging Rope Physics
|
Hello, use the following:
angle = angleMax * sin ( Math.Sqrt(g/L) *t ) with : g = gravity so 9.81 L = length of rod t= time
regards,
<!-- I am not perfect, therefor I am -->
|
|
-
-
- (0)
-
premium membership
-
Posts
13
|
Re: Swinging Rope Physics
|
I'm sorry, I don't totally follow. What exactly is angleMax? I assume this equation gives me the angle of movement (relative to the position vector in the previous state) per, in my case, frame?
Thank you so much for the help.
|
|
-
|
|
Re: Swinging Rope Physics
|
The equation you have posted is for a simple pendulum and only works for angles <10 degrees. Which might be fine, but if you want to be accurate you need to use a more complicated formula
http://upload.wikimedia.org/math/1/f/1/1f1e27a699b66866fd6195c4291f0e23.png
have a look at this
http://en.wikipedia.org/wiki/Pendulum
|
|
-
-
- (0)
-
premium membership
-
Posts
13
|
Re: Swinging Rope Physics
|
Hmm...
It seems like the easiest way to do this, then (as far as computational power is concerned) is to simply use the concept of conservation of energy, no?
Just using Total Energy = PE + KE at any given point should allow me to come up with a speed, but I'm not positive that I'll be able to determine the velocity (direction) of the 'mass' at each update call. For anyone who's done this, do you have a solution for this method?
|
|
-
|
|
Re: Swinging Rope Physics
|
Try this tutorial: http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=40
Although it's written for C++ using OpenGL, I was able to port it to C#/XNA relatively easily a few months ago (I don't have it anymore). It works great.
|
|
-
-
- (0)
-
premium membership
-
Posts
13
|
Re: Swinging Rope Physics
|
Ah, thank you very much. This is more complex than I was looking for, but it seems relatively straight forward, so I'll give it a go. Thanks for the tip.
|
|
-
|
|
Re: Swinging Rope Physics
|
In case the rope simulation gets too complex, I will try to explain the equation of the harmonic oscillatior a bit more:
angleMax is the amplitude of the swinging motion - so if you choose 30degrees, the pendulum will swing back and forth between -30 and +30 degrees. The length of the rod should be set in meters and the time in seconds - so that would basically be (float)elapsedTime.TotalSeconds. Given all that, the formular angle = angleMax * sin ( Math.Sqrt(g/L) *t ) gives you the angle of your pendulum object at any given time. It does not give you any velocity or position of the tip of the pendulum, but you can use the angle value that you get from this equation to rotate your pewndulum object sprite accordingly over time for example like this:
spriteBatch.Draw(pendulum, new Rectangle(x,y,width,height), null, new Color(r, g, b), angleMax * sin ( Math.Sqrt(g/L) *(float)elapsedTime.TotalSeconds ), new Vector2(originx,originy), SpriteEffects.None, 0f);
You just need to adjust the values for the sprite dimensions (x,y,width,height) and the location of the origin around which your rotation should happen: Vector2(originx,originy).
If you need to know the speed and/or position of the tip of the pendulum, you can calculate these based on simple vectormath using the given angle.
With this implementation you will be stuck with a fixed oscillation movement. If you want that the pendulum reacts to other objects in a physically meaningful way, you will need to do a more elaborate simulation such as the rope simulation. have fun,
Leif
|
|
-
|
|
Re: Swinging Rope Physics
|
prthealien:Try this tutorial: http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=40
Although it's written for C++ using OpenGL, I was able to port it to C#/XNA relatively easily a few months ago (I don't have it anymore). It works great.
Yeah, I just ported it as well, it works great.
|
|
-
|
|
Re: Swinging Rope Physics
|
Bapabooiee:
prthealien:Try this tutorial: http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=40
Although it's written for C++ using OpenGL, I was able to port it to C#/XNA relatively easily a few months ago (I don't have it anymore). It works great.
Yeah, I just ported it as well, it works great.
hay, if you've already ported it and tested it working, could you possibly post it here.
So to aid anyone else looking for a solution to this problem
Thanks
Darkside http://thegamedevspace.spaces.live.com/ 
|
|
-
-
- (0)
-
premium membership
-
Posts
13
|
Re: Swinging Rope Physics
|
Yeah, that would be great if you could post the C# code. I just got back from vacation, and was going to port it myself, but you could really save some time. I'd appreciate it (as would others searching the forums, no doubt).
|
|
-
-
- (0)
-
premium membership
-
Posts
8
|
Re: Swinging Rope Physics
|
I'm starting to look at using this in my game. I'm using the TorqueX 3D game engine, so I'm trying to work with thier physics if I can. Has anyone found any open source solutions?
|
|
-
|
|
Re: Swinging Rope Physics
|
|
|
-
|
|
Re: Swinging Rope Physics
|
Damn. I used to have some rope-physics code that was implemented in C++, and then ported over to C#. I did some interesting stuff with it... but I have no idea where that old code is =[
Anyway, that was a long time ago. I guess I'm on the same page as the OP. I see some purdy damn interesting articles upstairs, so I'm going to check those out =D
|
|
|