• Advertisement
Sign in to follow this  

SDL rotate rect evenly?

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites



attachicon.gifbullet.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 this post


Link to post
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;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Advertisement