SpiderPig Posted April 30 Share Posted April 30 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? Quote Link to comment Share on other sites More sharing options...
SpiderPig Posted April 30 Author Share Posted April 30 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. Quote Link to comment Share on other sites More sharing options...
Andy90 Posted April 30 Share Posted April 30 maybe you can set the gravity to the center of each qube. Quote Link to comment Share on other sites More sharing options...
Josh Posted April 30 Share Posted April 30 You need quaternions and Slerp(). It's the only way to interpolate between arbitrary rotations. Quote 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 More sharing options...
SpiderPig Posted April 30 Author Share Posted April 30 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. Quote Link to comment Share on other sites More sharing options...
Josh Posted April 30 Share Posted April 30 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. 1 Quote 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 More sharing options...
SpiderPig Posted May 1 Author Share Posted May 1 Yeah I think that's exactly what I need to do. Should be easier than writing out a 1000 line switch statement. Quote Link to comment Share on other sites More sharing options...
SpiderPig Posted May 1 Author Share Posted May 1 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. 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.