• Advertisement

• ### Popular Now

• 13
• 15
• 27
• 9
• 9
• Advertisement
• Advertisement
• Advertisement

# Rotating a bike towards a particular node

This topic is 3888 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, We are trying to move a bike on a spline based system. We are getting the direction the bike is supposed to move to and the angle of turn but for some reason its always rotating in the same direction. I beleive to get the direction of rotation we need to use the cross product but I am not sure how to use the cross product to orient the bike correctly. This is what we are currently doing :- Step 1 : Get next node to move to Step 2 : Find direction the bike should move to (next node position - bike position) Step 3 : Get the direction of the next node from the previous node. Step 4 : Dot product of bike direction and next node gives me the angle of rotation. After this I am not sure how to use the cross product to orient the bike towards the node. Unfortunately I can't supply the code as I am under strict NDA and hence have just put the algorithm on how I am trying to do it. Thanks

#### Share this post

##### Share on other sites
Advertisement
Hem !

Beware of trigonometry ! Inside the dot product you have a cosine.
If you compute your dot product with the bike forward vector: you will always get an angle relative to the forward vector but never know which side of this vector you will be (aka as cos(30deg) = cos(-30degree)).

Better compute your dot product with the new velocity vector using the right vector of your bike: you will get your angle through 90 degrees - angle from right vector.

This should fix your problem.

Ghostly yours,
Red.

#### Share this post

##### Share on other sites
Hi Red Ghost,

Thanks for the answer. Let me explain a bit more of what I am doing or trying to do. I am writing an AI for a bike and I have the access to velocity. I am moving the bike on a spline and the physics design doesn't allow me to create an orientation matrix and set it for rotating the bike as this would hinder certain aspects of the physics system. I only have access to functions which turn the bike right or left by some specific angle.

I need the sign of the angle only to turn the bike right or left. In general the angle itself is of no use to me. I tried to project the node position and the bike position on the y axis plane and create a plane towards the right vector and check the position of the bike with respect to that plane i.e. if the bike is in front or back of the plane but to no avail.

#### Share this post

##### Share on other sites
@ sabertooth:

Since you have the velocity vector, getting the right vector is simple for your bike unless you also want to do loopings with your bike.

If you do not do loopings with your bike, then the up vector is always the same.
Finding the right vector is then just the cross product of the velocity vector with the up vector.

ForwardVector = VelocityVector.Normalize()//UpVector is defined from the start and never vary much//(i.e. no loopings and no velocity vector paralell to the up vector)RightVector = ForwardVector.Cross(UpVector)

If you do loopings and the variation of the velocity vector are not overly important (no turns on a dime), then you can reorthogonalize your up and right vector provided you start with an orthogonalized velocity, up and right vector (e.g. the bike starts on a flat platform to provide a simple up and a simple forward vectors).

//this code reorthogonalize the up and right vectors after a velocity change//beware, the forward, up and right vectors must be orthogonalized at start.ForwardVector = VelocityVector.Normalize()RightVector = ForwardVector.Cross(UpVector)UpVector = RightVector.Cross(ForwardVector)

Hope that helps.

Ghostly yours,
Red.

#### Share this post

##### Share on other sites
Thanks red ghost, that Fixed it.

#### Share this post

##### Share on other sites

• Advertisement