gecko0307 / dagon

3D game engine for D
https://gecko0307.github.io/dagon
Other
321 stars 30 forks source link

entity rotation causes disposition of scale. #45

Closed aferust closed 5 years ago

aferust commented 5 years ago

I want my plane to do a barrel roll. it does with following code. However, disruptions in scale also occurs. Is it a bug or, am I on a totally wrong way?

auto q = rotationQuaternion(Axis.z, degtorad(stepAngle * dt)); // stepAngle = 150.0f;
plane.rotation.z += q.z;
gecko0307 commented 5 years ago

At first I thought it was a bug with DefaultEntityController, but it seems that plane.rotation is just being messed up. Entity.rotation is a quaternion, so you can't simply modify its components. In your case, you update an angle, then create a quaternion from it:

float z;
z += degtorad(stepAngle * dt);
plane.rotation = rotationQuaternion(Axis.z, z);

If you want to work in axis-angle representation, it's better to store three angles and multiply their corresponding quaternions:

float z, y, z;
plane.rotation = 
    rotationQuaternion(Axis.x, x) * 
    rotationQuaternion(Axis.y, y) * 
    rotationQuaternion(Axis.z, z);

If you want to update a quaternion with an angular velocity, then it should be done like this:

Vector3f angularVelocity = Vector3f(dx, dy, dz);
plane.rotation += 0.5f * Quaternionf(angularVelocity, 0.0f) * plane.rotation * dt;
plane.rotation.normalize();

There should be helper methods for updating a rotation, I just haven't added them yet.

aferust commented 5 years ago

Yes, it needs some helper methods like: http://wiki.ogre3d.org/Euler+Angle+Class

I think camera has something like that already: /graphics/tbcamera.d