• Create Account

## Shooting in SFML

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

13 replies to this topic

### #1mypel16000  Members

14
Like
0Likes
Like

Posted 02 April 2013 - 01:15 PM

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?

5832
Like
1Likes
Like

Posted 02 April 2013 - 01:32 PM

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

### #3mypel16000  Members

14
Like
0Likes
Like

Posted 02 April 2013 - 01:36 PM

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!

5832
Like
1Likes
Like

Posted 02 April 2013 - 01:42 PM

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

### #5stitchs  Members

1361
Like
0Likes
Like

Posted 02 April 2013 - 02:09 PM

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.

### #6FLeBlanc  Members

3133
Like
0Likes
Like

Posted 02 April 2013 - 02:12 PM

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.

5832
Like
0Likes
Like

Posted 02 April 2013 - 02:14 PM

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

### #8mypel16000  Members

14
Like
0Likes
Like

Posted 02 April 2013 - 02:32 PM

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


### #9stitchs  Members

1361
Like
0Likes
Like

Posted 02 April 2013 - 02:33 PM

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.

### #10Arhim  Members

228
Like
0Likes
Like

Posted 02 April 2013 - 02:42 PM

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?

### #11stitchs  Members

1361
Like
0Likes
Like

Posted 02 April 2013 - 02:46 PM

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.

### #12Arhim  Members

228
Like
2Likes
Like

Posted 02 April 2013 - 02:55 PM

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).

### #13mypel16000  Members

14
Like
0Likes
Like

Posted 02 April 2013 - 02:58 PM

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!

### #14slicer4ever  GDNet+

6360
Like
0Likes
Like

Posted 03 April 2013 - 12:45 AM

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.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.