Advertisement
eri0w

Untitled

Apr 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. // new module script
  2.  
  3. __Rope Rope;
  4. export Rope;
  5.  
  6. struct SRopePoint
  7. {
  8. float x;
  9. float y;
  10. float ox;
  11. float oy;
  12. };
  13.  
  14. #define NSEG 32
  15.  
  16. SRopePoint rps[33];
  17. float x1, y1, x2, y2, elen, gravity, damping;
  18.  
  19. DynamicSprite *rope_sprite;
  20.  
  21. function __Rope::SetStartPoint(float x, float y)
  22. {
  23. x1 = x;
  24. y1 = y;
  25. }
  26.  
  27. function __Rope::SetEndPoint(float x, float y)
  28. {
  29. x2 = x;
  30. y2 = y;
  31. }
  32.  
  33. function __Rope::SetLength(float len)
  34. {
  35. elen = len/IntToFloat(NSEG);
  36. }
  37.  
  38. function __Rope::SetGravity(float g)
  39. {
  40. gravity = g;
  41. }
  42.  
  43. function __Rope::SetDamping(float d)
  44. {
  45. damping = d;
  46. }
  47.  
  48. function noloopcheck __Rope::Update()
  49. {
  50. float dt = 1.0/IntToFloat(GetGameSpeed());
  51.  
  52. // verlet step
  53. int i = 1;
  54. while (i < NSEG)
  55. {
  56. float x = rps[i].x;
  57. float y = rps[i].y;
  58. rps[i].x = ((2.0 - damping)*x - (1.0 - damping)*rps[i].ox);
  59. // gravity
  60. rps[i].y = ((2.0 - damping)*y - (1.0 - damping)*rps[i].oy) + gravity*dt*dt;
  61. rps[i].ox = x;
  62. rps[i].oy = y;
  63. i++;
  64. }
  65.  
  66. // satisfy constraints
  67. int it = 0;
  68. while (it < 2)
  69. {
  70. i = 0;
  71. while (i < NSEG)
  72. {
  73. float dx = rps[i+1].x - rps[i].x;
  74. float dy = rps[i+1].y - rps[i].y;
  75. float dlen = Maths.Sqrt(dx*dx + dy*dy);
  76. float df = (dlen - elen)/dlen;
  77. rps[i].x = rps[i].x + 0.5*df*dx;
  78. rps[i+1].x = rps[i+1].x - 0.5*df*dx;
  79. rps[i].y = rps[i].y + 0.5*df*dy;
  80. rps[i+1].y = rps[i+1].y - 0.5*df*dy;
  81. i++;
  82. }
  83. it++;
  84. }
  85.  
  86. // snap end points
  87. rps[0].x = x1;
  88. rps[0].y = y1;
  89. rps[NSEG].x = x2;
  90. rps[NSEG].y = y2;
  91. }
  92.  
  93.  
  94. function __Rope::GetGraphic()
  95. {
  96. if(rope_sprite!=null){
  97. return rope_sprite.Graphic;
  98. } else {
  99. return -1;
  100. }
  101. }
  102.  
  103. function __Rope::Create(int rope_area_width , int rope_area_height)
  104. {
  105. if(rope_sprite!=null){
  106. rope_sprite.Delete();
  107. }
  108. rope_sprite = DynamicSprite.Create(rope_area_width, rope_area_height, true);
  109. //bg = DynamicSprite.CreateFromBackground();
  110.  
  111. float nseg = IntToFloat(NSEG);
  112. float dx = (x2 - x1)/nseg;
  113. float dy = (y2 - y1)/nseg;
  114. float x = x1;
  115. float y = y1;
  116. int i = 0;
  117. while (i <= NSEG)
  118. {
  119. rps[i].x = x;
  120. rps[i].y = y;
  121. rps[i].ox = x;
  122. rps[i].oy = y;
  123. x = x + dx;
  124. y = y + dy;
  125. i++;
  126. }
  127.  
  128. // run a second or two to settle
  129. int f = 0;
  130. while (f < 300)
  131. {
  132. Rope.Update();
  133. f++;
  134. }
  135. }
  136.  
  137. function __Rope::Render()
  138. {
  139. DrawingSurface *surf = rope_sprite.GetDrawingSurface();
  140. surf.DrawingColor = COLOR_TRANSPARENT;
  141. surf.Clear();
  142.  
  143. surf.DrawingColor = 65535;
  144. int i = 0;
  145. while (i < NSEG)
  146. {
  147. DrawAntialiasedLine(surf, rps[i].x, rps[i].y, rps[i+1].x, rps[i+1].y, 10);
  148. i++;
  149. }
  150. surf.Release();
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement