• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

No replies to this topic

### #1ankhd  Members

Posted 30 August 2014 - 02:11 AM

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);

D3DXVECTOR3 o1;

D3DXVec3Normalize(&o1, &crossdirup);

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

D3DXVECTOR3 o2;
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);

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

return r;//done

}//end
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.