PDA

View Full Version : Object throughing dynamics


fido_glc
02-22-2009, 11:44 PM
Hello.

What I want to code is the throughing of an object in the air: an object (movieclip) is selected with the mouse and then dragged to another position (new y < initial y) and then the object is let go to fall to the ground (initial y).

The movement of the object needs to look realistic. When it is let go, it has to go to the ground by following a curved line defined by the drag distance and drag speed.

I can figure out the x and y values of the start and stop drag and the time of the drag but I cannot figure out the code to define the curved movement of the object after it is let go.

Please check the attached image for an example.

Any help with the code or links to a tutorial would be appreciated.


Please let me know if I am clear.


Thank you for your time.

kkbbcute
02-23-2009, 03:20 AM
Creating a curve is really easy, and unless you want a hyper realistic simulation, you can just cheat by adding a gravity variable to the velocity of your ball falling every frame resulting in what seems to be a curve.

bluemagica
02-23-2009, 04:26 AM
hmm...going by your diagram, you are planning to retain some motion from during the drag, that is kind of hard and easy at the same time! But for that, you gotto forget about frames and rely on timers. depending on the initial x,y , the new x,y, and the time taken, you will have to calculate speed (speed = distance/time hence the timer class)! the only trick is getting the actual final coordinates where user truly stops dragging( think the user drags in zig-zag, will the speed and direction be affected?)

fido_glc
02-23-2009, 06:40 AM
hmm...going by your diagram, you are planning to retain some motion from during the drag, that is kind of hard and easy at the same time! But for that, you gotto forget about frames and rely on timers. depending on the initial x,y , the new x,y, and the time taken, you will have to calculate speed (speed = distance/time hence the timer class)! the only trick is getting the actual final coordinates where user truly stops dragging( think the user drags in zig-zag, will the speed and direction be affected?)

About the zig-zag, it will not matter. I am calculating the direction by a straight line defined by the points (initial x, initial y) and (final x, final y), not by the path in which the ball was dragged.

bluemagica
02-23-2009, 09:42 AM
thats what i meant! In a "real" situation, each time the ball changes its path(zig-zag) it will lose some momentum, so the way it is being dragged has significance during calculation of speed and direction!

newblack
02-23-2009, 04:14 PM
so the difference between your end drag and start drag positions, divided by duration of drag, is your muzzle velocity.

v0 = ( end - start ) / dt

you'll need to define a gravity vector (most likely pointing straight down)

g = ( 0, 9.8 * pixelPerMeterRatio )

where pixelPerMeterRatio maps meters to pixels

now you have all of the elements to express your MovieClip's position through the equations of motion:


x(0) = end

x(t) = x(0) + v(0) * t + g * t * t * 0.5


so for each frame of your animation, determine how much time has elapsed since the user stopped dragging; this is t. plug t into the equations of motion to solve for your MovieClip's position at that time.

remember that the above are vector equations, where x, v and g are all vectors (x and y components).