Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
March 1st, 2002, 02:15 PM
 TotalMonkey
Polynesian Monkey God

Join Date: Nov 2001
Location: Monkeytown, USA
Posts: 57
Time spent in forums: 1 h 28 m 46 sec
Reputation Power: 13
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:
// 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

#2
March 2nd, 2002, 01:04 PM
 WoR
Registered User

Join Date: Mar 2002
Posts: 20
Time spent in forums: < 1 sec
Reputation Power: 0

You reduce the absolute value 0.01 instead of
x_momentum -= x_momentum*0.01;
or (allmost correct):
x_momentum *= 0.99;

good luck

#3
March 3rd, 2002, 03:21 PM
 TotalMonkey
Polynesian Monkey God

Join Date: Nov 2001
Location: Monkeytown, USA
Posts: 57
Time spent in forums: 1 h 28 m 46 sec
Reputation Power: 13
Thanks!

That's exactly what I was looking for!

Thanks a bunch,
TotalMonkey!

#4
March 13th, 2002, 09:43 PM
 The Ostrich
Contributing User

Join Date: Mar 2002
Location: Oxford, UK
Posts: 28
Time spent in forums: < 1 sec
Reputation Power: 0
Yeah. A classic Engineering mistake - friction cannot be divided into independent components.

 Viewing: Dev Shed Forums > Programming Languages - More > Software Design > Need help with my frictional physics