Advertisement
Guest User

Untitled

a guest
Apr 20th, 2015
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1.  
  2. --# Main
  3. -- RailJoint
  4.  
  5. -- Use this function to perform your initial setup
  6. function setup()
  7. bodies = {}
  8. joint = nil
  9. parameter.action("Scene 1",scene1)
  10. parameter.action("Scene 2",scene2)
  11. parameter.action("Scene 3",scene3)
  12. fps = 60
  13. end
  14.  
  15.  
  16.  
  17. function clear()
  18. for k,v in pairs(bodies) do
  19. bodies[k]=nil
  20. end
  21. collectgarbage("collect")
  22. bodies = {}
  23. joint = nil
  24. end
  25.  
  26. function scene1()
  27. clear()
  28. local points = {}
  29. for i=1,61 do
  30. points[i] = vec2(WIDTH/2,HEIGHT/2)+vec2(math.sin(math.rad(i*6))*300,math.cos(math.rad(i*6))*300)
  31. end
  32. bodies[1] = Cart(points[1])
  33. joint = RailJoint(points,bodies[1].body)
  34. joint:setCV(-300,0.9)
  35. joint:setFriction(10)
  36. end
  37.  
  38. function scene2()
  39. clear()
  40. local points = {}
  41. for i=1,241 do
  42. points[i] = vec2(WIDTH/2,HEIGHT/2)+vec2(math.sin(math.rad(i*3))*50+math.cos(math.pi/2+math.rad(i*12))*30,
  43. math.cos(math.rad(i*3))*300)
  44. end
  45. bodies[1] = Cart(points[1])
  46. joint = RailJoint(points,bodies[1].body)
  47. end
  48.  
  49. function scene3()
  50. clear()
  51. local points = {}
  52. for i=1,361 do
  53. points[i] = vec2(WIDTH/2,HEIGHT/2)+vec2(math.sin(math.rad(i*2))*300,math.cos(math.rad(i*3))*300)
  54. end
  55. bodies[1] = Cart(points[1])
  56. bodies[2] = Cart(points[50])
  57. bodies[3] = Cart(points[100])
  58. bodies[4] = Cart(points[150])
  59. bodies[5] = Cart(points[200])
  60. joint = RailJoint(points,unpackbodies(bodies))
  61. joint:setCV(-100,0.9)
  62. end
  63.  
  64. function unpackbodies(tbl)
  65. local btbl = {}
  66. for k,v in pairs(tbl) do
  67. btbl[k] = v.body
  68. end
  69. return unpack(btbl)
  70. end
  71. function draw()
  72. background(40, 40, 50)
  73. strokeWidth(5)
  74. fps=fps*0.95+(1/DeltaTime)*0.05
  75. text(fps,100,100)
  76. for k,v in pairs(bodies) do
  77. v:draw()
  78. end
  79. if joint then joint:draw() end
  80. end
  81.  
  82.  
  83. --# Cart
  84. Cart = class()
  85.  
  86. function Cart:init(pos)
  87. self.body = physics.body(POLYGON,vec2(-30,-20),vec2(-30,20),vec2(30,20),vec2(30,-20))
  88. self.body.position = pos
  89. end
  90.  
  91. function Cart:draw()
  92. pushMatrix()
  93. translate(self.body.x,self.body.y)
  94. rotate(self.body.angle)
  95. sprite("Cargo Bot:Register Slot",0,0,60,40)
  96. popMatrix()
  97. end
  98.  
  99. --# RailJoint
  100. RailJoint = class()
  101.  
  102. function RailJoint:init(points,...)
  103. self.points = points
  104. local args = {...}
  105. self.bodies = {}
  106. for k,v in pairs(args) do
  107. self.bodies[k] = {}
  108. self.bodies[k].body = v
  109. v.linearDamping=0.05
  110. end
  111. self.m = mesh()
  112. self.r = {}
  113. for i=2,#self.points do
  114. local p1,p2 = self.points[i-1],self.points[i]
  115. local ang,pos,len = math.rad(angleOfPoint(p1-p2)),(p1+p2)/2,p1:dist(p2)
  116. self.r[i] = self.m:addRect(pos.x,pos.y,len,2,ang)
  117. end
  118. self.conveyor = nil
  119. end
  120.  
  121. function RailJoint:setCV(speed,stiffness)
  122. self.conveyor = {speed,stiffness}
  123. end
  124.  
  125. function RailJoint:setFriction(f)
  126. for k,v in pairs(self.bodies) do
  127. v.body.linearDamping = f
  128. end
  129. end
  130.  
  131. function RailJoint:cp(body)
  132. local closest = {math.huge,0,nil,nil,nil}
  133. local cart = body.body
  134. local tpos = cart.position
  135. for i=2,#self.points do
  136. local p1,p2 = self.points[i-1],self.points[i]
  137. local cp = cpol(tpos,p1,p2)
  138. if closest[1]>cp:dist(tpos) then
  139. closest = {cp:dist(tpos),i,cp,p1,p2}
  140. end
  141. end
  142. local pos = closest[3]
  143. local p1,p2 = closest[4],closest[5]
  144. local dl = (pos-tpos):normalize()
  145. local clp = cart.linearVelocity
  146. cart:applyForce(cart.mass*((pos-tpos)*32-(dl:dot(vec2(clp.x,clp.y))*dl))*1)
  147. --while cart.angle>360 do cart.angle=cart.angle-360 end
  148. --while cart.angle<0 do cart.angle=cart.angle+360 end
  149. local cang = angleOfPoint(p2-p1)-cart.angle
  150. while cang>180 do cang = cang-360 end
  151. while cang<-180 do cang = cang+360 end
  152. cart.angularVelocity = cart.angularVelocity+(cart.mass*(cang*32-cart.angularVelocity))/2
  153. end
  154.  
  155. function RailJoint:draw()
  156. self.m:draw()
  157. for k,v in pairs(self.bodies) do
  158. self:cp(v)
  159. if self.conveyor then
  160. local sd,sf = self.conveyor[1],self.conveyor[2]
  161. v.body:applyForce(vec2(sd,0):rotate(math.rad(v.body.angle)))
  162. v.body.linearVelocity = v.body.linearVelocity*sf
  163. end
  164. end
  165. end
  166.  
  167. function RailJoint:touched(touch)
  168. -- Codea does not automatically call this method
  169. end
  170.  
  171. function cpol(p,a,b)
  172. local c = p - a;
  173. local v = (b - a):normalize()
  174. local d = (b - a):len();
  175. local t = v:dot(c)
  176. if t < 0 then return a end
  177. if t > d then return b end
  178. v = v * t
  179. return a + v;
  180. end
  181. function angleOfPoint(pt) return math.deg(math.atan2(pt.y,pt.x)) end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement