Shooting in SFML

Started by
12 comments, last by slicer4ever 11 years ago

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?

Advertisement

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.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

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!

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.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

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.

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.

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.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

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++;
}

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.

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?

This topic is closed to new replies.

Advertisement