Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --# Main
- -- class
- -- Use this function to perform your initial setup
- function setup()
- b={}
- for i=1,5 do
- b[i]=Ball()
- end
- r={}
- for i=1,5 do
- r[i]=Rect()
- end
- CreateWalls()
- end
- -- This function gets called once every frame
- function draw()
- background(195, 195, 201, 255)
- for i=1,#b do
- b[i]:draw()
- end
- for i=1,#r do
- r[i]:draw()
- end
- end
- function CreateWalls()
- leftWall = CreateWall(1,1,1,HEIGHT-1,1.0)
- rightWall = CreateWall(WIDTH-1,0,WIDTH-1,HEIGHT-1,0.9)
- bottomWall = CreateWall(1,1,WIDTH-1,1,0.2)
- topWall = CreateWall(1,HEIGHT-1,WIDTH-1,HEIGHT-1,0.7)
- end
- --this function creates one wall (actually just a line)
- function CreateWall(x,y,x1,y1,r)
- local w = physics.body(EDGE,vec2(x,y),vec2(x1,y1)) -- vec2
- w.restitution=r --see comment above
- return w
- end
- --# Ball
- Ball = class()
- function Ball:init(x,y,d,c)
- self.x=x or math.random(0,WIDTH)
- self.y=y or math.random(0,HEIGHT)
- self.diameter=d or math.random(50,200)
- self.colr=c or color(math.random(0,255),math.random(0,255),math.random(0,255))
- self.p=Physics(CIRCLE,{self.diameter/2},self.x,self.y) --physics uses radius
- end
- function Ball:draw()
- pushStyle() -- store style settings
- fill(self.colr) --set color
- pushMatrix()
- local x,y,a=self.p:currentPosition()
- translate(x,y)
- rotate(a)
- ellipse(0,0,self.diameter)
- popMatrix()
- popStyle() -- put back style settings
- end
- --# Rect
- Rect = class()
- function Rect:init(x,y,w,h,c)
- self.x=x or math.random(0,WIDTH)
- self.y=y or math.random(0,HEIGHT)
- self.width=w or math.random(60,150)
- self.height=h or math.random(30,100)
- self.colr=c or color(math.random(0,255),math.random(0,255),math.random(0,255))
- local data={}
- table.insert(data,vec2(-self.width/2,-self.height/2))
- table.insert(data,vec2(-self.width/2,self.height/2))
- table.insert(data,vec2(self.width/2,self.height/2))
- table.insert(data,vec2(self.width/2,-self.height/2))
- self.p=Physics(POLYGON,data,self.x,self.y)
- end
- function Rect:draw()
- pushStyle() -- store style settings
- fill(self.colr) --set color
- pushMatrix()
- local x,y,a=self.p:currentPosition()
- translate(x,y)
- rotate(a)
- rect(-self.width/2,-self.height/2,self.width,self.height)
- popMatrix()
- popStyle() -- put back style settings
- end
- --# Physics
- Physics = class()
- --type=CIRCLE or POLYGON
- --data is a table. For circles it is one value, the radius, while for a polygon it is a set of vec2
- --x,y is the initial position of the centre
- --a is the initial angle
- --g is the gravity value, 0=tabletop, 1=normal (things fall down)
- --r is the restitution, ie springiness
- --f is friction
- --lv is linear velocity
- --i is any info you want to store to identify this object, eg a name
- function Physics:init(type,data,x,y,a,g,r,f,lv,i)
- if type==CIRCLE then
- self.body=physics.body(CIRCLE,data[1])
- elseif type==POLYGON then
- self.body=physics.body(POLYGON,unpack(data))
- end
- self.body.x=x
- self.body.y=y
- self.body.angle=a or 0
- if g then self.body.gravityScale=1 else self.body.gravityScale=0 end
- self.body.restitution=r or 1
- self.body.friction=f or 0.1
- if lv==nil then lv=vec2(100+math.random(400),100+math.random(400)) end
- self.body.linearVelocity=lv
- self.body.info=i
- end
- function Physics:currentPosition()
- return self.body.x, self.body.y,self.body.angle
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement