# Component Velocities [solved]

This topic is 4861 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, im busy making a micro-machine style game and am a little confused on getting the velocity in both Y and X directions when the car is travelling at an angle... The car is made up of a series of Seven Sprites, pointing at certain angles... the current layout is S, SW, W, NW, N, NE, E, SE When a directional arrow (Left or right) is pressed then this angle is changed in the correct manner.. however my code for working out the velocity only sork correctly when the sprite is moving in the positive (S, SE, SW) direction, and whenever I try for the Negative Y-Direction it fails... I understand you have to perform the same calculation for the X Values just with Sin instead, but could you point me as to what I would need to do to get it to function correctly?
// move forward
if(pTheInputEngine->KeyPressed(SDLK_UP))
{
// get correct angle (from the N direction is this the
// correct way to do it?!)
switch(spriteNumber)
{
case 0:
angle = 180;
break;
case 1:
angle = 135;
break;
case 2:
angle = 90;
break;
case 3:
angle = 45;
break;
case 4:
angle = 0;
break;
case 5:
angle = 45;
break;
case 6:
angle = 90;
break;
case 7:
angle = 135;
break;
default:
break;
}
// add accelleration (i cant spell ;))
YVelocity += (Accelleration * (pTheTimerEngine->GetFrameTime()/1000));

//Calculate Y-component of velocity...
YVelocity = - (YVelocity * cos(angle*PI/180));

}


Following this the YLocation is updated with
YLocation += YVelocity;
worldYLocation += YVelocity;


I have left my friction calculation out until I get this sorted... Thanks in advance, Cyph [Edited by - cyph on July 25, 2005 7:44:04 AM]

##### Share on other sites
Well for a start, your angles only go from 0 to 180. There are 360 degrees in a full circle.

Try using the following angles in your switch statement

180
135
90
45
0
315
270
225

##### Share on other sites
Your title is wrong. This is not angular velocity, which is the rate at which an object is rotating.

Component velocities would be a more appropriate title.

##### Share on other sites
Well i got it working, had to include an extra if statement because when I was adding the accelleration to the negative movement it was cancelling out the movement instead of adding to it...

	// move forward	if(pTheInputEngine->KeyPressed(SDLK_UP))	{	switch(spriteNumber)		{		case 0:					angle = 180;			break;		case 1:			angle = 135;			break;		case 2:			angle = 90;			break;		case 3:			angle = 45;			break;		case 4:			angle = 0;			break;		case 5:			angle = 315;			break;		case 6:			angle = 270;			break;		case 7:			angle = 225;			break;		default:			break;		}	if(angle<90 || angle >270)	{	YVelocity -= (Accelleration * (pTheTimerEngine->GetFrameTime()/1000));	YVelocity =  (YVelocity * cos(angle*PI/180));	}	else	{	YVelocity += (Accelleration * (pTheTimerEngine->GetFrameTime()/1000));	YVelocity = - (YVelocity * cos(angle*PI/180));	}

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 13
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633322
• Total Posts
3011360
• ### Who's Online (See full list)

There are no registered users currently online

×