Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 07-07-2012, 04:05 AM   #1
colfaxrev
bit chez fr.digital house
 
colfaxrev's Avatar
 
Join Date: Mar 2006
Location: Los Angeles
Posts: 688
Default Math question

this is for physics in a game.

I am using box 2d engine so the physics is already handled.

please look at my attachment pic, it explains it quite simply

basically i need to split x velocity into x and y velocity when trying to walk up a slope, and for some reason i can't figure out the right formula

any help would be greatly appreciated!
Attached Thumbnails
Click image for larger version

Name:	slopeVelocityProblem.gif
Views:	31
Size:	17.6 KB
ID:	38596  
__________________
Music is life
colfaxrev is offline   Reply With Quote
Old 07-07-2012, 01:20 PM   #2
ASWC
Super Moderator
 
ASWC's Avatar
 
Join Date: Dec 2007
Location: Greenville, SC
Posts: 6,528
Default

Reduce the gravity or reduce the friction on both or apply a friction joint.
__________________
aswebcreations
Super Duper!
ASWC is offline   Reply With Quote
Old 07-08-2012, 05:40 AM   #3
colfaxrev
bit chez fr.digital house
 
colfaxrev's Avatar
 
Join Date: Mar 2006
Location: Los Angeles
Posts: 688
Default

reducing gravity isn't an option because the feeling of how jumps works depends on that, the friction must stay cause some blocks need to be icy and slick and others need to ahve more friction

im close to the solution though

if i have an x velocity of 10 and the hill is at a -45 degree angle then x velocity should be 5 and y velocity should be -5

if it were a 90 degree angle (a wall) it would be x velocity 0 and y velocity -10

obviously i won't make walls do that but thats the ratio

so its a ratio, just trying to figure out how to spell it out in a formula
__________________
Music is life
colfaxrev is offline   Reply With Quote
Old 07-08-2012, 06:02 AM   #4
santanoa
Dual Screen Empowered
 
santanoa's Avatar
 
Join Date: Jul 2010
Location: Israel
Posts: 258
Default

isn't it

vx = vcos(theta)
vy = vsin(theta)

where vx is your x velocity, vy is your y velocity and v is your absolute velocity or velocity you want to distribute over x and y. theta is your angle.

That's what I learned in Physics class
santanoa is offline   Reply With Quote
Old 07-08-2012, 07:18 AM   #5
colfaxrev
bit chez fr.digital house
 
colfaxrev's Avatar
 
Join Date: Mar 2006
Location: Los Angeles
Posts: 688
Default

thanks for that answer! I think that looks about right although i did just get this to work

Code:
private function _onMouseWheel(event:MouseEvent):void
{
	_floorSprite.rotation += event.delta;
	
	var radians:Number = deg2rad(_floorSprite.rotation);
	var radians90DegreeAngle:Number = deg2rad(90);
	
	var xVel:Number = 10;
	var yVel:Number = 0;
	
	trace('-----------------------');
	trace('_floorSprite.rotation[' + _floorSprite.rotation + '] radians[' + radians + ']');
	trace('b xVel[' + xVel + ']');
	trace('b yVel[' + yVel + ']');
	
	trace('transform to XY velocity');
	
	//var angleRatio:Number = -_floorSprite.rotation / 90;
	var angleRatio:Number = -radians / radians90DegreeAngle;
	
	trace('angleRatio[' + angleRatio + ']');
	
	if (angleRatio >= 0)
	{
		//trace('uphill');
		yVel = -((xVel * angleRatio));
		xVel = xVel + yVel;
	}
	else if (angleRatio < 0)
	{
		//trace('downhill');
		yVel = -((xVel * angleRatio));
		xVel = xVel - yVel;
	}
	
	trace('a xVel[' + xVel + ']');
	trace('a yVel[' + yVel + ']');
}
__________________
Music is life
colfaxrev is offline   Reply With Quote
Old 07-08-2012, 07:26 AM   #6
colfaxrev
bit chez fr.digital house
 
colfaxrev's Avatar
 
Join Date: Mar 2006
Location: Los Angeles
Posts: 688
Default

Quote:
Originally Posted by santanoa View Post
isn't it

vx = vcos(theta)
vy = vsin(theta)

where vx is your x velocity, vy is your y velocity and v is your absolute velocity or velocity you want to distribute over x and y. theta is your angle.

That's what I learned in Physics class
that seems close but it wasn't giving me the right values im sure if that formula was correct it would run a lot faster than mine though
__________________
Music is life
colfaxrev is offline   Reply With Quote
Old 07-08-2012, 07:31 AM   #7
colfaxrev
bit chez fr.digital house
 
colfaxrev's Avatar
 
Join Date: Mar 2006
Location: Los Angeles
Posts: 688
Default

Code:
private function _onMouseWheel(event:MouseEvent):void
{
	_floorSprite.rotation += event.delta;
	
	var velocity:Number = 10;
	var theta:Number = -deg2rad(_floorSprite.rotation);
	var vx:Number = velocity * Math.cos(theta);
	var vy:Number = velocity * Math.sin(theta);
	
	trace('vx[' + vx + ']');
	trace('vy[' + vy + ']');
}
these numbers aren't quite right, do i have that right?
__________________
Music is life
colfaxrev is offline   Reply With Quote
Old 07-08-2012, 07:36 AM   #8
santanoa
Dual Screen Empowered
 
santanoa's Avatar
 
Join Date: Jul 2010
Location: Israel
Posts: 258
Default

It seems like there might be a problem if you go through multiple 'hills' or angle changes because your yVel is dependent on your xVel and your xVel is dependent on your yVel.

Also, for a 45 degree angle you'd want both xVel and yVel to be the same, right? For your method:

If you first change to a 45 degree angle
angleRation = 0.5
xVel = 15
yVel = 5

While:

xVel = vcos(theta) ==> 7.07 = (10)cos(45)
yVel = vsin(theat) ==> 7.07 = (10)sin(45)


Hope that makes sense.

Good Luck
santanoa is offline   Reply With Quote
Old 07-08-2012, 07:39 AM   #9
santanoa
Dual Screen Empowered
 
santanoa's Avatar
 
Join Date: Jul 2010
Location: Israel
Posts: 258
Default

Sorry, I didn't see your response...

It looks good to me. What's wrong?
santanoa is offline   Reply With Quote
Old 07-08-2012, 12:14 PM   #10
ASWC
Super Moderator
 
ASWC's Avatar
 
Join Date: Dec 2007
Location: Greenville, SC
Posts: 6,528
Default

A friction joint applies friction to the object you register with it. All other object would keep their normal friction while the object(s) registered with the joint will get your special friction. Once out of the hill you just remove them from the joint and they'll go back to their normal friction values.
__________________
aswebcreations
Super Duper!
ASWC is offline   Reply With Quote
Reply


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:22 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.