0
al_draw_rotated_bitmap(playerTux, defaultPlayer1.size / 2, defaultPlayer1.size / 2, 
        defaultPlayer1.pos[0], defaultPlayer1.pos[1], mousePos / mouseSensitivity, 0);
        al_draw_rotated_bitmap(playerTux, defaultPlayer2.size / 2, defaultPlayer2.size / 2, 
        defaultPlayer2.pos[0], defaultPlayer2.pos[1], 0, 0);

This function draws the bitmap of the player at a given position and a given angle at every loop of a while-loop. The movement is altered due to the events in the keyboard and the angle of the player due to the movement of the mouse in the x axis. Ignore the division by mouseSensitivity (Which is an internal aspect). consider just that it is possible to spin the player perfectly in it's own axis by moving the mouse. (OBS: The image of the weapon is already on the image of the player, so the weapon spins together with the player).

if (state.buttons & 1) {
            al_draw_rotated_bitmap(pistolFire, defaultPlayer1.size / 8, defaultPlayer1.size / 2, 
            defaultPlayer1.pos[0], defaultPlayer1.pos[1], mousePos / mouseSensitivity, 0); }

Now here, when the left button of the mouse is pressed, a "fire image" appears at the end of the weapon.

Considering all this, we have our player moving through the map (increassing and decreassing it's own xPos and yPos at every event of the W, A, S, D), and we have another player on the map that has the same capacity. When we fire the weapon (When we press the left button of the mouse), we must HIT the other player if he is inside the range of the angle of the weapon.

The question is: How can I do it?

if (state.buttons & 1) {
    detectShotColision(); }

How could I write such a function, considering only the angle of the weapon and the x and y position of both players?

4

1 回答 1

1

You can use atan2() to calculate the angle between the players. If it equals (within some acceptable delta) the angle the gun is facing, then the player was hit.

If there could be things in between the players that absorb the hit, then you'll need to walk a line in the direction the gun was fired. You may be able to combine the two for an optimized solution.

于 2013-03-31T13:31:09.607 回答