PDA

View Full Version : Math equation help, please


sstringer
06-27-2005, 08:10 PM
Hi all,

I am in pretty desperate need of some help with a simple math equation. I am working on an actionscript routine that handles a ball bouncing over words in a sentence as they are read aloud. The challenge is that I need the ball to bounce at specific beats in order to keep up with the words. The equation I need help with should be pretty simple for someone mathmatically inclined.

I have all of the physics worked out. The ball bounces and looks great. I just need to control the initial velocity. So, for simplicity's sake, let's take some shortcuts. 1) let's take all but the Y axis out of the equation. X-Velocity is fixed and is a solved problem. 2) gravity can be shorthanded as a decay rate applied each frame:

Vf = Vi * g

Where Vf is the final, or new, velocity for the current frame, Vi is the initial, or old, velocity from the previous frame, and g is the decay rate, or simplified gravity. This is pretty basic stuff.

The problem I'm having is applying this over time and in reverse in order to calculate the very first frame's initial velocity. I'm looking for a seed, or initial, velocity that, when applied in frame 1, will produce a final velocity of zero at time-x. For instance, let's say we want the ball to reach its zenith at frame 30 (1 second). What is the formula to calculate the initial velocity?

Vi = [???];

Where [???] produces equilibrium (velocity 0) after 30 frames. I assume it would be the sum of 30 increasingly decayed velocities, but I'm at a loss as to how I should calcualte those values. I'm also sure that distance over time plays a factor here, but I'm not sure how.

Any help would be most appreciated.

Many thanks,
Steve

Wino
06-27-2005, 08:19 PM
Won't the ball's y position be a parabolic function (even ignoring the X axis - assume you are looking on the parabola from the Z-axis)?

If that is the case, then the initial and terminal velocities will be the same, with the zero velocity reached in frame 15. What you need to do is determine how "high" you want the ball to go, then solve the parabola for the X-intercepts for that velocity.

I think this could be "fudged" with a sine wave (circular, not parabolic) with easier math.

Maybe I'm not understanding what you're trying to do. Is what I'm saying so far making sense?

sstringer
06-27-2005, 09:15 PM
Wino, thanks. Yes, this would be a parabolic function, but height is not the critical variable here. Perhaps calculating the height would give me what I need, but I don't know how to do that either.

Can you provide an example?

Wino
06-27-2005, 11:02 PM
I think "fudging" this with sines should be the easiest work-around. (IF!! I'm understanding what you want):

Sines go from zero to one to zero from 0 degrees to 90 degrees to 180 degrees. If you take the number of "frames" you need to affect (minus 1, as you need to start ast zero) and divide that into 3.14159 (Pi), you'll get 0 to Pi (which is zero to 180 degrees, but in radians, not degrees; Flash works in radians .... omitting an unnecessary step). Use the sine of the result of that as your height (Y-axis). That should give you a realistic-enough approximation.

I hope this makes sense to you. Let me know if you need more help (or if it answers what you need).

For example, you have 22 frames you need to affect. Divide Pi by 21. On frame 1 (minus 1, giving zero, both for radians and the sine of that value), you'll have zero value and zero sine. On frame 2, you'll get 0.3183... frame three is about a sine of .59, etc., giving you a slow buildup to 1 at frame 11 or so... going the same rate back down (slow at the top, faster on the sides near the descent nadir) as it went up.

alpineedge3
06-27-2005, 11:21 PM
for parabolic motion, where a =acceleration, y = distance

Vf^2-Vi^2 = 2ay

so Vi = sqrt(Vf^2-2ay)

also

y = (1/2)*at^2 + Vo*t

in kinematics there are 5 variables(Vf, Vi, a, t, distance). each equation uses 4 variables; you need to know 3 of them to solve for the 4th. you need to break the velocity up into component vectors. without getting into too much physics:

distance = velocity*time
x_distance = Vi*cos(angle)*t

the cos is there because part of the initial velocity is used to propel the ball in the y direction, and part of it is used for the x direction.

so solving for Vi:
Vi = x_distance/(t*cos(angle))

you can use this Vi as one the variables in the equations for the y-direction.

pixelwit
06-28-2005, 12:54 AM
I don't really understand the problem as described but I'm not so sure that's very relevant so please forgive me if I'm not seeing the full depth and breadth of the situation. ;)

The problem as described here...newY = oldY * decay;...describes an equation which never reaches 0 therefore working backwards from 0 should prove pretty difficult shouldn't it? No non-zero number times any other non-zero number will yield a result of 0 will it? You could determine the point at which floating point errors would round the decayed number off to 0 then work your equation back from there or if you wanted to you could work back from the point at which Flash stops updating the screen to reflect changes in position (.05 pixels), or more likely you could select an arbitrary point which looks like 0 to you. When representing changes in position on the screen I tend to stick with a half pixel or so as a limit.

Hope that makes sense.

-PiXELWiT
http://www.pixelwit.com

sstringer
06-28-2005, 11:45 AM
And the lucky winner is...

alpineedge3 with his parabolic function:

Vi = sqrt(Vf^2-2ay)

To make things easier, since Vf is 0, the formula that seems to magically work in my applet is:

var Vf:Number;
var d:Number = 50; // distance in pixels fed to the function but hard-coded here for illustration
var g:Number = 5; // "gravity" or a decay force fed to the function but hard-coded here

Vf = Math.sqrt(2 * g * d);

Turns out it couldn't be easier. Many thanks to all for helping. I really appreciate the effort.

sstringer
06-29-2005, 02:22 PM
As a postscript to this thread, I found an excellent tutorial that walks you through all the components of basic projectile physics. This is a must-read for anyone who is starting to wrestle with motion in Flash.

http://www.physicsclassroom.com/Class/vectors/U3L2a.html

alpineedge3
06-29-2005, 11:28 PM
glad it worked