Software Design
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreSoftware Design

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 1st, 2002, 01:15 PM
TotalMonkey TotalMonkey is offline
Polynesian Monkey God
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2001
Location: Monkeytown, USA
Posts: 57 TotalMonkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 47 m 42 sec
Reputation Power: 7
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

Reply With Quote
  #2  
Old March 2nd, 2002, 12:04 PM
WoR WoR is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Posts: 23 WoR User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
  #3  
Old March 3rd, 2002, 02:21 PM
TotalMonkey TotalMonkey is offline
Polynesian Monkey God
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2001
Location: Monkeytown, USA
Posts: 57 TotalMonkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 47 m 42 sec
Reputation Power: 7
Thanks!

That's exactly what I was looking for!

Thanks a bunch,
TotalMonkey!

Reply With Quote
  #4  
Old March 13th, 2002, 08:43 PM
The Ostrich The Ostrich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Location: Oxford, UK
Posts: 31 The Ostrich User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Yeah. A classic Engineering mistake - friction cannot be divided into independent components.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreSoftware Design > Need help with my frictional physics


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


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





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway