Jump to content
  • Advertisement
Sign in to follow this  
cyph

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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

You could always drop your trig and put your velocities straight into your case statement.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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));
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!