#1
  1. No Profile Picture
    Polynesian Monkey God
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Location
    Monkeytown, USA
    Posts
    57
    Rep Power
    14

    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
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    20
    Rep Power
    0

    Smile


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

    good luck
  4. #3
  5. No Profile Picture
    Polynesian Monkey God
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Location
    Monkeytown, USA
    Posts
    57
    Rep Power
    14

    Thanks!


    That's exactly what I was looking for!

    Thanks a bunch,
    TotalMonkey!
    "We're all - monkeys!" -- Jeffrey Goines, 12 Monkeys
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Location
    Oxford, UK
    Posts
    28
    Rep Power
    0
    Yeah. A classic Engineering mistake - friction cannot be divided into independent components.

IMN logo majestic logo threadwatch logo seochat tools logo