Hi all.
I think I have my align to terrain function working ok when I set the objects normal to (0.0 -1.0, 0.0).
Im not sure how to make my objects normal, it only has a vector3 for position and the meshes up is +y
I was woundering why I needed to set the objects normal to -1 and not 1 as 1 should be up.
Oh and whith the 3 wheels is there a way I could use my fourth wheel or its over kill. ???
here is a screen shot works well good 5 weeks to get here.just the vehicle that is
//the code
AlignToTerran(cTerrain *terrain)
{
cComponentMotion *ComponentMotion = GetComponentMotion();
if(ComponentMotion == NULL)
return;
//we need a height value based on the x and z value from the terrain
//for each wheels x z
// (x, z) relative to terrain's local space.
float y[MAX_WHEEL_FRAMES];
D3DXMATRIX worldpos;
//we need a height value based on the x and z value from the terrain
// (x, z) relative to terrain's local space. for the centre
float y1 = terrain->getHeight(ComponentMotion->m_vPos.x, ComponentMotion->m_vPos.z);
D3DXMatrixIdentity(&worldpos);
D3DXMatrixTranslation(&worldpos, ComponentMotion->m_vPos.x, y1, ComponentMotion->m_vPos.z);
//MIN_WHEEL_FRAMES because we only want the front 2 and back 2
for(UINT ctr =0; ctr < MIN_WHEEL_FRAMES; ctr++)
{
WheelMatrix[ctr]*= worldpos;
y[ctr] = terrain->getHeight(WheelMatrix[ctr]._41, WheelMatrix[ctr]._43);
}
//we now have our terrain height values for each wheels location on the terrain
//we now need to use 3 points to make a plane I think there for I am ???
D3DXVECTOR3 points[3];
D3DXVECTOR3 u , v, n, objn;
int a = 0, b =1, c = 2;
objn = D3DXVECTOR3(0.0,-1.0,0.0);
points[a] = D3DXVECTOR3(WheelMatrix[0]._41, y[0], WheelMatrix[0]._43);//wheel left front
points[b] = D3DXVECTOR3(WheelMatrix[1]._41, y[1], WheelMatrix[1]._43);//wheel left back
points[c] = D3DXVECTOR3(WheelMatrix[2]._41, y[2], WheelMatrix[2]._43);//wheel right front
//First convert the three points into two vectors
u = points[a] - points[c];
v = points[b] - points[c];
//Step 2
//Find the cross product of the vectors
D3DXVec3Cross(&n, &u, &v);
D3DXVec3Normalize(&n, &n);
// Obtain axis between two normals the wheels and the object, it will be the rotation axis
// Calc angle between normals
D3DXVECTOR3 axis;
D3DXVec3Cross(&axis, &objn, &n);
D3DXVec3Normalize(&axis, &axis);
FLOAT angle;
angle = acos(D3DXVec3Dot(&objn, &n));
if(angle != 0.0f)//EPSILON
{
D3DXMATRIX r;
// Create a quaternion with the rotation difference between normals
//m_Inclination = Quaternion.CreateFromAxisAngle(axis, angle);
D3DXMatrixRotationAxis(&r,
&axis,
angle);
Rotation *= r;
}
//set the matrix to the new location
SetWorldPos(worldpos, Rotation, Scale);
}//end AlignToTerran