Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Buttons = class()
- function Buttons:init(x,y,txt,funct,Mode,l,h)
- self.x = x
- self.y = y
- fontSize(30)
- font("Copperplate-Bold")
- self.txt = txt
- self.Sl,self.Sh = textSize(self.txt)
- self.funct = funct
- self.l=l or self.Sl+20
- self.h=h or self.Sh+20
- self.mode=Mode
- self.colour=color(12, 14, 11, 255)
- end
- function Buttons:draw()
- pushStyle()
- fontSize(30)
- font("Copperplate-Bold")
- fill(self.colour)
- if self.mode then
- text(self.txt,self.x+self.l/2,self.y+self.h/2)
- else
- text(self.txt,self.x,self.y)
- end
- popStyle()
- end
- function Buttons:touched(touch)
- if self.mode then
- self.tx=touch.x-self.l/2
- self.ty=touch.y-self.h/2
- else
- self.tx = touch.x
- self.ty = touch.y
- end
- if self.tx<=self.x + self.l/2 and self.tx>=self.x - self.l/2 and
- self.ty>=self.y - self.h/2 and self.ty<=self.y + self.h/2 then
- if touch.state==BEGAN then
- self.colour=color(113, 179, 77, 255)
- self.press=true
- elseif touch.state == ENDED and self.press then
- self.colour=color(100, 180, 94, 255)
- self.funct()
- sound(SOUND_PICKUP, 33875)
- end
- else
- self.colour=color(0, 0, 0, 255)
- end
- if touch.state==ENDED then
- self.colour=color(0, 0, 0, 255)
- self.press=false
- end
- end
- PhysicsDebugDraw = class()
- function PhysicsDebugDraw:init()
- self.bodies = {}
- self.joints = {}
- self.touchMap = {}
- self.contacts = {}
- end
- function PhysicsDebugDraw:addBody(body)
- table.insert(self.bodies,body)
- end
- function PhysicsDebugDraw:addJoint(joint)
- table.insert(self.joints,joint)
- end
- function PhysicsDebugDraw:draw()
- pushStyle()
- smooth()
- local _AA = 4 --force for the touch
- local _BB = 0.5
- for k,v in pairs(self.touchMap) do
- local _A= v.body:getWorldPoint(v.anchor)
- local _B = v.tp
- local diff = _B-_A
- local vel = v.body:getLinearVelocityFromWorldPoint(_A)
- v.body:applyForce( diff * _AA - vel * _BB, _A)
- end
- stroke(245, 231, 3, 255)
- strokeWidth(6)
- for k,joint in pairs(self.joints) do
- local a = joint.anchorA
- local b = joint.anchorB
- line(a.x,a.y,b.x,b.y)
- end
- noFill()
- for i,body in ipairs(self.bodies) do
- pushMatrix()
- translate(body.x, body.y)
- rotate(body.angle)
- if body.type == STATIC then
- stroke(255, 255, 255, 255)
- strokeWidth(10)
- -- fill(0,0,0,0)
- elseif body.type == DYNAMIC then
- -- stroke(255, 255, 255, 255)
- strokeWidth(6)
- elseif body.type == KINEMATIC then
- stroke(150,150,255,255)
- strokeWidth(3)
- end
- if body.shapeType == POLYGON then
- strokeWidth(3.0)
- local points = body.points
- for j = 1,#points do
- a = points[j]
- b = points[(j % #points)+1]
- line(a.x, a.y, b.x, b.y)
- end
- elseif body.shapeType == CHAIN and body.shapeType == EDGE then
- strokeWidth(3.0)
- local points = body.points
- for j = 1,#points -1 do
- a = points[j]
- b = points[j+1]
- line(a.x, a.y, b.x, b.y)
- end
- elseif body.shapeType == CIRCLE then
- -- strokeWidth(3)
- ellipse(0,0,body.radius*2)
- end
- popMatrix()
- end
- end
- function PhysicsDebugDraw:touched(touch)
- local touchPoint = vec2(touch.x, touch.y)
- if touch.state == BEGAN then
- for i,body in ipairs(self.bodies) do
- if body.type == DYNAMIC and body:testPoint(touchPoint) then
- self.touchMap[touch.id] = {
- tp = touchPoint,
- body = body,
- anchor = body:getLocalPoint(touchPoint)
- }
- return true
- end
- end
- elseif touch.state == MOVING and self.touchMap[touch.id] then
- self.touchMap[touch.id].tp = touchPoint
- return true
- elseif touch.state == ENDED and self.touchMap[touch.id] then
- self.touchMap[touch.id] = nil
- return true
- end
- return false
- end
- function PhysicsDebugDraw:collide(contact)
- if contact.state == BEGAN then
- self.contacts[contact.id] = contact
- elseif contact.state == MOVING then
- self.contacts[contact.id] = contact
- elseif contact.state == ENDED then
- self.contacts[contact.id] = nil
- end
- end
- --MAIN
- Test1 = class()
- function Test1:init()
- local xcc = WIDTH/2
- self.title = "Pendulum"
- self.point1 = vec2(WIDTH/2-264,1000)
- self.point2 = vec2(WIDTH/2-264,HEIGHT/2+300)
- self.table = vec2(WIDTH/2+250,1000)
- self.table1 = vec2(WIDTH/2+250,HEIGHT/2+300)
- self.colores = color(221, 42, 42, 255)
- end
- function Test1:setup()
- createGround()
- pisoArriba()
- crearParedDerecha()
- crearParedIzquierda()
- local BOX = createBox(WIDTH/2-10, HEIGHT/2+170, 10, 180)
- local example = createCircle(WIDTH/2-10, HEIGHT/2+300, 50, 50)
- -- stroke(71, 117, 146, 255)
- -- strokeWidth(100)
- example.type = STATIC
- local MOVE = physics.joint(REVOLUTE , BOX, example , example.position)
- debugDraw:addJoint(MOVE)
- local ball_p_1 = createCircle(WIDTH/2-10,HEIGHT/2+40,30)
- ball_p_1.restitution = 1
- local MOVE2 = physics.joint(REVOLUTE,BOX,ball_p_1,ball_p_1.position)
- debugDraw:addJoint(MOVE2)
- local P_2 = createBox(WIDTH/2-10, HEIGHT/2+170, 10, 180) --BOX2
- local MOVE3 = physics.joint(REVOLUTE,example,P_2,example.position)
- debugDraw:addJoint(MOVE3)
- local ball_p_2 = createCircle(WIDTH/2-10,HEIGHT/2+40,30)
- ball_p_2.restitution = 1
- local MOVE4 = physics.joint(REVOLUTE,P_2,ball_p_2,ball_p_2.position)
- debugDraw:addJoint(MOVE4)
- local P_3 = createBox(WIDTH/2-10, HEIGHT/2+170, 10, 180) --BOX2
- local ball_p_3 = createCircle(WIDTH/2-10,HEIGHT/2+40,30)
- ball_p_2.restitution = 1
- local MOVE5 = physics.joint(REVOLUTE,example,P_3,example.position)
- debugDraw:addJoint(MOVE5)
- local MOVE6 = physics.joint(REVOLUTE,P_3,ball_p_3,ball_p_3.position)
- debugDraw:addJoint(MOVE6)
- end
- function Test1:draw()
- physics_force = physics.raycast(self.point1,self.point2)
- force_2 = physics.raycast(self.table,self.table1)
- stroke(116, 102, 180, 255)
- strokeWidth(8)
- if physics_force then
- line(self.point1.x,self.point1.y,physics_force.point.x,
- physics_force.point.y)
- physics_force.body:applyForce(vec2(0,-320))
- else
- line(self.point1.x,self.point1.y,self.point2.x,self.point2.y)
- end
- if force_2 then
- line(self.table.x,self.table.y,force_2.point.x,force_2.point.y)
- force_2.body:applyForce(vec2(0,-320))
- else
- line(self.table.x,self.table.y,self.table1.x,self.table1.y)
- end
- -- Codea does not automatically call this method
- end
- function Test1:touched(touch)
- fill(207, 23, 23, 255)
- if touch.state == BEGAN then
- local bola = createCircle(touch.x,touch.y,25,25)
- end
- end
- displayMode(FULLSCREEN_NO_BUTTONS)
- -- Use this function to perform your initial setup
- function setup()
- lineCapMode(ROUND)
- debugDraw = PhysicsDebugDraw()
- exitButtoninit()
- -- test classes
- -- to add your own, make sure to define setup() and cleanup()
- -- in addition to draw() and touched()
- tests = {Test1()}
- currentTestIndex = 1
- currentTest = nil
- setTest(currentTestIndex)
- end
- -- function que borra el test anterior para mostrar el nuevo
- function setTest(t)
- -- currentTestIndex = t
- currentTest = tests[t]
- currentTest:setup()
- end
- -- fin de functiiones tests
- function createCircle(x,y,ratio)
- local circle = physics.body(CIRCLE, ratio)
- -- enable smooth motion
- circle.x = x
- circle.y = y
- circle.restitution = 0.3
- debugDraw:addBody(circle)
- return circle
- end
- function createBox(x,y,w,h)
- -- polygons are defined by a series of points in counter-clockwise order
- 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))
- box.interpolate = true
- box.x = x
- box.y = y
- box.restitutions = 0.25
- box.sleepingAllowed = false
- debugDraw:addBody(box)
- return box
- end
- function createGround() --piso
- local ground = physics.body(POLYGON, vec2(0,20), vec2(0,0), vec2(WIDTH,0), vec2(WIDTH,20))
- ground.type = STATIC
- debugDraw:addBody(ground)
- return ground
- end
- function crearParedDerecha() --derecha
- local xc = WIDTH/2
- local alto = HEIGHT/2
- local _D = physics.body(POLYGON, vec2(xc+380,23),vec2(xc+380,997),vec2(xc+360,997),vec2(xc+360,23))
- _D.type = STATIC
- debugDraw:addBody(_D)
- return _D
- end
- function crearParedIzquierda() --derecha
- local xcc = WIDTH/2
- --local alto = HEIGHT/2
- local _I = physics.body(POLYGON, vec2(xcc-380,23),vec2(xcc-380,997),vec2(xcc-360,997),vec2(xcc-360,23))
- _I.type = STATIC
- debugDraw:addBody(_I)
- return _I
- end
- function pisoArriba() --piso
- local top = physics.body(POLYGON, vec2(0,1020), vec2(0,1000), vec2(WIDTH,1000), vec2(WIDTH,1020))
- top.type = STATIC
- debugDraw:addBody(top)
- return top
- end
- -- This function gets called once every frame
- function draw()
- -- This sets the background color to black
- background(97, 97, 14, 0)
- currentTest:draw()
- debugDraw:draw()
- exitButtondraw()
- local str = string.format(" %s", currentTest.title)
- font("Arial")
- fontSize(23)
- fill(255, 255, 55, 255)
- text(str, WIDTH/2-10, HEIGHT - 50)
- end
- function touched(touch)
- if debugDraw:touched(touch) == false then
- currentTest:touched(touch)
- exitButtontouch(touch)
- end
- end
- function collide(contact)
- if debugDraw then
- debugDraw:collide(contact)
- end
- if currentTest and currentTest.collide then
- currentTest:collide(contact)
- end
- end
- function exitButtoninit()
- exit = Buttons(WIDTH/2-350,HEIGHT/2-500,"EXIT",close,true)
- reestart = Buttons(WIDTH/2+200,HEIGHT/2-500,"RESTART",restart,true)
- end
- function exitButtondraw()
- exit:draw()
- reestart:draw()
- end
- function exitButtontouch(touch)
- exit:touched(touch)
- reestart:touched(touch)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement