Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Cannot get the math right

This thread is locked; no one can reply to it. rss feed Print
Cannot get the math right
briostheman
Member #12,496
January 2011

I have a space type shooter game where a ship shoots bullets. The ship can rotate 360 degrees. Basically the ship faces the mouse cursor as the cursor moves around the screen. Right now the bullets do not leave from the center of the ship, they move based on the rotation so they are offset sometimes. It looks weird so I want to fix it.

Here is the rotation code for the player

#SelectExpand
1void Player::Think(int mx, int my, int null_int1, int null_int2){ 2 3double radangle = (atan2((my + 10) - (posY + 16), ((mx + 10) - (posX + 16)))); 4 5//update the player angle 6SetFaceAngle(radangle +1.5);

and here is the code to create the bullet

a = gun->fire(pos_x, pos_y, player, 16);

Peter Hull
Member #1,136
March 2001

Where are you drawing the centre of your ship - is it (posX, posY) or (posX+16, posY+16)? Also is pos_x the same as posX?

Basically, if I understand correctly, you want to start the bullet at (cx + r * cos(ang), cy + r * sin(ang)) where ang is the angle the ship is pointing, (cx, cy) is the ship's centre, and r is the distance from the ship's centre to the point where you want the bullet to appear. (I'm assuming your ship has a gun at the end of its nose, like the one in Asteroid)

briostheman
Member #12,496
January 2011

correct, the bullet should leave from the center of the player and travel out the nose of the ship, here is the player draw

#SelectExpand
1al_draw_rotated_bitmap(image, (width / 2), (height / 2), posX+(width / 2), posY+(height / 2), faceAngle, 0);

kenmasters1976
Member #8,794
July 2007

You probably want to draw the center of your ship at (posX, posY) instead:

al_draw_rotated_bitmap(image, (width / 2), (height / 2), posX, posY, faceAngle, 0);

Go to: