Jump to content
  • Advertisement
Sign in to follow this  
kobyH

how to calculate rotation of car along a path

This topic is 3686 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 gurus of gamedev :) I am coding a 3d city tour simulation with car going on fixed paths. I interpolate the car model using the Catmull-Rom algorithm but I have problems in calculating the rotation to add to the model in order to orient it to the direction of motion. here is what I currently do:
std::vector<C3dModel::TransformVector> CTrack::SetPosition(void)
{
	
	vector<C3dModel::TransformVector> tv;
	transientPoint = this->Interpolate(currentPoint,percentage);
	percentage+=0.01f*speed;
	if(percentage>1.0f) 
	{
		//currentPoint = (currentPoint==lastPoint) ? loopPoint : currentPoint+1;
		nextPoint = (nextPoint==lastPoint) ? loopPoint+1 : ++currentPoint+1;
		if(currentPoint==lastPoint)  currentPoint = loopPoint ;
		
		percentage = 0.f;
	}
	
	C3dModel::TransformVector v = {C3dModel::TRANSLATE, 0,transientPoint.x,transientPoint.y,transientPoint.z};
	tv.push_back(v);
	//printf("from:%d to %d: car position:(%3.3f,%3.3f,%3.3f)\n",currentPoint,currentPoint+1,transientPoint.x,transientPoint.y,transientPoint.z);
	float rotation = atan2f((wayPoints[nextPoint].z - transientPoint.z) , (wayPoints[nextPoint].x - transientPoint.x))*180.f/PI ;
	C3dModel::TransformVector r = {C3dModel::ROTATE, rotation, 0.f, 1.0f, 0.f};
	tv.push_back(r);
	printf("from:%d to %d: rotate car:(%3.3f)\n",currentPoint,nextPoint,rotation);
	return tv;
}

Any Answer will be greatly appreciated!

Share this post


Link to post
Share on other sites
Advertisement
the direction of of a point on the path is the first derivative of the catmull rom equation. That should give you the orientation of the car, or what it should be to stick to the path.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!