The problem may not be with the formulas used or the code.
The problem I think is with the iterations, during which the ball bouncing height slightly increases with each bounce.
Hope some FLASH expert can give a better explanation, and suggest a solution.
i have this working in a 3 frame movie, running at 30FPS
movieclip instance - ball - across all 3 frames
button instance - start_btn - across all 3 frames
button instance - reset_btn - across all 3 frames
Code:
// frame#1 -
var t:Number = 0;
var d:Number = 0.6;
var g:Number = 9.8; // Acceleration of gravity
var e:Number = 0.1; // Energy absorption
var y:Number = ball._y;
var gr:Number = 300; // ground
btn_start.onRelease = function() {
gotoAndPlay(2);
};
btn_reset.onRelease = function() {
y = ball._y = 70;
gotoAndStop(1);
};
stop();
Code:
// frame#2 -
ball._y = Math.min(y+(g/2)*t*t, gr+2);
if (ball._y >= gr) {
d = -d;
y += e*(gr+2-y);
t = Math.sqrt((gr-y)*2/g);
if (y >= gr) {
gotoAndStop(1);
}
}
Code:
// frame#3 -
t += d;
gotoAndPlay(2);
can you make use of the logic and apply to your onClipEvent method ?
mx2004 fla
Last edited by a_modified_dog; 02-20-2007 at 04:54 PM.
Reason: fla file version added
I'm not really understanding what's happening. You're setting the ball's position to be the minimum of what it would be according to free fall or the height of the ground + 2. That makes sense except for the +2. Then if the ball hits the ground you're setting d=-d, i'm assuming d's the velocity, and y becomes it self +some energy times what you have. I don't understand what you're doing with time with sqrts or adding d. If you could, could you look at my code and see what I'm doing thats not right?
// frame#2 -
ball._y = Math.min(y+(g/2)*t*t, gr+2);
if (ball._y >= gr) {
d = -d;
y += e*(gr+2-y);
t = Math.sqrt((gr-y)*2/g);
if (y >= gr) {
gotoAndStop(1);
}
}
I'm going to attach my .fla so you guys can see what my code does. Also note that theres a problem where, if I place the ground at (0,460) ground._y will return 475 for some reason I think. See for yourself
Hey! This code seems to be working OK!
//__________________________________________________ _________
onClipEvent(load)
{
aY = 9.8;
t = 0;
v0 = 0;
v = 0;
ground=350;
yb=40;
yt=40;
this._y=yb;
}
onClipEvent(enterFrame)
{
trace(this._y);
if(this._y<yt){//Stops the ball moving to a height <yt.
yb=yt;
t=0;
v0=0;
}
Thanks, it works but its sloppy. The ball is forced to stop so it doesn't look real. Also, I'm still not sure why its telling me the ground is at 475 when its not.
When I click on the movieclip which is the ground, the property box shows Y: 460.0. I don't see 475.3 anywhere except for the console window that pops up
In scene 1, Layer 1, world
Properties:
W:500.0 X: 235.6
H: 40.0 Y: 475.3
__________________________
I have again checked up.
The above data is indeed shown!
I think i see where the problem lies. If you watch the animation, the ball doesn't hit the ground at the same spot everytime so therefore there will be times when it will fall a greater distance than others. I'm not exactly sure though how to correct that. The ball is accelerating so its distance is not linearly increasing
Thanks, it works but it’s sloppy. The ball is forced to stop so it doesn't look real. Also, I'm still not sure why its telling me the ground is at 475 when its not.
This is my analysis:
When we make a program combining physics and animation I think, we should remember that during animation the movement is controlled by iterations of time (t), which gets added or subtracted during each iteration. Since the computer uses binary method of calculation, this sometimes leads to errors due to lose of significant digits.
_______________________________
//See the following example:
var a=1.23456E20;
var b=1E5;
var c;
c=a-b;
trace(c);
_______________________________
The trace statement shows 1.23456E20, which is obviously not correct!
This has happened because the computer stores only about 15 significant digits!
Anyhow this is what I think. Let other computer experts offer their comments.
Don't hate physics. Understand how the binary system works.
To overcome this problem to some extent, we have to fix the boundary conditions for the ball at the top, and bottom positions.