0

I'm writing a simple version of the game Duck Hunt in C++, using OpenGL. I managed to render the 3D model of a rifle in the center of the screen, and what I want is to rotate it on his y and z axes depending on the mouse movements, so that the rifle is always pointing to the mouse pointer. I have tried to compute the rotation angles using the arctan equation, in order to have the right proportion between the mouse movements and the distance of the pointer from the rifle, but the result is not really good as the sensibility of the rotation seems to be too low. Here there is my current implementation:

void passiveMotion(int x, int y) {
    switch (gamePhase) {
        case gameStarted:

            if(x > 540 && x < 1000)
                gunYrot = ((-(atan(abs(y-720) / (double)(x-540)))) * 180 / M_PI);
            else if(x > 80 && x < 540)
                gunYrot = (((atan(-abs(y - 720) / (double)(x - 540)))) * 180 / M_PI - 180);


            if (y > 320 && y < 355) gunZrot = y;
        break;
        default: break;
    }
}

The game screen is this: enter image description here

Any clues about what could be the best approach to move the rifle?

Bonus question: at the moment, the bullet is shot from the point in which the rifle is rendered (so it starts from its center); how could I dinamically compute the coordinates of where the gun barrel ends to make it the starting point of the bullet?

Thank you for any help with this.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
luibo
  • 13
  • 3
  • Using atan2 will make your life at least a little bit easier. – user253751 Jun 07 '22 at 16:24
  • I guess you are using the old version of OpenGL, with glRotate and glBegin and so on. When you use newer versions you have to compute all the matrices yourself (libraries make it easy) which makes it very easy to access the same matrices outside of OpenGL – user253751 Jun 07 '22 at 16:24
  • 1
    How about computing a [LookAt matrix](https://stackoverflow.com/questions/349050/calculating-a-lookat-matrix)? – Wyck Jun 07 '22 at 16:36
  • See also [gamedev.se]. – Thomas Matthews Jun 07 '22 at 17:22
  • Take a look at this: https://github.com/aromanro/SolarSystem/blob/55c9b905c22aeebbf6dbda66474f707bf515804f/SolarSystem/SolarSystemView.cpp#L585 also to the function after it. In the end, that turns the camera towards the clicked point and also the spaceship as in this video: https://youtu.be/itc9wElsb2E – Adrian Roman Jun 07 '22 at 19:42

0 Answers0