Sign in to follow this  

Vehicle Steering

This topic is 725 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

I want to make the vehicle steer to look at the player, so if the player is on the vehicle right side, I should call "vehicle->SteerRight()", if the player is on the left I should call "vehicle->SteerLeft()"

 

Given (D3DXVECTOR3 PlayerPosition and D3DXMATRIX VehicleWorldMatrix) how do I find out if the vehicle should steer right or left?

Edited by Medo3337

Share this post


Link to post
Share on other sites

If PlayerPosition is in world space then you just need to do inverse transform of it's position by vehicle matrix. After that sign of Y vector will tell you if he is on the left or right side.

Share this post


Link to post
Share on other sites

Another way of doing this, that I personally find more intuitive, is to put a plane that runs through the vehicle and essentially cuts it in a left and a right half. Then you can just detect which side of the plane the player is on, which is usually done through a signed distance check against the plane. Positive might mean left and negative right, depending on which side of the plane you consider "up".

Edited by GuyWithBeard

Share this post


Link to post
Share on other sites

If you have a forward vector of the vehicle, you could also project playerpos, vehiclepos and forward_vector to the 2D ground plane and do:

 

(playerpos - vehiclepos) dot forward_vector

 

and check the sign.

 

Projecting to 2D ground plane just means removing the y coordinate from all coordinates. (or z, or whatever is "up" in your world)

Might not work like you want if you want to be driving on walls and ceilings, but if you don't need that, it should work.

Edited by Olof Hedman

Share this post


Link to post
Share on other sites

I'm not sure if this is correct, but here is what I'm doing now and it seems that it works:

D3DXVECTOR3 view( -VehicleWorldMatrix.m[2][0], -VehicleWorldMatrix.m[2][1], -VehicleWorldMatrix.m[2][2] );
D3DXVec3Normalize(&view, &view);
D3DXVECTOR3 toTarget = PlayerPosition - VehiclePosition;
D3DXVECTOR3 cross;
D3DXVec3Cross(&cross, &toTarget, &view);

if (cross.y > 0)
{
     // Steer right

} else {
     // Steer left

}
If you think there is something wrong, please let me know.
Edited by Medo3337

Share this post


Link to post
Share on other sites

view would be what I called "forward_vector".

 

Your method should work fine in most situations, with the same limitations as what I posted.

 just a bit more unnecessary calculations compared to my suggestion smile.png

 

BoredEngineers solution is the most general purpose and would work regardless of the orientation of the vehicle. (assuming you want "right of" to mean from the vehicles perspective, which from an outsides observer would be "above" or "below" the vehicle, if it was turned on its side)

 

In your and my solution, the roll of the vehicle (or rotation around the length of the vehicle, or whatever you want to call it) will not affect what is "right of" and "left of" the vehicle.

 

No need to normalize the "view" vector btw, since you are only interested in the sign of y.

Edited by Olof Hedman

Share this post


Link to post
Share on other sites

@Olof Hedman: 

 

BoredEngineers solution is the most general purpose and would work regardless of the orientation of the vehicle. (assuming you want "right of" to mean from the vehicles perspective, which from an outsides observer would be "above" or "below" the vehicle, if it was turned on its side)

 

 

D3DXMatrixInverse is taking FLOAT* pDeterminant,

 

I have: D3DXVECTOR3 PlayerPosition; and D3DXMATRIX VehicleWorldMatrix

 

How do I call D3DXMatrixInverse() ?

Share this post


Link to post
Share on other sites
@GuyWithBeard: Here is what I tried (but it doesn't work):
D3DXMatrixInverse(&VehicleWorldMatrixInversed, NULL, &VehicleWorldMatrix);

D3DXVECTOR3 TransformedCharacterPosition;
D3DXVec3TransformCoord(&TransformedCharacterPosition, &PlayerPosition, &VehicleWorldMatrixInversed);
D3DXVec3Normalize(&TransformedCharacterPosition, &TransformedCharacterPosition);
Edited by Medo3337

Share this post


Link to post
Share on other sites

 

@GuyWithBeard: Here is what I tried (but it doesn't work):
D3DXMatrixInverse(&VehicleWorldMatrixInversed, NULL, &VehicleWorldMatrix);

D3DXVECTOR3 TransformedCharacterPosition;
D3DXVec3TransformCoord(&TransformedCharacterPosition, &PlayerPosition, &VehicleWorldMatrixInversed);
D3DXVec3Normalize(&TransformedCharacterPosition, &TransformedCharacterPosition);

 

Not sure why normalize it at the end. After you've transformed it, player coordinates are now local space of the car, so Y or Z value (which ever is horizontal axis in your case) tells you on which side character is.

Share this post


Link to post
Share on other sites

@BoredEngineer: Actually what I can use is the value of X not Y, and there is no value for Z

 

Now, if x > 0, the character on the right, if x < 0, the character on the left.

Share this post


Link to post
Share on other sites

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this