My problem with spin turned out to be something pretty bizarre when I started looking at the whole rotation vector for the Irrlicht node that was being affected by the physics engine. From 0 to ~90 -- whether it was 89.99999 or 90.0 I am unsure--the rotation around the Y axis was as expected, but after 90 degrees X and Z got flipped and Y started to decrement. Something like this as I rotated clockwise:
(0, 0, 0) ...
(0, 30, 0) ...
(0, 60, 0) ...
(180, 90, 180)...
(180, 60, 180)...
(180, 30, 180)...
The effect was that it did continue to spin around properly but it really screwed up me figuring out if it was pointing the in the right direction. I have tabled that for now in use of faceTarget.
It looks like IRigidBody::faceTarget is an irrBullet exclusive. I am having a little bit of odd trouble with it. I am trying to make an enemy face the player, so I figure I'd have the enemy face the position of the player. But this doesn't seem to work well--especially when they get close to each other. It's like it gives up the closer they are. What I did instead was get the 2d X,Z direction to the player, normalize that, and subtract the X, Z components from the enemy's current position. For some reason that works (not sure what adding them wasn't working, but it pointed the opposite way).
I am assuming I have some matrix stupidity going on that I need to correct. Here's what the enemy object's world transform was from getWorldTransform()
-0.707107, -0, 0.707107, 0,
-0, 1, -0, 0,
-0.707107, -0, -0.707107, 0,
5, 10, 5, 1,
Initially positioned at (5, 10, 5), rotation set to (0, 0, 0), and scale set to (0.33, 0.33, 0.33). The world as I am trying to set it up has +Y as up (0, 1, 0). Gravity is (0, -9.8, 0).
I am wondering if I am doing something naive with my matrices because of these goofy little problems.
Edit: Well for the whole pointing backwards thing I assume there's something backwards in faceTarget:
Code: Select all
matrix4 mat = body->getWorldTransform();
vector3df rot, dif = mat.getTranslation() - targetPosition;
rot.Y = atan2( dif.X, dif.Z ) * 180.f / irr::core::PI;
rot.X = atan2( dif.Y, sqrt( dif.X * dif.X + dif.Z * dif.Z ) ) * 180.f / irr::core::PI;
mat.setRotationDegrees(rot);
body->setWorldTransform(mat);
Particularly mat.getTranslation() - targetPosition. Might these need to be reversed? I just followed the math through. Say the object is at (5, 10, 5) and I want it to face (0, 10, 0). dif will be set to (5, 0, 5), which will calculate an angle of 45 degrees. However, I would have preferred an angle of 225 degrees or -135 degrees depending.
The oddball stuff that was happening came about when I was trying to flip the X and Z components of the target location to try to flip it around. I understand now why that wouldn't work because that flip changes, say a target of (5, 0, 5) to (-5, 0, -5) which is only going to work if the body is at (0, 0, 0). So now I see why my normalized direction vector worked better.