I am making a game from scratch using OpenGl and GLFW. Since my motivation is to learn computer graphics, I am using my own explicit math functions for rotation and translation. My current rotation code tries to rotate the player to the direction of mouse pointer; with the center of player as origin, I take two vectors and deduce the rotation angle by a dot product. My problem is that the rotation of the player never stops and the object keeps spinning even after rotation angle turns to 0, it again starts increasing.
Following is my current implementation of rotation : `
double safe_acos(double value) {
if (value<=-1.0) {
return 0;
} else if (value>=1.0) {
return 0;
} else {
return acos(value);
}
}
void rotateObject(std::vector<float> &GameObjPos, double angle, GE_Vec3 centrePos) {
auto cosVal = (float) cos(angle);
auto sinVal = (float) sin(angle);
for (size_t i = 0; i <= GameObjPos.size() - 3; i += 3) {
float x = GameObjPos[i];
float y = GameObjPos[i + 1];
GameObjPos[i] = x * cosVal - y * sinVal - centrePos.x * cosVal + centrePos.y * sinVal + centrePos.x;
GameObjPos[i + 1] = x * sinVal + y * cosVal - centrePos.x * sinVal - centrePos.y * cosVal + centrePos.y;
}
}
double GetMagnitudeSq(GE_Vec3 GameObjPos) {
return (GameObjPos.x * GameObjPos.x + GameObjPos.y * GameObjPos.y + 0.0f);
}
double GetDotProduct(GE_Vec3 GameObjPos1, GE_Vec3 GameObjPos2) {
return (GameObjPos1.x * GameObjPos2.x + GameObjPos1.y * GameObjPos2.y);
}
double GetRotationAngle(GE_Vec3 GameObjPos1, GE_Vec3 GameObjPos2) {
double dotProduct = GetDotProduct(GameObjPos1, GameObjPos2);
double magnitudeProduct = GetMagnitudeSq(GameObjPos1) * GetMagnitudeSq(GameObjPos2);
double magnitudeSqRoot = sqrt(magnitudeProduct);
return safe_acos(dotProduct / magnitudeSqRoot);
}
GE_Vec3 GetGameObjCentre(std::vector<float> GameObjPos) {
return {(GameObjPos[0] + GameObjPos[6]) / 2, (GameObjPos[1] + GameObjPos[7]) / 2, 0.0f};
}
GE_Vec3 GetObjFrontCentrePos(std::vector<float> GameObjPos){
return {(GameObjPos[6] + GameObjPos[9]) / 2.0f, (GameObjPos[7] + GameObjPos[10]) / 2.0f };
}
void GE_Physics::update(std::vector<std::vector<float>> &GameObjPosVec, int PlayerDirection, MousePosition mousePos,
float deltaTime = 1.0f) {
for (int i = 0; i < GameObjPosVec.size(); i++) {
updatePosition(GameObjPosVec[i], PlayerDirection, deltaTime);
MousePosition pos = {-1.0 + 2.0 * (double) mousePos.x / 1280, 1.0 - 2.0 * (double) mousePos.y / 720};
updateRotation(GameObjPosVec[i], pos);
}
}
void GE_Physics::updateRotation(std::vector<float> &GameObjPos, MousePosition mousePos) {
if(mousePos.x ==-1 && mousePos.y ==1){
return;
}
GE_Vec3 gameObjFront = GetObjFrontCentrePos(GameObjPos);
GE_Vec3 gameObjCentre = GetGameObjCentre(GameObjPos);
GE_Vec3 mousePosVec = {(float) mousePos.x - gameObjCentre.x, (float) mousePos.y - gameObjCentre.y, 0.0f};
GE_Vec3 objFrontVec = {gameObjFront.x - gameObjCentre.x, gameObjFront.y = gameObjCentre.y, 0.0f};
double rotAngle = GetRotationAngle(objFrontVec, mousePosVec);
#ifdef __DEBUG__
std::clog<<"rotation angle : "<<rotAngle<<std::endl;
#endif
rotateObject(GameObjPos, rotAngle, gameObjCentre);
}
`
GameObjPosinrotateObjectis distracting: why do you need to jump three indices? \$\endgroup\$