Advertisement
luismiasas

physics.body(force)

Sep 1st, 2013
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.12 KB | None | 0 0
  1. Buttons = class()
  2.  
  3.  
  4.  
  5. function Buttons:init(x,y,txt,funct,Mode,l,h)
  6.  
  7. self.x = x
  8.  
  9. self.y = y
  10.  
  11. fontSize(30)
  12.  
  13. font("Copperplate-Bold")
  14.  
  15. self.txt = txt
  16.  
  17. self.Sl,self.Sh = textSize(self.txt)
  18.  
  19. self.funct = funct
  20.  
  21. self.l=l or self.Sl+20
  22.  
  23. self.h=h or self.Sh+20
  24.  
  25. self.mode=Mode
  26.  
  27. self.colour=color(12, 14, 11, 255)
  28.  
  29. end
  30.  
  31.  
  32.  
  33. function Buttons:draw()
  34.  
  35. pushStyle()
  36.  
  37. fontSize(30)
  38.  
  39. font("Copperplate-Bold")
  40.  
  41. fill(self.colour)
  42.  
  43. if self.mode then
  44.  
  45. text(self.txt,self.x+self.l/2,self.y+self.h/2)
  46.  
  47. else
  48.  
  49. text(self.txt,self.x,self.y)
  50.  
  51. end
  52.  
  53. popStyle()
  54.  
  55. end
  56.  
  57.  
  58.  
  59. function Buttons:touched(touch)
  60.  
  61. if self.mode then
  62.  
  63. self.tx=touch.x-self.l/2
  64.  
  65. self.ty=touch.y-self.h/2
  66.  
  67. else
  68.  
  69. self.tx = touch.x
  70.  
  71. self.ty = touch.y
  72.  
  73. end
  74.  
  75. if self.tx<=self.x + self.l/2 and self.tx>=self.x - self.l/2 and
  76.  
  77. self.ty>=self.y - self.h/2 and self.ty<=self.y + self.h/2 then
  78.  
  79. if touch.state==BEGAN then
  80.  
  81. self.colour=color(113, 179, 77, 255)
  82.  
  83. self.press=true
  84.  
  85. elseif touch.state == ENDED and self.press then
  86.  
  87. self.colour=color(100, 180, 94, 255)
  88.  
  89. self.funct()
  90.  
  91. sound(SOUND_PICKUP, 33875)
  92.  
  93. end
  94.  
  95. else
  96.  
  97. self.colour=color(0, 0, 0, 255)
  98.  
  99. end
  100.  
  101. if touch.state==ENDED then
  102.  
  103. self.colour=color(0, 0, 0, 255)
  104.  
  105. self.press=false
  106.  
  107. end
  108.  
  109. end
  110.  
  111. PhysicsDebugDraw = class()
  112.  
  113. function PhysicsDebugDraw:init()
  114. self.bodies = {}
  115. self.joints = {}
  116. self.touchMap = {}
  117. self.contacts = {}
  118.  
  119. end
  120.  
  121. function PhysicsDebugDraw:addBody(body)
  122. table.insert(self.bodies,body)
  123. end
  124.  
  125. function PhysicsDebugDraw:addJoint(joint)
  126. table.insert(self.joints,joint)
  127. end
  128.  
  129.  
  130. function PhysicsDebugDraw:draw()
  131.  
  132. pushStyle()
  133. smooth()
  134.  
  135. local _AA = 4 --force for the touch
  136. local _BB = 0.5
  137. for k,v in pairs(self.touchMap) do
  138. local _A= v.body:getWorldPoint(v.anchor)
  139. local _B = v.tp
  140. local diff = _B-_A
  141. local vel = v.body:getLinearVelocityFromWorldPoint(_A)
  142. v.body:applyForce( diff * _AA - vel * _BB, _A)
  143.  
  144. end
  145.  
  146. stroke(245, 231, 3, 255)
  147. strokeWidth(6)
  148. for k,joint in pairs(self.joints) do
  149. local a = joint.anchorA
  150. local b = joint.anchorB
  151. line(a.x,a.y,b.x,b.y)
  152. end
  153.  
  154. noFill()
  155.  
  156. for i,body in ipairs(self.bodies) do
  157. pushMatrix()
  158. translate(body.x, body.y)
  159. rotate(body.angle)
  160.  
  161. if body.type == STATIC then
  162. stroke(255, 255, 255, 255)
  163. strokeWidth(10)
  164. -- fill(0,0,0,0)
  165. elseif body.type == DYNAMIC then
  166. -- stroke(255, 255, 255, 255)
  167. strokeWidth(6)
  168. elseif body.type == KINEMATIC then
  169. stroke(150,150,255,255)
  170. strokeWidth(3)
  171. end
  172.  
  173. if body.shapeType == POLYGON then
  174. strokeWidth(3.0)
  175. local points = body.points
  176. for j = 1,#points do
  177. a = points[j]
  178. b = points[(j % #points)+1]
  179. line(a.x, a.y, b.x, b.y)
  180. end
  181.  
  182. elseif body.shapeType == CHAIN and body.shapeType == EDGE then
  183.  
  184. strokeWidth(3.0)
  185. local points = body.points
  186. for j = 1,#points -1 do
  187. a = points[j]
  188. b = points[j+1]
  189. line(a.x, a.y, b.x, b.y)
  190. end
  191.  
  192. elseif body.shapeType == CIRCLE then
  193.  
  194. -- strokeWidth(3)
  195. ellipse(0,0,body.radius*2)
  196. end
  197.  
  198. popMatrix()
  199. end
  200. end
  201.  
  202. function PhysicsDebugDraw:touched(touch)
  203. local touchPoint = vec2(touch.x, touch.y)
  204. if touch.state == BEGAN then
  205. for i,body in ipairs(self.bodies) do
  206. if body.type == DYNAMIC and body:testPoint(touchPoint) then
  207. self.touchMap[touch.id] = {
  208. tp = touchPoint,
  209. body = body,
  210. anchor = body:getLocalPoint(touchPoint)
  211. }
  212. return true
  213. end
  214. end
  215. elseif touch.state == MOVING and self.touchMap[touch.id] then
  216. self.touchMap[touch.id].tp = touchPoint
  217. return true
  218. elseif touch.state == ENDED and self.touchMap[touch.id] then
  219. self.touchMap[touch.id] = nil
  220. return true
  221. end
  222. return false
  223. end
  224.  
  225. function PhysicsDebugDraw:collide(contact)
  226.  
  227. if contact.state == BEGAN then
  228. self.contacts[contact.id] = contact
  229. elseif contact.state == MOVING then
  230. self.contacts[contact.id] = contact
  231.  
  232.  
  233. elseif contact.state == ENDED then
  234. self.contacts[contact.id] = nil
  235. end
  236. end
  237.  
  238. --MAIN
  239.  
  240. Test1 = class()
  241.  
  242. function Test1:init()
  243. local xcc = WIDTH/2
  244. self.title = "Pendulum"
  245. self.point1 = vec2(WIDTH/2-264,1000)
  246. self.point2 = vec2(WIDTH/2-264,HEIGHT/2+300)
  247. self.table = vec2(WIDTH/2+250,1000)
  248. self.table1 = vec2(WIDTH/2+250,HEIGHT/2+300)
  249. self.colores = color(221, 42, 42, 255)
  250.  
  251. end
  252.  
  253. function Test1:setup()
  254. createGround()
  255. pisoArriba()
  256. crearParedDerecha()
  257. crearParedIzquierda()
  258.  
  259. local BOX = createBox(WIDTH/2-10, HEIGHT/2+170, 10, 180)
  260.  
  261. local example = createCircle(WIDTH/2-10, HEIGHT/2+300, 50, 50)
  262. -- stroke(71, 117, 146, 255)
  263. -- strokeWidth(100)
  264. example.type = STATIC
  265.  
  266. local MOVE = physics.joint(REVOLUTE , BOX, example , example.position)
  267. debugDraw:addJoint(MOVE)
  268.  
  269. local ball_p_1 = createCircle(WIDTH/2-10,HEIGHT/2+40,30)
  270. ball_p_1.restitution = 1
  271.  
  272. local MOVE2 = physics.joint(REVOLUTE,BOX,ball_p_1,ball_p_1.position)
  273. debugDraw:addJoint(MOVE2)
  274.  
  275. local P_2 = createBox(WIDTH/2-10, HEIGHT/2+170, 10, 180) --BOX2
  276.  
  277. local MOVE3 = physics.joint(REVOLUTE,example,P_2,example.position)
  278. debugDraw:addJoint(MOVE3)
  279.  
  280. local ball_p_2 = createCircle(WIDTH/2-10,HEIGHT/2+40,30)
  281. ball_p_2.restitution = 1
  282.  
  283. local MOVE4 = physics.joint(REVOLUTE,P_2,ball_p_2,ball_p_2.position)
  284. debugDraw:addJoint(MOVE4)
  285.  
  286. local P_3 = createBox(WIDTH/2-10, HEIGHT/2+170, 10, 180) --BOX2
  287.  
  288. local ball_p_3 = createCircle(WIDTH/2-10,HEIGHT/2+40,30)
  289. ball_p_2.restitution = 1
  290.  
  291. local MOVE5 = physics.joint(REVOLUTE,example,P_3,example.position)
  292. debugDraw:addJoint(MOVE5)
  293.  
  294. local MOVE6 = physics.joint(REVOLUTE,P_3,ball_p_3,ball_p_3.position)
  295. debugDraw:addJoint(MOVE6)
  296.  
  297.  
  298. end
  299.  
  300. function Test1:draw()
  301.  
  302. physics_force = physics.raycast(self.point1,self.point2)
  303. force_2 = physics.raycast(self.table,self.table1)
  304.  
  305. stroke(116, 102, 180, 255)
  306. strokeWidth(8)
  307. if physics_force then
  308. line(self.point1.x,self.point1.y,physics_force.point.x,
  309. physics_force.point.y)
  310. physics_force.body:applyForce(vec2(0,-320))
  311. else
  312. line(self.point1.x,self.point1.y,self.point2.x,self.point2.y)
  313. end
  314.  
  315. if force_2 then
  316.  
  317. line(self.table.x,self.table.y,force_2.point.x,force_2.point.y)
  318. force_2.body:applyForce(vec2(0,-320))
  319.  
  320. else
  321. line(self.table.x,self.table.y,self.table1.x,self.table1.y)
  322. end
  323.  
  324.  
  325.  
  326. -- Codea does not automatically call this method
  327. end
  328.  
  329. function Test1:touched(touch)
  330. fill(207, 23, 23, 255)
  331. if touch.state == BEGAN then
  332. local bola = createCircle(touch.x,touch.y,25,25)
  333. end
  334.  
  335.  
  336. end
  337.  
  338. displayMode(FULLSCREEN_NO_BUTTONS)
  339.  
  340. -- Use this function to perform your initial setup
  341. function setup()
  342. lineCapMode(ROUND)
  343. debugDraw = PhysicsDebugDraw()
  344. exitButtoninit()
  345.  
  346.  
  347. -- test classes
  348. -- to add your own, make sure to define setup() and cleanup()
  349. -- in addition to draw() and touched()
  350. tests = {Test1()}
  351. currentTestIndex = 1
  352. currentTest = nil
  353. setTest(currentTestIndex)
  354.  
  355. end
  356. -- function que borra el test anterior para mostrar el nuevo
  357. function setTest(t)
  358. -- currentTestIndex = t
  359. currentTest = tests[t]
  360. currentTest:setup()
  361. end
  362.  
  363. -- fin de functiiones tests
  364.  
  365. function createCircle(x,y,ratio)
  366. local circle = physics.body(CIRCLE, ratio)
  367. -- enable smooth motion
  368. circle.x = x
  369. circle.y = y
  370. circle.restitution = 0.3
  371. debugDraw:addBody(circle)
  372. return circle
  373. end
  374.  
  375. function createBox(x,y,w,h)
  376. -- polygons are defined by a series of points in counter-clockwise order
  377. local box = physics.body(POLYGON, vec2(-w/2,h/2), vec2(-w/2,-h/2), vec2(w/2,-h/2), vec2(w/2,h/2))
  378. box.interpolate = true
  379. box.x = x
  380. box.y = y
  381. box.restitutions = 0.25
  382. box.sleepingAllowed = false
  383. debugDraw:addBody(box)
  384. return box
  385. end
  386.  
  387. function createGround() --piso
  388. local ground = physics.body(POLYGON, vec2(0,20), vec2(0,0), vec2(WIDTH,0), vec2(WIDTH,20))
  389. ground.type = STATIC
  390. debugDraw:addBody(ground)
  391. return ground
  392. end
  393.  
  394.  
  395. function crearParedDerecha() --derecha
  396. local xc = WIDTH/2
  397. local alto = HEIGHT/2
  398. local _D = physics.body(POLYGON, vec2(xc+380,23),vec2(xc+380,997),vec2(xc+360,997),vec2(xc+360,23))
  399. _D.type = STATIC
  400. debugDraw:addBody(_D)
  401. return _D
  402. end
  403.  
  404. function crearParedIzquierda() --derecha
  405. local xcc = WIDTH/2
  406. --local alto = HEIGHT/2
  407. local _I = physics.body(POLYGON, vec2(xcc-380,23),vec2(xcc-380,997),vec2(xcc-360,997),vec2(xcc-360,23))
  408. _I.type = STATIC
  409. debugDraw:addBody(_I)
  410. return _I
  411. end
  412.  
  413. function pisoArriba() --piso
  414. local top = physics.body(POLYGON, vec2(0,1020), vec2(0,1000), vec2(WIDTH,1000), vec2(WIDTH,1020))
  415. top.type = STATIC
  416. debugDraw:addBody(top)
  417. return top
  418. end
  419.  
  420. -- This function gets called once every frame
  421. function draw()
  422. -- This sets the background color to black
  423. background(97, 97, 14, 0)
  424.  
  425. currentTest:draw()
  426. debugDraw:draw()
  427. exitButtondraw()
  428. local str = string.format(" %s", currentTest.title)
  429.  
  430. font("Arial")
  431. fontSize(23)
  432. fill(255, 255, 55, 255)
  433.  
  434.  
  435. text(str, WIDTH/2-10, HEIGHT - 50)
  436.  
  437. end
  438.  
  439. function touched(touch)
  440. if debugDraw:touched(touch) == false then
  441. currentTest:touched(touch)
  442. exitButtontouch(touch)
  443. end
  444. end
  445.  
  446. function collide(contact)
  447. if debugDraw then
  448. debugDraw:collide(contact)
  449. end
  450.  
  451. if currentTest and currentTest.collide then
  452. currentTest:collide(contact)
  453. end
  454. end
  455.  
  456. function exitButtoninit()
  457.  
  458. exit = Buttons(WIDTH/2-350,HEIGHT/2-500,"EXIT",close,true)
  459. reestart = Buttons(WIDTH/2+200,HEIGHT/2-500,"RESTART",restart,true)
  460.  
  461. end
  462.  
  463. function exitButtondraw()
  464.  
  465. exit:draw()
  466. reestart:draw()
  467.  
  468. end
  469.  
  470. function exitButtontouch(touch)
  471.  
  472. exit:touched(touch)
  473. reestart:touched(touch)
  474. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement