Jump to content
  • Advertisement
Sign in to follow this  

Error rotating object along its path

This topic is 4339 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. There are many topics like that on net but none has helped me with this problem. I have three points: p1-source position p2-current object position (interpolated between p1 and p3) p3-destination point I need to know the angle of rotation(absolute) that should be applied to the object so i'll always look in its moving direction. Here's what i've got but somewhere i went wrong. Please help.
float GetAngle2D( const D3DXVECTOR3* p1, const D3DXVECTOR3* p2, const D3DXVECTOR3* p3 )

	D3DXVECTOR2 v1, v2, v3;
	v1 = D3DXVECTOR2( p1->x, p1->z );
	v2 = D3DXVECTOR2( p2->x, p2->z );
	v3 = D3DXVECTOR2( p3->x, p3->z );

	v1 = v3 - v2;
	v2 = v1 - v3;

	D3DXVec2Normalize( &v1, &v1 );
	D3DXVec2Normalize( &v2, &v2 );
	float fAngle = atan2f( v2.x - v1.x, v2.y - v1.y );

	return fAngle;


And that's how i call it (just in case).
D3DXVec3CatmullRom( &vp, &p[0], &p[1], &p[2], &p[3], m_fStep );
float fA = NMATH::GetAngle2D( &p[1], &vp, &p[2] );

m_pCam->SetPosition( &vp );
m_pCam->SetRotation( 0.0f, fA, 0.0f );

Share this post

Link to post
Share on other sites
Personnaly, I would use the derivative of the catmul-rom spline to get the direction. Or, store that value for the control points, and interpolate (linearly or otherwise). It's easy to derive, but I'm not 100% sure the catmull-rom is continuous on the first derivative.

also, check out


Share this post

Link to post
Share on other sites
Yes, this sample looks like what i need but it uses directx framework which makes it not too clear :(

Share this post

Link to post
Share on other sites
in any case, your method looks wrong. if all you want is the direction the spline is going at a particular point, then use the second derivative.

q(t) = 0.5 *((2 * P1) + (-P0 + P2) * t +
(2*P0 - 5*P1 + 4*P2 - P3) * t^2 +
(-P0 + 3*P1- 3*P2 + P3) * t^3));

derive and you should get

q'(t) = 0.5 *((2 * P1) + (-P0 + P2) +
(2*P0 - 5*P1 + 4*P2 - P3) * 2 * t +
(-P0 + 3*P1- 3*P2 + P3) * 3 * t^2));

the vector q'(t) should give you the look direction for your camera.

Equally, derive again and you get

q'''(t) = 0.5 *((2*P0 - 5*P1 + 4*P2 - P3) * 2 +
(-P0 + 3*P1- 3*P2 + P3) * 6 * t));

q'''(t) is the 'acceleration' of the spline at a point, or if you will, for a road surface, will give you the natural 'banking' or curvature of the spline at a point.

Again, I am not sure about the continuity of the curve at the first and second derivative, so you may find 'breaks'. Let me check on the wiki :)

EDIT : Ah, it's not C2 continous, but it is C1 continuous, so using the first derivative for the direction of your camera will be fine.

I would then recommend calculating the second derivative at the control points, and lineraly interpolate along that that section to get the curvature of the spline (or if you want, the natural 'up' vector of your camera).

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!