• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

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

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.

6 replies to this topic

### #1Killerko  Members

Posted 06 March 2014 - 11:56 AM

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

### #2Vortez  Members

Posted 06 March 2014 - 12:08 PM

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.

### #3Killerko  Members

Posted 06 March 2014 - 12:51 PM

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

### #4TheUnnamable  Members

Posted 06 March 2014 - 01:02 PM

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.

Posted 06 March 2014 - 02:51 PM

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.

### #6Strewya  Members

Posted 07 March 2014 - 08:28 AM

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, 07 March 2014 - 03:18 PM.

devstropo.blogspot.com - Random stuff about my gamedev hobby

### #7Killerko  Members

Posted 08 March 2014 - 03:27 AM

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.

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.