0

I'm trying to rotate an object in a program written with OpenGL.

This object goes around in 3D space.

The angle of rotation on the Y axis (the yaw) is calculated with this:

facingAngle = -atan2(heading.y, heading.x);

…and I add it to the model like this:

glm::mat4 model;
model = glm::translate(model, position);
model = glm::rotate(model, facingAngle, glm::vec3(0.0f, 1.0f, 0.0f));

Then I would like this object to lean towards the direction it's heading to, like a cycle or an aircraft does (rolling).

I have tried countless options to calculate this value, but without any luck to find what I actually want.

The best I get so far, if I do this:

leaningAngle = glm::sqrt(squaredLength(steeringForce)) / gravity;

(The function, squaredLength, does the following: return ( vector.x * vector.x + vector.y * vector.y );)

The main problem with this is that it always leans the object to one given direction (so in half of the cases it leans outwards instead of inwards). However when I would like to modify this value with either *1 or *-1 based on where the object is heading, I get a weird behavior which I suppose is Gimbal lock.

What I would like to have is a simple equation which will give me an angle to roll the object between realistic values (90 and -90 degrees), and the weight of the angle should be calculated based on the current steering force (the sharper the turn, the bigger the leaning angle is).

The final code should be look like this:

glm::mat4 model;
model = glm::translate(model, position);
model = glm::rotate(model, facingAngle, glm::vec3(0.0f, 1.0f, 0.0f));
model = glm::rotate(model, leaningAngle, glm::vec3(1.0f, 0.0f, 0.0f));
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Letokteren
  • 749
  • 1
  • 15
  • 28
  • The problem is you are combining Euler angles with rest of your transformations which is order sensitive and has its quirks. Blind trial and error will only misleads you further. Instead take a look at [Understanding 4x4 homogenous transform matrices](http://stackoverflow.com/a/28084380/2521214) especially the links at the end where you can find object/camera control examples doing exactly what you want... – Spektre Feb 26 '17 at 22:34
  • @Spektre Thank you very much for the links! They are good reads, indeed. However maybe I'm misunderstanding something here, but basically what each says is to ditch glm's functions and write my own transformation functions and models? I don't see the point in that. Shouldn't glm work in the same way? – Letokteren Feb 27 '17 at 09:25
  • Of coarse you can use GLM for this (I use my own instead as at time I start with this there were no GLM and I stick with mine implementation as I am used to it) The difference is you should use more matrices (one for each object or frame of reference) and combine them together instead of stacking up rotations and translations... They will be integrated inside the matrix that move/turn instead. for example in the links I use `mobj` for the object and `meye,ieye` for the camera you can compute eye from obj just by inversing and off-setting so it will be always following `obj` automatically – Spektre Feb 27 '17 at 10:02
  • there is a difference between local and global rotations ... – Spektre Feb 27 '17 at 10:06

0 Answers0