Players in my game are supposed to be able to move at a constant speed, no matter what their heading is set to. The current rotation code works, except when the heading is directly behind the player, in which case, it instantly flips around.
I know this has to do with the else branch being triggered, but how can I stop it from being triggered when the player is 180° degrees around? is there a distance check I should be doing? or should I make the first if something like if (dp + 0.01f > STEP.getY()) to fix this? Any better suggestions?
If the dot product > the rotation step (STEP.getY()) go in that direction
The other if is the same thing. The reason it needs to be more than the step value is so the player dosen’t effectively ‘vibrate’, because it’s moving too far one way, then the other.
The else branch just sets the player’s rotation to point where you are aiming.
The general logic should be:
-if it’s to the left of me, turn left
-if it’s to the right of me, turn right
-if it’s absolutely directly behind me, turn left (it doesn’t materially matter which you pick here)
And the reason you wobble back and forth is because you don’t scale the amount of turn by the amount of left/right (ie: the left right dot)
Well, you also need to do a dot product with the forward direction so you know if the thing is in front or behind you. Only if it’s in front of you do you do the code you have… or you can prenormalized the dp.
Like, if the forward dot says it’s behind you (<0) then use the sign of the dp… unless dp is 0 then just use 1 or -1.
If so you are treating its values as if they aren’t magic black-box values… which could be dangerous since they are magic 4 dimensional values that probably do not mean what you think they mean.
I’ve so far been assuming that STEP is a Vector3f.
Ok, this looks like a MUCH better solution then the crazy ting I put together a few hours ago…
What I had checked the distance between two vectors multiplied by direction quaternions. Bad.