Public Group

# SDL rotate rect evenly?

This topic is 565 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am making a basic "space shooter" game to test myself. I am using SDL external library in c++ to make it. I have a class called "Bullet" and a vector of Bullet objects. I als o have a class called "Ship", which is the player. When the player shoots, one of the Bullet objects is teleported to the player and given the angle of the player. The problem is that when the player is to the side the bullet is off-center. Can anybody help me? Here is my code for my class:

void Bullet::shoot(float ship_x, float ship_y, float ship_angle, int ship_width){
x = ship_x + (ship_width/2) - (width/2);
y = ship_y;
angle = ship_angle;
}



When the ship is pointing up (0 degree angle) or down (180 degree angle) than the bullet spawns perfectly. But the more away it gets from those the more off kilter it gets. Any thought?

##### Share on other sites
Is X,Y of the bullet the center of the bullet? And ship_x, ship_y the center of the player?

In that case you should handle x,y the same. I assume you want the bullet offset at the front of the ship.
Also, assumed angle is in radians and 0 means facing to the right.

float dx = ( ship_width / 2 ) * cosf( angle );
float dy = ( ship_width / 2 ) * sinf( angle );
x = ship_x + dx;
y = ship_y + dy;

If my assumptions are wrong you may need to offset and/or reverse the angle in cosf/sinf.

##### Share on other sites

Is X,Y of the bullet the center of the bullet? And ship_x, ship_y the center of the player?

In that case you should handle x,y the same. I assume you want the bullet offset at the front of the ship.
Also, assumed angle is in radians and 0 means facing to the right.

float dx = ( ship_width / 2 ) * cosf( angle );
float dy = ( ship_width / 2 ) * sinf( angle );
x = ship_x + dx;
y = ship_y + dy;

If my assumptions are wrong you may need to offset and/or reverse the angle in cosf/sinf.

When I try this code it only amplifies the problem. The bullet is very far off from the ship, and when the player rotates the ship the bullet sporadically moves close and away from the ship. I don't understand why multiplying the width of the ship divided by two by the angle would help at all to be honest.

##### Share on other sites

[attachment=36187:bullet.png]

Let the circle be the position of the bullet when firing. The center is ship_x, ship_y, and you want the correct starting position of the bullet. You have the angle your ship is turned at (alpha in the image).

So to get the bullets position you need to rotate the offset (which I measured as half of the ships width) about alpha. That's what the calculation is for:

( ship_width / 2 ) * cosf( angle )   gives the X offset from the ships position

( ship_width / 2 ) * sinf( angle )   gives the Y offset from the ships position

As said before, this formula only works correct, if angle = 0 means the ship is facing right, and a positive angle turns counter clock wise.

If the ship is orientated differently you have to offset angle in the sinf/cosf calls.

If the rotation is the wrong way you negativate the angle in sinf/cosf.

If your angle is in degrees you have to convert them to radian (multiply by 3.1415926f / 180.0f) for sinf/cosf.

##### Share on other sites

bullet.png

Let the circle be the position of the bullet when firing. The center is ship_x, ship_y, and you want the correct starting position of the bullet. You have the angle your ship is turned at (alpha in the image).

So to get the bullets position you need to rotate the offset (which I measured as half of the ships width) about alpha. That's what the calculation is for:

( ship_width / 2 ) * cosf( angle ) gives the X offset from the ships position

( ship_width / 2 ) * sinf( angle ) gives the Y offset from the ships position

As said before, this formula only works correct, if angle = 0 means the ship is facing right, and a positive angle turns counter clock wise.

If the ship is orientated differently you have to offset angle in the sinf/cosf calls.

If the rotation is the wrong way you negativate the angle in sinf/cosf.

If your angle is in degrees you have to convert them to radian (multiply by 3.1415926f / 180.0f) for sinf/cosf.

Even when i convert the degrees to radians the laser is still off. I think this is because you though that x and y were the center of the bullet rect, but it is actually the upper left-hand corner. I tried adding 1/2 ship_width to the offset x but this doesnt help. How should I adjust my code? Thanks.

##### Share on other sites

Is the bullet that big? It shouldn't make much of a difference, here what it ought to look then. I also added the angle offset, as you said you hat 0 aiming up and 180 down.

float dx = ( ship_width / 2 ) * cosf( 3.1415926f / 180.0f * ( angleInDegrees - 90 ) );
float dy = ( ship_width / 2 ) * sinf( 3.1415926f / 180.0f * ( angleInDegrees - 90 ) );
x = ship_x + dx - width / 2;
y = ship_y + dy - height / 2;

1. 1
2. 2
Rutin
21
3. 3
4. 4
A4L
15
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633736
• Total Posts
3013604
×