
March 1st, 2002, 01:15 PM
|
|
Polynesian Monkey God
|
|
Join Date: Nov 2001
Location: Monkeytown, USA
Posts: 57
Time spent in forums: 1 h 28 m 46 sec
Reputation Power: 12
|
|
|
Need help with my frictional physics
I'm having problem with creating a "loss of momentum due to friction" routine. I'm using Flash 5 actionscript, but the code is relatively portable in concept. Sorry, for showing the whole script, but I thought it was necessary to see what I'm trying to go for here (which is meant to be a 2-d representation of "true-physics") The "main loop" is the the "onClipEvent (enterFrame)" section, and the "friction" construct is in bold:
Code:
onClipEvent (load) {
// initialize variables
fcount = 0;
x_momentum = 0;
y_momentum = 0;
// trigonometric function for movement
function move (distance) {
angle = (_rotation/360)*2*Math.PI;
x_momentum += distance*Math.sin(angle);
y_momentum += -distance*Math.cos(angle);
}
}
onClipEvent (enterFrame) {
// increment frame counter
fcount++;
// keypress detection
if (Key.isDown(Key.UP)) {
move(1);
}
if (Key.isDown(Key.DOWN)) {
move(-1);
}
if (Key.isDown(Key.LEFT)) {
_rotation -= 5;
}
if (Key.isDown(Key.RIGHT)) {
_rotation += 5;
}
// movement due to momentum
_x += x_momentum;
_y += y_momentum;
// set borders to bounce
if (_y<20 or _y>380) {
// undo y-momentum from y-postion
_y -= y_momentum;
// reverse y-momentum
y_momentum *= -1;
}
if (_x<20 or _x>480) {
// undo x-momentum from x-postion
_x -= x_momentum;
// reverse x-momentum
x_momentum *= -1;
}
// apply friction when frame count is max (every 10th frame)
if (fcount >=10) {
// x-component friction:
// if x-momentum is positive, decrease it by a 0.01
if (x_momentum>0) {
x_momentum -= 0.01;
// if the x-momentum is "small", make it equal zero
if (x_momentum<0.001) {
x_momentum = 0;
}
// if x-momentum is negative, increase it by a 0.01
} else if (x_momentum<0) {
// if the x-momentum is "small", make it equal zero
x_momentum += 0.01;
if (x_momentum<0.001) {
x_momentum = 0;
}
}
// y-component friction:
// if y-momentum is positive, decrease it by a 0.01
if (y_momentum>0) {
y_momentum -= 0.01;
// if the y-momentum is "small", make it equal zero
if (y_momentum<0.001) {
y_momentum = 0;
}
// if y-momentum is negative, increase it by a 0.01
} else if (y_momentum<0) {
y_momentum += 0.01;
// if the y-momentum is "small", make it equal zero
if (y_momentum<0.001) {
y_momentum = 0;
}
}
}
// reset the frame count when max is reaches
if (fcount >=10) {
fcount = 0;
}
}
Anyhow, what's happening is that the two momentum components are dropping to zero really quick, so I'm guessing that it's just a 3am logic problem. Any suggestions on how I should actually construct this part?
__________________
"We're all - monkeys!" -- Jeffrey Goines, 12 Monkeys
|