Saturday, September 10, 2022
HomeGame Developmentopengl - Rotate recreation object to the mouse pointer

opengl – Rotate recreation object to the mouse pointer


I’m making a recreation from scratch utilizing OpenGl and GLFW. Since my motivation is to study laptop graphics, I’m utilizing my very own specific math capabilities for rotation and translation. My present rotation code tries to rotate the participant to the course of mouse pointer; with the middle of participant as origin, I take two vectors and deduce the rotation angle by a dot product. My drawback is that the rotation of the participant by no means stops and the thing retains spinning even after rotation angle turns to 0, it once more begins rising.

Following is my present implementation of rotation :
`

double safe_acos(double worth) {
    if (worth<=-1.0) {
        return 0;
    } else if (worth>=1.0) {
        return 0;
    } else {
        return acos(worth);
    }
}

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.dimension() - 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::replace(std::vector<std::vector<float>> &GameObjPosVec, int PlayerDirection, MousePosition mousePos,
                        float deltaTime = 1.0f) {
    for (int i = 0; i < GameObjPosVec.dimension(); 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);
}

`

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments