Public Group

# How to make spread when shooting? and rotate bullets correctly?

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

## Recommended Posts

Hi,
I learn c++ and making a small game in the process.. I'm not very good at this but getting better slowly...

I have a character that shoots bullets towards the cursor and I wanted to make a weapon that when shooting the bullets will spread a little, not just fire in a line.

So I did generate random point in area around the cursor and points bullet to that point. It works, but the amount of spread is not constant and gets wider as the cursor getting closer to the player. This is not desired behavious... Is there any way how you can do it so the spread will be the same no matter where the cursor is?

    spread.x = (rand()%spread_value)-spread_value/2+xdir;
spread.y = (rand()%spread_value)-spread_value/2+ydir; 

this is how I do it now.. spread_value is how wide the spread should be and xdir, ydir is the x,y of the cursor (direction where shooting)

I also have one more problem that I wonder how to fix and thats with the bullets as seen above rotate towards the cursor (so they no longer face the direction where they are traveling) ...its not that important but I wonder how to fix that...

this is how I calculate the rotation of the bullet currently:

bullet_rot = -atan2((x_direction-x_position),(y_direction-y_position))-M_PI/2;

x,y_position is the position of the player, x,y_direction is the cursor values

btw I use HGE (Haaf's Game Engine and C++)

##### Share on other sites

Take the vector from the gun position to the cursor position and normalise it (ie, make it a constant length), that will solve the first issue.

For the rotation, i dont really get what you mean. Do you mean, a rotation like a real bullet? If so, just rotate it before doing others transformations to position the bullet.

##### Share on other sites

I solved the bullet rotation problem.. I did calculate the rottation towards the cursor everytime in my update function and that was the problem. Once I moved that line into constructor and deleted it from update function it set the rotation of the bullet only once when it creates a new bullet and dont update it everytime. (Well.. that was easy)

but I don't quite understand what should I do with that normalized vector...

##### Share on other sites

I think the idea with the normalized vector is to spawn points around that vector in an area, normalize it again and you have your direction vector for the bullet. And that will give constant spread.

A simplified way of doing the same thing is to store the direction you're shooting in, add some random offset and point the bullet in that direction.

##### Share on other sites

If you want a spread pattern that is independent of the distance from the cursor, compute the angle to the cursor that you're firing the bullets towards and apply a small randomized +/- offset to that angle for each bullet fired.  The maximum allowed offset will dictate how much spread you have, and it won't matter whether the cursor is right in front of the gun or on the other side of the screen.

##### Share on other sites

If you want a spread pattern that is independent of the distance from the cursor, compute the angle to the cursor that you're firing the bullets towards and apply a small randomized +/- offset to that angle for each bullet fired.  The maximum allowed offset will dictate how much spread you have, and it won't matter whether the cursor is right in front of the gun or on the other side of the screen.

If you mean angles by using radians/degrees, wouldn't that require a sin/cos calculation each and every time you create a bullet, which is fairly expensive?

I believe it's easier to do what Vortez said. Calculate the direction vector from player pos to cursor and normalize it, then multiply it by a constant distance, and keep everything else exactly the same. Basically, pretend the cursor is always at a certain distance from the player, and don't change any of the existing code.

normCur = normalize(cursorPos);
xdir = some_constant_for_distance*normCur.x;
ydir = some_constant_for_distance*normCur.y;

spread.y = (rand()%spread_value)-spread_value/2+ydir;
Edited by Strewya

##### Share on other sites

Thanks!

I managed to do what Strewya suggested

    cursor.x = xdir - xpos;
cursor.y = ydir - ypos;
cursor.Normalize();
xdir = 500 * cursor.x;
ydir = 500 * cursor.y;


?

?

this is the code that actually worked

spread is now independent of the cursor position while still shooting where the cursor is pointing

x,ydir is the world position of the cursor

x,ypos is the world position of player

I was unable to implement the angle solution.. managed to get the angle but was unable to get the new cursor position from that angle.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 13
• 9
• 11
• 15