Shooter Game Help

I've been making a multiplayer shooter game and have a small problem. I want to know how to make where the bullet appears be where the players gun is. The bullet shoots towards the mouse using atan2, and appears in the center of the player. So how would I make the bullet appear where the gun is depending on how the player is rotated. I don't know if you really need and of my code to know what I mean, but if you do say so.
Short answer: Math


Long answer:

There are a lot of shortcuts you can take here depending on where the gun output is in relationship to the point of rotation and the position of the player. For purposes of this explanation, I'm going to assume you are unable to take any of these shortcuts.

To start with, we need to know a few things:

1) The player's position
2) The point of rotation in relation to the player's position.
3) At angle 0 (no rotation), the point of the gun output in relation to the point of rotation
4) The angle of rotation

Note that all of these positions (except for #1, the player position) are relative to another point. Therefore if we assume there is no rotation, the point of gun output would simply be:

1
2
3
output = player_pos + rot_pos + gun_pos;

// that is... #1 + #2 + #3 


So then the tricky part is rotating the gun. But really.. that's not all that tricky.

2D rotation is pretty simple. Given an angle 'theta' (#4), you can rotate any 2D point with the below code:

1
2
3
4
5
double sintheta = sin(theta);
double costheta = cos(theta);

rotated.x = (gun_pos.x * costheta) - (gun_pos.y * sintheta);
rotated.y = (gun_pos.y * costheta) + (gun_pos.x * sintheta);


'rotated' is now rotated around the origin (0,0). Since 'gun_pos' is a relative to the rotation point, we were already oringiated at 0,0.

So all we have to do from there is sub in our rotated point:

 
output = player_pos + rot_pos + rotated;


And that's where you'd spawn your bullet.
Could you give an example? I can't get it working.
So....
1. player.x and player.y
2. ?
3. like player.x + 10 player.y + 10?
4. player_pos aimed at mouse?

player.dir = player pointed at mouse with atan2

I tried this and the rotation of the circle worked, but the circle was way off from the player.
1
2
3
4
5
6
7
double sintheta = sin(player.dir);
double costheta = cos(player.dir);

int rotatedX = (player.x + 10 * costheta) - (player.y + 10 * sintheta);
int rotatedY = (player.y + 10 * costheta) + (player.x + 10 * sintheta);

al_draw_filled_circle(rotatedX, rotatedY, 5, al_map_rgb(255, 0, 255));



Okay I got it figured out.

1
2
newBullet.x = players[ID].x + (20 * cos(players[ID].dir) - 20 * sin(players[ID].dir));
newBullet.y = players[ID].y + (20 * sin(players[ID].dir) + 20 * cos(players[ID].dir));
Topic archived. No new replies allowed.