Predict bounce

BlitzMax Forums/BlitzMax Programming/Predict bounce

siread(Posted 2007) [#1]
I want to find the best way to predict the coordinates of where a ball will bounce.

The ball is affected by its x,y,z velocities, gravity and air friction.

xvel:-friction
yvel:-friction
zvel:-gravity

x:+xvel
y:+yvel
z:+zvel

Is there a way to determine what the x and y position will be when z next hits zero?

Obviously I could run a loop which simply plays out the motion of the ball until z hits zero, but this wastes time. I'd rather calculate the co-ords.


Czar Flavius(Posted 2007) [#2]
Isn't the z-coordinate affected by air friction too?

You'll probably need to use equations of motion.

http://en.wikipedia.org/wiki/Equations_of_motion

Times like this I'm glad I took Mechanics at school!


Dreamora(Posted 2007) [#3]
sure there is a way.

just create temp variables like you have already that use the current x,y,z (after that calc above) and simulate the next step.

With that simulation you are able to 1. predict z<0 if you need that or predict nearly anything else if the difference of the 2 positions is small enough :)


Fry Crayola(Posted 2007) [#4]
As Czar Flavius says, you'll want to brush up on your mechanics for this.

If we ignore air friction it's fairly easy. Using the ball's vertical velocity, as well as acceleration due to gravity (9.81 metres per second per second, I'm sure you know), you can calculate the exact time it will take for the ball to hit the ground. I'm thinking the equation you want is s = ut + 0.5at^2 where s is the distance to the ground, u is the initial vertical velocity of the ball, a is the acceleration due to gravity and t is time in seconds. You'll need to rearrange that to get t, which I kinda forget how (it's a lot easier to do on paper than in code) but hopefully someone can point us in that direction. I'm eating me dinner :)

Because it's a quadratic equation you ought to get two values, one of which will more than likely be negative, which would also tell you how long ago the ball bounced, so take the positive one.

Come to think of it, I have this baby that I took from a site somewhere but heaven knows if it actually works, that was quite a while ago

Rem
	bbdoc: Solve a quadratic equation
	about: Solves a quadratic equation of the form ax^2 + bx + c = 0
	returns: A floating point array with two elements
End Rem
Function fry_Solve_Quadratic:Float[](a:Float, b:Float, c:Float)

	Local root:Float[2]

	'calculate D
	Local D:Double = (b*b) - (4.0 * a * c)
	
	If D = 0 Then
		'one root:
		root[0] = (b * -1.0) / (2.0 * a)
		root[1] = root[0]
	Else If D > 0 Then
		'two roots
		root[0] = ((b * -1.0) + Sqr(D)) / (2.0 * a)
		root[1] = ((b * -1.0) - Sqr(D)) / (2.0 * a)
	Else
		'complex roots. Oh man!
		DebugLog "Complex Roots. Oh man!"
		DebugLog "D: "+D
		DebugLog "Values given are A="+a+" B="+b+" C="+c
		root = [0.0, 0.0]
	End If
	
	Return root

End Function


Sorry about the rather unhelpful comments. Really sorry, actually, I'm not sure exactly how or if this works either. I do recall though that the calculation of "D" is some helpful mathematical function that tells you if there's one root, two roots, or if they're complex.

With t, you can easily calculate the x and y locations of the bounce using the starting coordinates and respective components of the velocity.

Friction poses problems. How complex are you planning it? The simplest method sees friction as a constant and you just ensure that's in all your calculations, making allowances for things like stationary balls (you don't want friction to make the ball shoot into the air). If it's more accurately modelled, you'll be looking at finding out the effective coefficient of friction of the air and the ball, and it makes the calculation more complex. Exactly how much more complex, I can't say right now. It's been seven years since my A-Level Maths, every time I go back to it I have to re-learn everything.


Grey Alien(Posted 2007) [#5]
I used that Wiki page. It was bitchin' as I studied physics 14 years ago!


siread(Posted 2007) [#6]
Thanks for the detailed explanation Fry. However, I'm not much wiser. C in GCSE, me. :P


Fry Crayola(Posted 2007) [#7]
Ok. Again ignoring air resistance for now, you've got a moving ball with a velocity. That velocity, V, has three components for the three axes - X, Y and Z. You know where the ball is, it has its own current 3D coordinates, we'll notify them in lower case.

I'll assume that x and y are the 2D coordinates of the pitch, and z is height.

First, you want to find out how long it takes for the ball to hit the ground, so you need to get the vertical component of the velocity. If you only have one velocity value, you'll need to work out the angle between the ground and the path of the ball at this exact moment and then use Z = sin(angle) * V.

With Z, and also z (the height of the ball above the ground) we can work out the time taken using the above function. The equation we're interested in is:

z = (Z * time) + (0.5 * -9.81 * (time ^2))

-9.81 being the acceleration due gravity. The convention here is usually that a positive velocity indicates the ball is going up, so this would be negative.

We rearrange this equation to form the quadratic equation that equals zero in order to plug values into the function. This gives:

-4.905 * (time ^ 2) + Z * time - z = 0

The form of a quadratic equation is ax^2 + bx + c = 0, where x is the unknown value you want to find out (time in this case), and a, b and c are constants. Taken from our reworked equation, a = -4.905, b = Z and c = -z, and we know what Z and z are.

When you plug those three values into the function you'll get an array of floats in return, with two elements. Both of these are correct values - if you substituted them for the time value in the earlier equation, the result would be zero, as expected. One of these is the answer that you want. If one is negative which it almost certainly will be, then obviously you've got a negative time result, telling you when the ball last hit the ground. A positive value will be the time it will next hit. If you get zero, the ball's already on the ground. I'm pretty sure you'll never get two negatives or two positives, a ball falling with constant acceleration can never reach a coordinate twice unless it started underground. If you want though, you can always check both and take the lowest non-negative.

Now we have time, t. We can use this to find out the coordinates the ball will hit the ground, which is what we wanted in the first place. Because we've not got air resistance yes, the ball is moving with constant velocity throughout. We need to calculate X and Y, the components of the velocity along the x and y axes.

Just as we calculated Z earlier using the angle of the ball from the ground, we now use a similar equation to calculate the horizontal component, using the same angle.

horizontal component = cos(angle) * velocity

This in itself can't be used, but that can be broken up into the X and Y components using the angle the ball's travelling on that 2D plane.

X = cos(angle) * horizontal component
Y = sin(angle) * vertical component


With these velocities, it's a simple matter of multiplying them by the time value we have, and then adding them to the current coordinates of the ball. That's where it will bounce.


Getting the angles in 3D space can be iffy, I've not much experience of it. What I'm doing at the moment is storing the angles whenever I apply any force to the ball, that way I don't need to try and calculate them. I know what direction a player is trying to kick it and at what angle he's trying to lift it off the ground. Every time you apply the force, be it a bounce, a shot, a rebound or whatnot, you can then calculate where it will bounce, store that, and just refer to that every time you need it until the ball is affected again.

Bringing air resistance into play is a tricky one and depends on how you want to model it. If it's just a constant force, then the ball will be affected by acceleration on every axis. Genuine resistance would, by definition, exert a stronger force the faster the ball travels. And then you might also have the spin of the ball to worry about...

Of course, if any of the above is wrong, sorry about that and hopefully some kind soul will correct me. And some of it you probably know already but I figured it's best not to leave anything out.


siread(Posted 2007) [#8]
Thanks for this Fry. Finally getting my head stuck into it. :)