Public Group

# Shooting in SFML

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

## 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
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

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?

1. 1
Rutin
26
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 9
• 9
• 9
• 46
• 41
• ### Forum Statistics

• Total Topics
631750
• Total Posts
3002059
×