Author Topic: Round weapon spread?  (Read 1407 times)

So I'm trying to make a weapon have round spread specifically instead of square spread. I've tried several different weapon packs and each one of them seems to have square spread (including Gravity Cat's weapons and several other weapon packs).

I can visualize how this would work, you start out with a unit vector like (1, 0, 0) and then rotate it a bit up, and then a random amount around to get your "spread" vector. Then you take that vector and rotate it as if it was still (1, 0, 0) all the way to the muzzle vector, preserving the angle between it and the spread vector, and you have your final direction vector.

However I have no idea how this would be translated into code and every time I've tried it's gone horribly wrong. Any help would be appreciated!

http://stackoverflow.com/questions/5837572/generate-a-random-point-within-a-circle-uniformly
the person who answered it explains it a lot better than i ever could've
on second thought that's not enough information for your purposes, isn't it
i wouldn't know how to actually get the z-axis working so sorry

also if anyone knows how to get a graphing calculator like mathematica please say. i'm on my phone right now and can't really tell how to use it
« Last Edit: December 22, 2015, 12:54:09 AM by Gytyyhgfffff »

« Last Edit: December 22, 2015, 11:53:47 AM by Swollow »

Something with $pi, mCos and mSin.

I remember my radar code did something like this

Bump because this is still and open question that I haven't been able to figure out. Does anyone know how one could do this?

You want to create projectiles in a square, right?

You want to create projectiles in a square, right?
No, I want it to be round. IE, if you were to stand back from a wall and shoot straight forward at that wall, you'd see a circular pattern from the spots that the bullets landed instead of square. The current method most addons use to create spread is this:
%velocity = %muzzleVector;
[Set %x, %y, and %z to some sort of random numbers]
%matrix = MatrixCreateFromEuler(%x @ " " @ %y @ " " @ %z);
%velocity = vectorNormalize(MatrixMulVector(%matrix, %velocity));


Which, although a very efficient method of generating spread, makes a square pattern instead of a round one like I'm wanting.

I'm on my phone, so excuse me if there's any problems

Code: [Select]
function randomPointInCircle(%radius) {
  %angle = getRandom(0, 2*$pi);
  %magnitude = getRandom(0, %radius);
  %x = %magnitude * mCos(%angle);
  %y = %magnitude * mSin(%angle);
  return %x SPC %y;
}

I'm fairly certain torque uses radians for mSin and mCos. If not just replace 2*$pi with 360.

Now, that will just get a random point in a circle. For a circular spread, we can take a random point in a circle, say, 1 unit forward, and then find a vector from the muzzle to the circular plane.

Code: [Select]
function getRandomVector(%velocity, %radius, %dist) {
  %random = randomPointInCircle(%radius);
  %xAngle = mAtan(getWord(%random, 0)/%dist);
  %zAngle = mAtan(getWord(%random, 1)/%dist);
  %x = %velocity * mSin(%xAngle);
  %y = %velocity * mCos(%xAngle);
  %z = %velocity * mSin(%zAngle);
  return %x SPC %y SPC %z;
}

Now, this assumes your muzzle is facing north and is horizontal. I'm sure there's some sort of vector rotation function that you can use with both the random vector and the muzzle's rotation.

I'm fairly certain this should work but no guarantees. It should atleast point you in the right direction.
« Last Edit: December 27, 2015, 09:23:01 PM by Scout31 »

By the way %rand isn't used in getRandomVector, it only is defined.


Also, isn't the %zAngle supposed to get the 3rd word aka getWord(%blah, 2) since it's the z and not the y

No. A circle is a 2 dimensional shape, so getting a random point in a circle only gives an x and y coordinate. There is no 3rd word. Assuming the player is facing north (positive on the y-axis), that means a random point in the circle deviates our actual point on the x and z axes.


getRandom(a,b) gets a random integer between a and b (inclusive)

getRandom() gets a random decimal between 0 and 1 (inclusive?)

so in your "randomPointInCircle" function you should do the randoms like this

%angle = getRandom()*(2*$PI);
%magnitude = getRandom()*%radius;


also i initialled cooked something up like scout did but its rotation didn't work at all angles, I'm sure thats what ipquarx needs the help with the most
« Last Edit: December 28, 2015, 06:58:12 PM by Swollow »

also i initialled cooked something up like scout did but its rotation didn't work at all angles, I'm sure thats what ipquarx needs the help with the most
this is exactly it yes
I've tried pretty much all of the ones listed here and one of my own and they all work somewhat but they're all either unevenly distributed or change depending on the angle or both
the one i came up with is to change the xyz variables from random points in a cube to random points in a sphere, which does not change depending on what direction you're facing which is fantastic
the problem is that it seems to be unevenly distributed towards the middle, which I would say is to be expected. I just don't know how I'm supposed to make it even, or even if being biased towards the center is a good thing. It would be slightly more like a normal distribution where most is clustered around the center and less are out around the edges, but limited so that it can't go outside a certain radius.
« Last Edit: December 28, 2015, 07:25:15 PM by Ipquarx »