# A woo who moment with vehicle and terrain alignment need advise

This topic is 2142 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

https://reg8dw.bay.livefilestore.com/y2p5-OBalH6KT8WcJe2FDnByL1PQeABFkTKEEOfmSGNpeb0pVZB-AIHCuObpNOliKGvzyIgIqV7EwzSgSL4H-GmDnTx7n8Yiy-cuZn81cJCeVE/TerrainVehicleAlignment.jpg?psid=1

//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


##### Share on other sites

The normal is used for finding the angle between the object and the terrain. both of these normals(object,terrain) should be pointing relative uppish, and the angle you get is the one that will rotate one onto the other. If your using the wrong normal(0,-1,0) for one, but still get the right results, then you are likely using the wrong normal for BOTH.

I think that the issue is in your calculation of the terrain normal, or, when creating the rotation axis, you are specifying the normals in the wrong order. When you take the 3 points and get the 2 vectors, you should probably use c-a, and b-a to create U and V vectors. which if i my math is correct will give the proper normal. Remember that the cross product is not associative, which means that x^y = z != y^x.

##### Share on other sites

That worked well.

thank you.

##### Share on other sites

Remember that the cross product is not associative, which means that x^y = z != y^x.

Small nitpick: x^y != y^x means the cross product is not commutative. But it's not associative either

##### Share on other sites

Remember that the cross product is not associative, which means that x^y = z != y^x.

Small nitpick: x^y != y^x means the cross product is not commutative. But it's not associative either

rofl... whoops, *I* knew what *I* meant anyway, and its non-commutativity was what I was trying to get across.

• 18
• 18
• 11
• 21
• 16