Reputation: 27
So, I'm working at an 8ball-pool game in OpenGL and the balls are troubling me. They move pretty well but they currently slide along the table. I can't seem to figure out the rotations along the three axis as they seem to change after a rotation. All I know is the current position (curPos) and the next one (nextPos).
velocityAng += (nextPos - curPos) / sphereRadius;
//and afterward I rotate on each axis
modelMatrix = glm::rotate(modelMatrix, velocityAng[2], glm::vec3(0, 0, 1));
modelMatrix = glm::rotate(modelMatrix, velocityAng[1], glm::vec3(0, 1, 0));
modelMatrix = glm::rotate(modelMatrix, velocityAng[0], glm::vec3(1, 0, 0));
I figured out that if the ball is in its initial position and I shoot at up or right degrees it goes well, but after I shoot right, and rotate the ball 90 degrees, when I shoot up it doesn't go well anymore. It's obvious that shooting at any degrees different than 0, 90, 180, 270 it goes completely wrong. The axis of the ball seems to change when I rotate it.
Upvotes: 1
Views: 463
Reputation: 21936
You’re rotating too much, you only need single rotation. The axis of that rotation should be perpendicular to (nextPos - curPos) vector, i.e. you need something like this (untested):
const float angle = ( nextPos – curPos ).length() / sphereRadius;
// Assuming the table is on the XY plane
const vec3 axis = cross( nextPos - curPos, glm::vec3( 0, 0, 1 ) );
modelMatrix = rotate( modelMatrix, angle, axis );
Upvotes: 1