Rotate what now? Aks, what are you talking about?
I wanted to write down how I made the enemy characters in Artificial Rage rotate towards the player,
since I couldn’t find a simple answer.
Most things I found was math. Now math is fun and good, but when you’re tired
and want to get one thing just to work at 4 am, it’s not gonna help you.
Especially since I’ve never learned linear algebra at any school I’ve went to (or I just likely don’t remember),
and double especially since all the math lingo is in English and I have no idea what any of it means!!!
Getting Gooder at math is on my eternal to-do list, but anyhow, for those like me
who just need to get something done, here’s how I did it.
First off, I just wanted the character to rotate around it’s Y-axis: If you would
stick a.. well stick in a grape and twirl the stick in your fingers, that’s the Y-axis of the grape.
This means the following snippet does not take account the other axises. But I’m sure it could be used for it.
Code dump incoming:
// C99 code
void Actor_RotateTowards(Actor_Data* actor, Vector3 targetPosition)
{
// Rotates the actor around Y axis
Vector3 diff = Vector3Subtract(actor->position, targetPosition);
float y_angle = -(atan2(diff.z, diff.x) + PI / 2.0);
Vector3 newRotation = (Vector3) { 0, y_angle, 0 };
// Use quaternion slerping for smoother rotation, so the actor always follows the shortest path
Quaternion start = QuaternionFromEuler(actor->rotation.z, actor->rotat