# 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.

## 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 on other sites
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 on other sites
Thanks,
This puts me in the right direction!!

1. 1
2. 2
3. 3
Rutin
20
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633656
• Total Posts
3013186
×