Sign in to follow this  

Camera problems(UpVector)

This topic is 4074 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 guys i am making a car game in DX9 (c++)and am having some troubles with my camera, when ever i set the camera to 1st or 3rd person view the camera tends to tilt (rotate to show the car upside down) when i move the car left or right, i think it is because the up vector is not right, can someone give me a hand or some advise? the full code can be downloaded at: http://homepage.ntlworld.com/j.power40/html/zipfiles/3DCarvol2.zip car.cpp
CCar::CCar(void)
{
	
}
CCar::~CCar(void)
{
 
}
bool CCar::create(LPDIRECT3DDEVICE9 device)
{
	//Setup the cars physics 1st
	car_initPhysics();
	Model = new CModel();
	// Create a color for the text - in this case Blue
	d3dBlue1 = D3DCOLOR_ARGB(255,0,0,255);
	return Model->loadModel(device, "./models/lamboChas.x");
}
void CCar::car_initPhysics(void)
{
	accelerationVector.x = 0.0f;
	accelerationVector.y = 0.0f;
	accelerationVector.z = 0.0f;	

	VelocityVector.x = 0.0f;
	VelocityVector.y = 0.0f;
	VelocityVector.z = 0.0f;

	directionVector.x = 0.0f;
	directionVector.y = 0.0f;
	directionVector.z = 1.0f;
	
	referenceVector.x = 0.0f;
	referenceVector.y = 0.0f;
	referenceVector.z = 1.0f;
	
	ACCELERATION_RATE	=  5.0;
	BRAKING_RATE		= -20.0;
	FRICTION_RATE		= -1.0;
	STOP_RATE			=  0.0;
	STEERING_RATE		=  5.0;
	MAX_SPEED			=  0.5f;
}
void CCar::car_setPosition(D3DXVECTOR3 newPosition)
{
	positionVector = newPosition;
}

void CCar::getPosition(D3DXVECTOR3 &carPosition) 
{
	carPosition = positionVector;
} 

D3DXVECTOR3 CCar::car_getUpDirection() 
{
	return directionVector;
} 

void CCar::getCarDirection(D3DXVECTOR3 &carDirection) 
{
	D3DXVECTOR3 tempCarDirection;
	D3DXVec3Normalize(& tempCarDirection, & directionVector);	
	carDirection = tempCarDirection;
}

void CCar::car_updatePhysics(float dtime) 
{ 
	VelocityVector = VelocityVector + accelerationVector * dtime; /*Update the VelocityVector*/
	positionVector = positionVector + VelocityVector;/*Update the positionVector*/
	/*Get the magnitude of the cars velocity*/
	magnitude = (sqrt)((VelocityVector.x * VelocityVector.x) + 
		(VelocityVector.y * VelocityVector.y) + 
		(VelocityVector.z * VelocityVector.z));
	
	speed = magnitude;/*speed = magnitude of the cars vectors*/

	/*Update the angle for the car to turn by getting the dot 
	product between the direction and a reference in radians, and then clamp it to stop floating point errors*/
	tempDot = D3DXVec3Dot(&directionVector,&referenceVector);
	if (tempDot < -1){
		tempDot = -1;}
	else if (tempDot > 1){
		tempDot = 1;}

	fDot = acos(tempDot);

	/* check if the directionVector has a -ve X, which means it is pointing towards -ve X
	 in this case, the angle should be 360 - angle,360 in radians is 2*pi */
	if(directionVector.x < 0) 
	{
		fDot = 2*D3DX_PI - fDot;
	}

	car_updateCar();
}

void CCar::car_updateCar()
{
	D3DXVECTOR3 directionVectorTemp;

	if (bAccelerationKeyPressed == true)/*ACCELERATE THE CAR*/
	{
		if (speed <= MAX_SPEED) //Cap the speed
			{	
				accelerationVector = directionVector * ACCELERATION_RATE;
			}
	}
	if (bLeftKeyPressed == true)/*TURN CAR LEFT*/
	{
		if (speed >= 0.5f)/*only rotate if speed is above*/
		{
			directionVectorTemp.x = directionVector.x;
			directionVectorTemp.z = directionVector.z;
			//Rotate  the direction vector by an angle
			directionVector.x = directionVectorTemp.x*cos(D3DXToRadian(STEERING_RATE))
				- directionVectorTemp.z*sin(D3DXToRadian(STEERING_RATE));
			directionVector.z = directionVectorTemp.x*sin(D3DXToRadian(STEERING_RATE))
				+ directionVectorTemp.z*cos(D3DXToRadian(STEERING_RATE));
		}
	}
	if (bRightKeyPressed == true)/*TURN CAR LEFT*/
	{
		if (speed >= 0.5f)/*only rotate if speed is above*/
		{
			directionVectorTemp.x = directionVector.x;
			directionVectorTemp.z = directionVector.z;
			//Rotate the direction vector by an angle
			directionVector.x = directionVectorTemp.x*cos(D3DXToRadian(- STEERING_RATE))
				- directionVectorTemp.z*sin(D3DXToRadian(- STEERING_RATE));
			directionVector.z = directionVectorTemp.x*sin(D3DXToRadian(- STEERING_RATE))
				+ directionVectorTemp.z*cos(D3DXToRadian(- STEERING_RATE));
		}
	}
	if (bBreakingKeyPressed == true)/*BRAKE THE CAR*/
	{		
		accelerationVector = directionVector * BRAKING_RATE;
	}

	if (bAccelerationKeyPressed == false && speed != 0)/*Set the friction rate on the car*/
	{
		accelerationVector = directionVector * FRICTION_RATE;
	}

	VelocityVector = directionVector * speed;
}

void CCar::render(LPDIRECT3DDEVICE9 device)
{

	// Display the Speed
	RECT rSpeed;
	rSpeed.top = 100;
	rSpeed.bottom = 600;
	rSpeed.left = 5;
	rSpeed.right = 640;
	std::stringstream ss; ss <<"Speed "<<  speed/100;
	dxMgr.pFont->DrawText(NULL, ss.str().c_str(), -1, &rSpeed, DT_TOP | DT_LEFT, d3dBlue1);

	D3DXMATRIX transMatrix;				// the translation matrix
	D3DXMATRIX rotMatrix;				// the rotation matrix
	D3DXMATRIX scaleMatrix;				// the scale matrix

	// create the translation matrix
	D3DXMatrixTranslation(&transMatrix, positionVector.x, positionVector.y, positionVector.z);
	
	// create the rotation matrix for the object
	//D3DXMatrixRotationY(&rotMatrix,D3DXToRadian(rotation));
	D3DXMatrixRotationY(&rotMatrix,fDot);
	
	// Scale the Car by the size amount
	D3DXMatrixScaling(&scaleMatrix, size, size, size);

	// Multiply the translation matrix by the rotation matrix
	// The resulting matrix is stored in the transMatrix
	D3DXMatrixMultiply(&transMatrix, &rotMatrix, &transMatrix);

	// Multiply the translation matrix by the scale
	D3DXMatrixMultiply(&transMatrix, &scaleMatrix, &transMatrix);

	// Transform the object into world space
	device->SetTransform(D3DTS_WORLD, &transMatrix);
	
	// render the model for this car
	Model->render(device);
}
void CCar::car_setSize(float carSize)
{
	size = carSize;
}




game.cpp
if ( TheCamera.getCameraType() == Camera::THIRD_PERSON_VIEW ) 
	{
		// Update the view matrix representing the cameras 
		// New position/orientation.	
		D3DXVECTOR3 vCameraDirection, vCameraPosition, vCameraTranslation;

		// Get the orientation of the camera
		pCar->getCarDirection(vCameraDirection);
		vCameraDirection.y = -0.01f;
		TheCamera.setLook(&vCameraDirection);

		// Get the position of the camera
		pCar->getPosition(vCameraPosition); 
		vCameraTranslation = -pCar->car_getUpDirection();
		vCameraTranslation += -vCameraDirection * 2.5f;
		D3DXVec3Normalize(&vCameraTranslation, &vCameraTranslation);
		vCameraPosition.z += vCameraTranslation.z * 100.0f;
		vCameraPosition.x += vCameraTranslation.x * 100.0f;
		vCameraPosition.y += vCameraTranslation.y * 2000.0f;
		TheCamera.setPosition(&vCameraPosition);		
	}
	if ( TheCamera.getCameraType() == Camera::FIRST_PERSON_VIEW ) 
	{
		// Update the view matrix representing the cameras 
		// New position/orientation.	
		D3DXVECTOR3 vCameraDirection, vCameraPosition, vCameraTranslation;

		// Get the orientation of the camera
		pCar->getCarDirection(vCameraDirection);
		vCameraDirection.y = -0.01f;
		TheCamera.setLook(&vCameraDirection);

		// Get the position of the camera
		pCar->getPosition(vCameraPosition);
		vCameraTranslation = -pCar->car_getUpDirection();
		vCameraTranslation += -vCameraDirection * 2.5f;
		D3DXVec3Normalize(&vCameraTranslation, &vCameraTranslation);
		vCameraPosition.y += vCameraTranslation.y * 450.0f;
		TheCamera.setPosition(&vCameraPosition);		
	}
	TheCamera.getViewMatrix(&mCurrentView);
	dxMgr.pd3dDevice->SetTransform(D3DTS_VIEW, &mCurrentView);




[Edited by - Prog101 on October 20, 2006 4:17:34 PM]

Share this post


Link to post
Share on other sites

This topic is 4074 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this