Advertisement
Dermotb

Ray2a

Apr 3rd, 2013
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. --# Main
  3. -- Main
  4.  
  5. function setup()
  6. t=Track2()
  7. parameter.boolean("Rays",true)
  8. end
  9.  
  10. function draw()
  11. t:draw()
  12. end
  13.  
  14. function touched(touch)
  15. t:touched(touch)
  16. end
  17. --# Track1
  18. Track1 = class()
  19.  
  20. function Track1:init(x)
  21. self.car={x=350,y=100}
  22. self.FOV=75 --degrees each side
  23. self.slice=1 --degrees per view
  24. end
  25.  
  26. function Track1:draw()
  27. background(128)
  28. spriteMode(CORNER)
  29. --create new shapes every second or sl
  30. if counter==nil then counter=0 end
  31. counter = counter + 1
  32. if counter%60==1 then --every 60 redraws, or about one second
  33. screen=image(WIDTH,HEIGHT) --draw on an image the same size as the screen
  34. setContext(screen)
  35. fill(0,0,255,100)
  36. for i=1,3 do
  37. rect(self.car.x+math.random(-200,200),self.car.y+math.random(100,500),
  38. math.random(10,100),math.random(10,100))
  39. ellipse(self.car.x+math.random(-200,200),self.car.y+math.random(100,500),math.random(10,100))
  40. end
  41. setContext()
  42. views=self:GetRays(screen)
  43. end
  44. sprite(screen,0,0)
  45. fill(255,0,0)
  46. ellipse(self.car.x,self.car.y,20)
  47. stroke(0,100,200,200)
  48. strokeWidth(1)
  49. if views~=nil then
  50. for i,v in ipairs(views) do
  51. line(self.car.x,self.car.y,v.x,v.y)
  52. end
  53. end
  54. end
  55.  
  56. function Track1:GetRays(s)
  57. local rays={} --to store results
  58. local f=math.pi/180 --to convert degrees to radians
  59. for i=-self.FOV,self.FOV,self.slice do
  60. local d,dy,dy=0,0,0 --distance to object, and x,y position of object
  61. for u=1,1000 do --put a limit ont he looping
  62. local xx=self.car.x+u*math.sin(i*f) --step forward
  63. local yy=self.car.y+u*math.cos(i*f)
  64. --if in bounds, and pixel non blank, we've hit something
  65. if xx>1 and xx<=WIDTH and yy>1 and yy<=HEIGHT then
  66. local r,g,b,a=s:get(xx,yy)
  67. if b~=0 then d=u dx=xx dy=yy break end
  68. else break end
  69. end
  70. --store angle, distance and x,y position of object
  71. if d>0 then table.insert(rays,{angle=i,dist=d,x=dx,y=dy}) end
  72. end
  73. return rays
  74. end
  75.  
  76. function Track1:touched(touch)
  77. --dummy for now
  78. end
  79. --# Track2
  80. Track2 = class()
  81.  
  82. function Track2:init(x)
  83. self.car={x=25,y=50,speed=4,angle=0}
  84. self.FOV=75 --degrees each side
  85. self.slice=5 --degrees per view
  86. self.trackOffset={x=50,y=50}
  87. self:CreateTrack()
  88. end
  89.  
  90. function Track2:draw()
  91. --assign temporary short names just to make code easier to read
  92. local tx,ty=self.trackOffset.x,self.trackOffset.y
  93. background(128)
  94. pushStyle()
  95. spriteMode(CORNER)
  96. sprite(imgTrack,tx,ty)
  97. fill(255,0,0)
  98. ellipse(tx+self.car.x,ty+self.car.y,12)
  99. stroke(0,100,200,200)
  100. strokeWidth(1)
  101. views=self:GetRays(imgTrack)
  102. if views~=nil and Rays then
  103. for i,v in ipairs(views) do
  104. line(tx+self.car.x,ty+self.car.y,tx+v.x,ty+v.y)
  105. end
  106. end
  107. popStyle()
  108. self:SetNewPosition()
  109. end
  110.  
  111. function Track2:CreateTrack()
  112. imgTrack=image(500,400)
  113. setContext(imgTrack)
  114. pushStyle()
  115. fill(0,0,0,0) --transparent
  116. stroke(201,201,201) --white
  117. self.TrackBlue=201 --define the track colour (blue)
  118. strokeWidth(50)
  119. rect(0,0,500,400)
  120. popStyle()
  121. setContext()
  122. end
  123.  
  124. function Track2:GetRays(s)
  125. local rays={} --to store results
  126. local f=math.pi/180 --to convert degrees to radians
  127. for i=self.car.angle-self.FOV,self.car.angle+self.FOV,self.slice do --CHANGE
  128. local d,dy,dy=0,0,0 --distance to object, and x,y position of object
  129. for u=1,1000 do --put a limit on the looping
  130. local xx=self.car.x+u*math.sin(i*f) --step forward
  131. local yy=self.car.y+u*math.cos(i*f)
  132. --if in bounds, and pixel non blank, we've hit something
  133. if xx>1 and xx<=WIDTH and yy>1 and yy<=HEIGHT then
  134. local r,g,b,a=s:get(xx,yy)
  135. if b~=self.TrackBlue then d=u dx=xx dy=yy break end
  136. else break end
  137. end
  138. --store angle, distance and x,y position of object
  139. if d>0 then table.insert(rays,{angle=i,dist=d,x=dx,y=dy}) end
  140. end
  141. return rays
  142. end
  143.  
  144. function Track2:SetNewPosition()
  145. local maxd,maxa=0,0
  146. for i,v in ipairs(views) do
  147. if v.dist>maxd then maxd=v.dist maxa=v.angle end
  148. end
  149. local f=math.pi/180 --to convert degrees to radians
  150. self.car.x=self.car.x+self.car.speed*math.sin(maxa*f)
  151. self.car.y=self.car.y+self.car.speed*math.cos(maxa*f)
  152. self.car.angle=maxa
  153. end
  154.  
  155. function Track2:touched(touch)
  156. --dummy for now
  157. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement