Advertisement

a ball in a half pipe

Started by January 16, 2006 05:59 PM
5 comments, last by Tom Sloper 8 years, 7 months ago
I'm new to these Forums, so first of all, hello :) Imagine, you've got a half pipe with a ball in it. The ball can be controlled by the player, to the left and to the right. Now my problem is, how do I calculate the forces that force the ball to roll back? For example, the player steers the ball to the left, so it rolls up the hill, and then he releases the key. The ball will roll down again, and depending on the material, it will roll back up on the other side a bit, and down once more etc. I did a lot playing around, but I think, the only way, to get it working, is to use real physical logic, but I'm not the master of this :) so I hope, someone here could help me a bit :) It would be cool, if the calculations would work for a flexible ground, but if this should be too complicated, it would be enough, if it worked on a half pipe that can be calculated by a X^2 function. Thank you in advance :)
The force on the ball is due to gravity. Since the acceleration due to gravity is constant, it is easier to think in terms of acceleration instead of force when dealing with gravity.

Ok, you can split the acceleration-due-to-gravity vector (G = [0, g]) into two vectors -- one is tangent to the curve(GT) and one is perpendicular to the curve(GN). GT is the one that makes the ball move, so we can forget about GN for this problem.

The unit vector tangent to the curve is T = [dx, dy] / sqrt(dx2+ dy2). The derivative of y = x2 is dy = 2x dx, so the unit vector tangent to the curve (substituting dx = 1) is T = [1, 2x] / sqrt(1+4x2). Sorry, if this is going too fast... Finally,
    GT = T * (G dot T)        G dot T = ( [0, g] dot [1, 2x] ) / sqrt(1+4x2)    G dot T = 2xg / sqrt(1+4x2)        GT = [1, 2x] / sqrt(1+4x2) * 2xg / sqrt(1+4x2)    GT = [1, 2x] * 2xg / (1+4x2) 
Now you have your acceleration vector (GT) -- plug that into your integrator along with the current velocity and position and you should be good to go. Oh, g = -9.81 m/s2
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Advertisement
Thanks for your quick answer ;)

Well, I think I understand the most of it, but at the end, it seems like I've got problems with your last sentence :) "plug it into your integrator", what do you mean with "integrator"?
Am I right, if I just take the final equation GT = [1, 2x] * 2xg / (1+4x2) and put in the ball position as X, getting me an acceleration vector which I have to multiplicate with the current velocity of the ball?

And now if the player takes control over the ball - he would have enough power to roll it back up from where it came - is it right, to just add HIS vector to the gravitation vector? And one more thing, would the player's vector also be depending on the position, so to say depending on what the ground looks like, or can I just take any vector, like [1, 4] or something?

(Oh, and how did you manage using the Courier Font in your posting? :D )
He used and some < sup>< /sup> and < sub>< /sub> (without the blank space).

One thing I like about GDNet is the possibility to "Edit" a post made by another player. So if you see some smart formating in a post, all you have to do is to click 'Edit' on that post and you'll see how it is done.
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
Quote: Original post by ZeHa
Well, I think I understand the most of it, but at the end, it seems like I've got problems with your last sentence :) "plug it into your integrator", what do you mean with "integrator"?
Am I right, if I just take the final equation GT = [1, 2x] * 2xg / (1+4x2) and put in the ball position as X, getting me an acceleration vector which I have to multiplicate with the current velocity of the ball?

What I described was the acceleration of the ball along the curve due to gravity. If you want to compute the position based on that, you have to integrate it (because acceleration is the derivative of velocity, which is the derivative of position). For constant acceleration, finding the equation for the position (at a certain time) is simple, and the result is P(t) = P0 + V0t + 1/2 At2. In your case, acceleration is not constant and it is a function of the position, so the solution is not so simple. A naive solution via code would look something like this:
    vector acceleration = Vector( 1, old_position.x ) * 2 * old_position.x * g / ( 1 + 4 * old_position.x * old_position.x );    vector velocity = old_velocity + acceleration * time;    vector position = old_position + velocity * time;    old_velocity = velocity;    old_position = position; 
This may seem like the right answer, but it is not. It is equivalent to finding the area under a curve by summing rectangles under the curve. Now, finding the area under a curve is done by integrating the equation of the curve, so this naive solution is actually an integration (more correctly, an approximate "numerical" integration)! In fact, it is called Euler integration. Euler integration is probably the worst method, but its advantage is that it is very simple and very fast. There are others such as Verlet and Runge-Kutta. Note: there are others at GameDev who know much more than I do about this subject than I do.
Quote: Original post by ZeHa
And now if the player takes control over the ball - he would have enough power to roll it back up from where it came - is it right, to just add HIS vector to the gravitation vector? And one more thing, would the player's vector also be depending on the position, so to say depending on what the ground looks like, or can I just take any vector, like [1, 4] or something?

In order for the player to prevent the ball rolling down the curve, he must exert a force equal and opposite to the force due gravity (FG = mGT), so FP = -FG = -mGT. Any other force will cause the ball to accelerate in some direction -- A = F/m = (FP+FG)/m = FP/m+GT

John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

Hi!

Im developing an application in C# to represent a ball on an arch but i'm having trouble getting to the phisycs i need. can anyone help?

Advertisement

Diogo, this thread was dormant for ten years. Please start a new thread if you have a new question or topic. Closing.

-- Tom Sloper -- sloperama.com

This topic is closed to new replies.

Advertisement