0
\$\begingroup\$

I am trying to give an object a velocity spread (e.g. shotgun bullets or particle effect).

//dir is the initial direction
float angle1 = atan(dir.x, dir.z) + spread;
float angle2 = atan(dir.y, abs(dir.z)) + spread;
vec3 xz_dir = vec3(sin(angle1, 0.0, cos(angle2));
vec3 yz_dir = vec3(0.0, sin(angle2), cos(angle2));
vec3 new_dir = vec3(xz_dir.x * yz_dir.z, yz_dir.y, xz_dir.z * yz_dir.z);
new_dir.normalize();

The new_dir spread works fine in the XY and YZ planes but if there is a Y component in the direction, the spread along the XY plane doesn't work.

\$\endgroup\$
  • \$\begingroup\$ Can this question provide the solution to your issue? \$\endgroup\$ – Alexandre Vaillancourt Jul 6 at 1:31
  • \$\begingroup\$ I am trying to work with Euler angles. I grab two angles from the direction XZ plane and YZ plane and then add a random spread. Then I convert them back into two vectors. Then I combine the two vectors to get the new direction with the added spread. If I also add a random spread in the XY plane by first grabbing the angle, add a spread, convert to vector: float angle3 = atan(dir.y, dir.x) + spread; vec3 xy_dir vec3(cos(angle1_rad), sin(angle1_rad), 0.0); How would I add this xy vector to the other two vectors? \$\endgroup\$ – sparkart Jul 6 at 7:24
  • 2
    \$\begingroup\$ Euler angles will not give you the results you want here. As illustrated here, stacking ranges of Euler angles tends to make odd shapes that are not what you intended or predicted from looking at your math. I'd strongly recommend throwing away Euler angles and trying a fresh approach for bullet spread. \$\endgroup\$ – DMGregory Jul 6 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.