Skip to main content
GameDev.net gamedev.net
🔒 Locked

Component Velocities [solved]

Started by cyph Jul 25, 2005 at 5:50 AM 2 replies 1.2k views
Original Post
cyph
cyph
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]
cyph
cyph
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));	}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.