# Shooting in SFML

## Recommended Posts

I have used this method to implement Projectile movement into my shooting game:

Dlen = sqrt(Dx*Dx + Dy*Dy)

Dx/=Dlen;

Dy/=Dlen

Bx=Bx+Dx*Bs;

By=By+Dy*Bs;

*Dlen = normalised value, Dx = xOffset, Dy = yOffset, Bx= bulletX, By= bulletY, Bs = bullet Speed.

The problem is that this function changes the speed of the projectile depending on how close the mouse (aiming point) is to the player (shooting point). It also seems like it draws 6 separate bullets as it doesn't move it smoothly. Is there any other method I can use to shoot projectiles from the player (in a top-down 2D shooter) towards the mouse?

##### Share on other sites

Didn't you ask the exact same question the other week?

Are you using floating point values for the velocities? Because it ain't gonna work properly if you use integers due to rounding.

##### Share on other sites

No, it was different, I didn't know how to use this method.

They are floats, but that's not the point. The point is that the closer you get the mouse to the player (as the distance gets smaller), the slower the projectiles go. But when the distance is larger, the projectiles go so fast, you only see like 3 images in the screen of the bullet sprite because its so quick!

##### Share on other sites

Whn you say Dlen is normalised, you're not normalising (Dx, Dy) before you calculate sqrt(Dx*Dx + Dy * Dy) are you? Because that would not be good.

##### Share on other sites

I'm only just learning Vector Math, so I may be a bit off here.

Is mypel16000 not normalising? I thought he did it when he divides Dx and Dy (vector components) each by Dlen (magnitude). Does that not bring each value in the range of 0 - 1? Or are you commenting that he is prematurely labelling Dlen as 'normalised', which does not happen until after the divisions.

Regards,

Stitchs.

##### Share on other sites
FLeBlanc    3141
You should probably show us your actual code. In the pseudocode you posted, your vector normalization looks okay, but your description of the problem leads me to believe that you're doing something wrong in the code.

##### Share on other sites

If he normalises (Dx, Dy) before working out the length and dividing, it's always going to be length 1 (ish). That would be ok if he doesn't use (previous values of) Dx, Dy for any calculations...

Really we need to see more code.

##### Share on other sites

Here's my code:

void Projectile::shot_CheckDirection(sf::RenderWindow& window, Player& player, int mouseX, int mouseY)
{
bulletX = (int)player.playerSprite.GetPosition().x ;
bulletY = (int)player.playerSprite.GetPosition().y ;

shot_XOffset = mouseX -( bulletX );
shot_YOffset = mouseY -( bulletY );
shot_RotationAngle = (atan2(shot_XOffset, shot_YOffset));
bulletSprite.SetRotation( (shot_RotationAngle * 180 / 3.14159265) );

shot_Length= sqrt(shot_XOffset*shot_XOffset + shot_YOffset*shot_YOffset);
}

void Projectile::shot_Move          (sf::RenderWindow& window)
{
move_XOffset = (int)shot_XOffset;
move_YOffset = (int)shot_YOffset;

move_XOffset /= shot_Length;
move_YOffset /= shot_Length;

bulletX += ((shot_XOffset*0.1))    ;
bulletY += ((shot_YOffset*0.1))   ;

bulletSprite.SetPosition((int)bulletX, (int)bulletY);
window.Draw(bulletSprite);
frameCounter++;
}


##### Share on other sites

I see, so in order to get the true value, it IS only normalised once each component has been divided by the magnitude. So it is premature labeling.. I would also be interested to see a code-snippet.

##### Share on other sites
Aristotel    228

Shouldn't there be a constant like speed with which you multiply the offsets each frame and not by taking a percentage by multiplying them with 0.1?

##### Share on other sites

Is this not where the problem lies:

move_XOffset = (int)shot_XOffset;
move_YOffset = (int)shot_YOffset;


By casting the shot offsets to Integer, are you losing all the decimal precision you just calculated and rounding everything down to zero?

What are the data-types of each of your variables?

Stitchs.

##### Share on other sites
Aristotel    228

Actually, if you look at the code, the only place where the bullets position is updated is this:

bulletX += ((shot_XOffset*0.1)) ;
bulletY += ((shot_YOffset*0.1)) ;


but shot_XOffset and shot_YOffset are calculated like this:

shot_XOffset = mouseX -( bulletX );
shot_YOffset = mouseY -( bulletY );


This makes the bullet move 10% of the distance between the mouse pointer and the origin position, which means it is not constant if you change the mouse position (hence the faster and slower movement).

##### Share on other sites

OH my GOD!! Thank you Arhim, if you look at my code, the plan was to write

bulletY += ((move_YOffset*0.1)) ;

y no

bulletY += ((shot_YOffset*0.1)) ;

Just a silly coding mistake... THANKS EVERYONE!

##### Share on other sites
slicer4ever    6760

mypel16000, on 02 Apr 2013 - 17:02, said:
OH my GOD!! Thank you Arhim, if you look at my code, the plan was to write

bulletY += ((move_YOffset*0.1)) ;

y no

bulletY +=((shot_YOffset*0.1));

Just a silly coding mistake... THANKS EVERYONE!

are move_XOffset and move_YOffset integers(since your casting shot_(X/Y)Offset as integers, i'm assuming yes), because your going to run into tons of problem if they are.