Sign in to follow this  
mypel16000

Shooting in SFML

Recommended Posts

mypel16000    14

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


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


Link to post
Share on other sites
mypel16000    14

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


Link to post
Share on other sites
stitchs_login    1361

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


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


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


Link to post
Share on other sites
mypel16000    14

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


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


Link to post
Share on other sites
stitchs_login    1361

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


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


Link to post
Share on other sites
mypel16000    14

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


Link to post
Share on other sites
slicer4ever    6769

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.

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