Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. /// @description smf_sphere_avoid_block(blockMatrix, x, y, z, radius)
  2. /// @param blockMatrix[16]
  3. /// @param x
  4. /// @param y
  5. /// @param z
  6. /// @param radius
  7. /*
  8. Makes the sphere (x, y, z) avoid the given block.
  9. The block matrix can be created with matrix_build(x, y, z, xrot, yrot, zrot, xscale, yscale, zscale), where:
  10. (x, y, z) are the center coordinates of the cube,
  11. (xrot, yrot, zrot) are the euler angles of the cube, and
  12. (xscale, yscale, zscale) are half the side lengths of the cube (so the cube is actually twice as large as these values)
  13.  
  14. Returns an array of the following format:
  15. [x, y, z, xup, yup, zup, collision (true or false)]
  16.  
  17. Script made by TheSnidr
  18. www.TheSnidr.com
  19. */
  20. var bM;
  21. bM = argument0;
  22.  
  23. var dx, dy, dz, R
  24. dx = argument1 - bM[12];
  25. dy = argument2 - bM[13];
  26. dz = argument3 - bM[14];
  27. R = argument4;
  28.  
  29. //The inverse of the square of the scale of each dimension
  30. var lx, ly, lz;
  31. lx = 1 / (sqr(bM[0]) + sqr(bM[1]) + sqr(bM[2]));
  32. ly = 1 / (sqr(bM[4]) + sqr(bM[5]) + sqr(bM[6]));
  33. lz = 1 / (sqr(bM[8]) + sqr(bM[9]) + sqr(bM[10]));
  34.  
  35. //Find normalized block space position
  36. var bx, by, bz, b;
  37. bx = (dx * bM[0] + dy * bM[1] + dz * bM[2]) * lx;
  38. by = (dx * bM[4] + dy * bM[5] + dz * bM[6]) * ly;
  39. bz = (dx * bM[8] + dy * bM[9] + dz * bM[10]) * lz;
  40. b = max(abs(bx), abs(by), abs(bz));
  41.  
  42. //Nearest point on the cube in normalized block space
  43. var cx, cy, cz;
  44. cx = clamp(bx, -1, 1);
  45. cy = clamp(by, -1, 1);
  46. cz = clamp(bz, -1, 1);
  47.  
  48. var d, nx, ny, nz, px, py, pz;
  49. //If the center of the sphere is outside the cube, check if there is an intersection. If there is, move the sphere out of the cube
  50. if (b > 1)
  51. {
  52. px = cx * bM[0] + cy * bM[4] + cz * bM[8];
  53. py = cx * bM[1] + cy * bM[5] + cz * bM[9];
  54. pz = cx * bM[2] + cy * bM[6] + cz * bM[10];
  55.  
  56. nx = dx - px;
  57. ny = dy - py;
  58. nz = dz - pz;
  59. d = sqr(nx) + sqr(ny) + sqr(nz);
  60. if d >= sqr(R){return [argument1, argument2, argument3, 0, 0, 0, false];}
  61. d = 1 / sqrt(d);
  62. nx *= d;
  63. ny *= d;
  64. nz *= d;
  65.  
  66. px += bM[12] + nx * R;
  67. py += bM[13] + ny * R;
  68. pz += bM[14] + nz * R;
  69. return [px, py, pz, nx, ny, nz, true];
  70. }
  71.  
  72. //If the center of the sphere is inside the cube, move it out at any cost
  73. if (b == abs(cx))
  74. {
  75. cx = sign(cx);
  76. d = cx * sqrt(lx);
  77. nx = bM[0] * d;
  78. ny = bM[1] * d;
  79. nz = bM[2] * d;
  80. }
  81. else if (b == abs(cy))
  82. {
  83. cy = sign(cy);
  84. d = cy * sqrt(ly);
  85. nx = bM[4] * d;
  86. ny = bM[5] * d;
  87. nz = bM[6] * d;
  88. }
  89. else// if (b == abs(cz))
  90. {
  91. cz = sign(cz);
  92. d = cz * sqrt(lz);
  93. nx = bM[8] * d;
  94. ny = bM[9] * d;
  95. nz = bM[10] * d;
  96. }
  97.  
  98. px = cx * bM[0] + cy * bM[4] + cz * bM[8] + bM[12] + nx * R;
  99. py = cx * bM[1] + cy * bM[5] + cz * bM[9] + bM[13] + ny * R;
  100. pz = cx * bM[2] + cy * bM[6] + cz * bM[10] + bM[14] + nz * R;
  101. return [px, py, pz, nx, ny, nz, true];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement