Jump to content

Aligning to Up & Forward Vectors


SpiderPig
 Share

Recommended Posts

I'm using this code to align a pivot to the up direction of my player controller.

up_pivot->AlignToVector(up_direction, 1);
target_quat = target_quat.Slerp(up_pivot->GetQuaternion(), 0.1f);

kinematic_joint->SetPose(pos, target_quat);

The video below shows it working really well when the forward direction is along the X axis (red arrow in the distance).  However when moving back and forth along the z axis player spins 90 degrees to face the (+) X axis when the up vector is something like Vec3(0.0f, 0.0f, 1.0f) or Vec3(0.0f, 0.0f, -1.0f).  You can see it at around 25 seconds into the video.  I know this is a by product of aligning to a vector, but what I'm wondering is - is there is a way to align to an up vector as well as a forward vector?  This way I think my player rotation can be more controlled when the up direction changes as I can track both up and forward vectors with ease.  Matrix math perhaps?

Link to comment
Share on other sites

After toying with aligning to vectors it seems that one simple align to vector function will not be suited for all use cases.  It's fine for one use alignment, like at game start, but not good for animating a vector that can go through 360 degree rotation on all axis in real-time.  There are certain situations where in order to get a good result the object that's being aligned to the vector must rotate by changing it's forward axis.  Which is what the video above was showing.

I think I've found a solution for this.   Although at the moment it is rather convoluted.  It involves writing 216 switch statements (scream) that takes the desired up direction axis, the current forward axis, and the current sideways axis and spitting out the new forward and side vectors that will ensure the transition to a new up vector will be smooth.  The camera never rotates 90 degrees to a new forward direction.  So far it's very smooth and after few more days (years) I'll have it finished and will show the (hopefully good) result. :)

At the moment it only works if walking over the edge of a cube but I'm near certain I can modify it slightly to walk all around a spherical object.

Link to comment
Share on other sites

Gravity is going to the centre of the cube and I do use Slerp().  :)

The problem is what the video shows.  Going over a some edges will cause the player to rotate about its local y axis 90 degrees.  I think even if I used AlignToVector() and was walking on a sphere it would do the same thing.

Link to comment
Share on other sites

Your final rotation must be calculated arbitrarily then. You want to turn the entity 90 degrees over the edge. You have to rotate around the axis of that edge.

  • Upvote 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Pretty sure I got it working.  I'll test it a bit more before bringing in the game though.  There is a switch statement for the change in the UP vector.  Depending on what axis is UP currently to what axis is the target UP direction dictates what the rotation matrix should be for both the forward and side vectors.  Should work for a round planet too.

 

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...