Laser script, request von dede165

    • Skript

    Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

    • Laser script, request von dede165

      @dede165
      Hiermit stelle ich mein Script samt Demo.gmz zur Verfügung. Das Script stellt einen einfachen Weg bereit, Laser bzw Linien mit variabler Länge und Breite sowie Farbe malen zu lassen, dabei wird der Abprallwinkel bei Kollision mit einem Objekt automatisch mit einberechnet, so dass insgesamt nur ein Scriptaufruf nötig ist. Es sollte beachtet werden, dass die Rechenintensität davon abhängt, wie oft der Abprallwinkel neu berechnet werden muss.

      Unterstützt wird das Abprallen auch von Formen jenseits einer Geraden, allerdings wird wahrscheinlich nicht jede crazy Form das gewünschte Ergebnis erziehlen.

      Das Script ist sicher nicht voll optimiert & wenn ihr Verbesserungsvorschläge habt, seid so nett und postet sie unter diesen Thread.
      Credits sind nicht nötig, allerdings bin ich auch nicht böse drum. Benutzt es frei für eueren Nutzen in Projekten und ändert es zu euren Nöten, gebt ihr allerdings aus ihr hättet es selbst erstellt, dann kommt der Knecht Rubrecht mit der Wünschelrute! :deal:

      ursprüngliche Fragestellung > Wie berechne ich den Winkel eines reflektierten Lasers ?

      SCRIPT:
      Spoiler anzeigen

      GML-Quellcode

      1. ///collision_beam_bounce(x,y,direction,max dist,object,col1,col2,width,alpha,collison radius,collision resolution)
      2. // - by Rhazul
      3. /*
      4. * x = start xpos
      5. * y = start ypos
      6. * direction = direction of the line
      7. * max dist = max dist to check
      8. * object = instance to check for collision
      9. * col1 = 1 colour of the beam
      10. * col2 = 2 colour of the beam
      11. * width = width of the beam
      12. * alpha = draw alpha value (set to 0 to disable draw)
      13. * collision radius = area to check for angle transformation on collision with object >= 1 (def. ~ 4, used for surface normal)
      14. * collision resolution = resolution of collision angle check >= 1 (def. ~ 1, used for surface normal)
      15. */
      16. // - Draws a line with given length, thickness, alpha and colour from x,y towards direction and reflects it from object.
      17. /// NOTE : this goes into the draw event. The provided object's sprite should have precise collision cheching enabled.
      18. var _x2,_x1,_y2,_y1,_dir,_dist,_obj,_col1,_col2,_wdh,_alph,_res,_rad,_normal;
      19. _x2 = argument0;
      20. _x1 = _x2;
      21. _y2 = argument1;
      22. _y1 = _y2;
      23. _dir = argument2;
      24. _dist = argument3;
      25. _obj = argument4;
      26. _col1 = argument5;
      27. _col2 = argument6;
      28. _wdh = argument7;
      29. _alph = clamp(argument8,0,1);
      30. _rad = max(1,argument9);
      31. _res = max(1,argument10);
      32. _normal = 0;
      33. while(_dist > 0) {
      34. var _collision = noone;
      35. var _length = 0;
      36. repeat(_dist) {
      37. var _xtry,_ytry;
      38. _xtry = _x2 + lengthdir_x(1,_dir);
      39. _ytry = _y2 + lengthdir_y(1,_dir);
      40. _collision = collision_point(_xtry,_ytry,_obj,true,true);
      41. _length++;
      42. _x2 = _xtry;
      43. _y2 = _ytry;
      44. if (_collision != noone) {break;}
      45. }
      46. if (_alph > 0) {
      47. _alph -= (_length/_dist)*.75; // This will decrease alpha based on length, comment out if not desired
      48. var _alphaPrev = draw_get_alpha();
      49. draw_set_alpha(_alph);
      50. draw_line_width_colour(_x1,_y1,_x2,_y2,_wdh,choose(_col1,_col2),choose(_col1,_col2));
      51. draw_set_alpha(_alphaPrev);
      52. }
      53. if (_collision != noone) {
      54. _dist = abs(_dist - _length);
      55. if (_dist < 0) {return noone;}
      56. _x1 = _x2;
      57. _y1 = _y2;
      58. var _xx,_yy_rad,_res,_nx,_ny,_i,_j;
      59. _xx = _x2;
      60. _yy = _y2;
      61. _nx = 0;
      62. _ny = 0;
      63. if (collision_circle(_xx,_yy,_rad,_obj,true,true)) {
      64. for (_j = _res; _j <= _rad; _j += _res) {
      65. for (_i = 0; _i < _rad; _i += _res) {
      66. if (point_distance(0,0,_i,_j) <= _rad) {
      67. if (!collision_point(_xx + _i,_yy + _j,_obj,true,true)) { _nx += _i; _ny += _j; }
      68. if (!collision_point(_xx + _j,_yy - _i,_obj,true,true)) { _nx += _j; _ny -= _i; }
      69. if (!collision_point(_xx - _i,_yy - _j,_obj,true,true)) { _nx -= _i; _ny -= _j; }
      70. if (!collision_point(_xx - _j,_yy + _i,_obj,true,true)) { _nx -= _j; _ny += _i; }
      71. }
      72. }
      73. }
      74. if (_nx = 0 && _ny = 0) {
      75. _normal = 0;
      76. }else{
      77. _normal = point_direction(0,0,_nx,_ny);
      78. }
      79. }else{
      80. _normal = 0;
      81. }
      82. var _ndir = (_normal*2) - _dir + 180;
      83. if (abs(_ndir) = _dir) {
      84. return noone;
      85. }else{
      86. _dir = _ndir;
      87. }
      88. }else{
      89. return noone;
      90. }
      91. }
      Alles anzeigen



      Demo.gmz > DOWNLOAD
      132 little bugs in the code. 132 little bugs. Fix a few, set the compiler to stew, 172 little bugs in the code... :vogel:

      Dieser Beitrag wurde bereits 3 mal editiert, zuletzt von Rhazul ()