Jump to content
  • Advertisement
Sign in to follow this  

wondering if this is the right way to align the Yup to a direction

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


At it again just thought I would try my hand at aligning rotation about  some axis based on a direction.


I create a plane at right angles to the direction vector facing it then I pick 3 of the points to obtain the axis between the two normals.


works ok but if I set the Position to the targets my mesh stops rendering or it vanished.


what sort of rotation is it, to do that.


here it the function any tips would be nice thanks.

//this will return a rotation matrix that will align a mesh that was created with y up to
//a direction aligns the meshes y with the direction
D3DXMATRIX AlignYupToDirection(D3DXVECTOR3 &pos, D3DXVECTOR3 &target)
	//holds our plane corrners
	D3DXVECTOR3 points[4];

	//we need to get the direction we are heading
	D3DXVECTOR3 dir = target - pos;

	D3DXVec3Normalize(&dir, &dir);

	//create a up vector
	D3DXVECTOR3 up = findPerpendicularIn3d(dir);

	float halfsize = 60.0;//any size may be larger

	D3DXVECTOR3 crossdirup;
	D3DXVECTOR3 crosso1dir;

	D3DXVec3Cross(&crossdirup, &dir, &up);


	D3DXVec3Normalize(&o1, &crossdirup);

	D3DXVec3Cross(&crosso1dir, &o1, &dir);

	D3DXVec3Normalize(&o2, &crosso1dir);

	//offset them
	o1 *= halfsize;
	o2 *= halfsize;

	//this is our plane at our position facing the direction we want
	//we only need 3 but not sure which ones yet
	points[0] = pos + o1 + o2;
	points[1] = pos + o1 - o2;
	points[2] = pos - o1 - o2;
	points[3] = pos - o1 + o2;

	//now using the plane we can create a axis to rotate about
	//we now need to use 3 points from the plane 
	D3DXVECTOR3 u , v, n, objn;
	int a = 0, b =1, c = 2;

	objn = D3DXVECTOR3(0.0, 1.0,0.0);//up


	//First convert the three points into two vectors
	u = points[b] - points[a];
	v = points[c] - points[a];
	//Step 2
	//Find the cross product of the vectors
	D3DXVec3Cross(&n, &v, &u);
	D3DXVec3Normalize(&n, &n);
	//step 3
	// Obtain axis between two normals, it will be the rotation axis
	D3DXVECTOR3 axis; 
	D3DXVec3Normalize(&objn, &objn);
	D3DXVec3Cross(&axis, &objn, &n);

	// Calc angle between normals
	D3DXVec3Normalize(&axis, &axis);

	FLOAT angle;
	float dot = D3DXVec3Dot(&objn, &n);
	Clamp(dot, - 1.0f, 1.0f ); 
	angle = acos(dot);

	// Create a quaternion with the rotation difference between normals
	//m_Inclination = Quaternion.CreateFromAxisAngle(axis, angle);


	return r;//done


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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!