Skip to content

Instantly share code, notes, and snippets.

@dermotbalson
Created April 9, 2013 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dermotbalson/5346060 to your computer and use it in GitHub Desktop.
Save dermotbalson/5346060 to your computer and use it in GitHub Desktop.
Demos
function setup()
--first the rectangle
rect_width=100 --size of rectangle
rect_height=50
--now create the physics object, we feed it the location
--of the four corners, relative to the centre of the
--rectangle, which is why all the measurements below
--are for half the height or width
p_rect = physics.body(POLYGON,
vec2(-rect_width / 2, -rect_height / 2),
vec2(-rect_width / 2, rect_height / 2),
vec2(rect_width / 2, rect_height / 2),
vec2(rect_width / 2, -rect_height / 2)
)
p_rect.x = math.random(60,250) -- choose random x
p_rect.y = math.random(60,250) -- same for y
p_rect.angle=math.random(0,360) -- rotation, 0 to 360deg
p_rect.gravityScale = 0
p_rect.restitution = 1 -- this rectangle is bouncy
p_rect.friction = 0.1 -- the amount of friction to apply
-- set a random velocity (pixels per second)
p_rect.linearVelocity = vec2(100+math.random(400),100+math.random(400))
p_rect.info="rect" --give the rectangle a name, so when
--collisions occur, we can figure out which object was
--involved. This is optional.
img=readImage("Tyrian Remastered:Mine Spiked Huge")
--I've chosen a roughly circular image so I an
--use a physics circle object
--use the average of height and width as diameter of circle
img_diam=(img.width+img.height)/2
--create the physics object - note diam/2 as
--use radius not diameter
p_img = physics.body(CIRCLE,img_diam/2)
p_img.x = math.random(60,250) -- random position
p_img.y = math.random(60,250) -- ditto
p_img.angle=math.random(0,360)
p_img.gravityScale = 0 -- no gravity
p_img.restitution = 0.8 -- this image is not so bouncy
p_img.friction = 0.4 -- the amount of friction
p_img.linearVelocity = vec2(math.random(400),math.random(400))
p_img.info="mine" --as for rectangle above
CreateWalls()
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
function draw()
background(200,200,200,255)
--draw the rectangle --
pushStyle()
fill(255,0,0,255)
--see explanation of next few lines underneath
pushMatrix()
translate(p_rect.x, p_rect.y)
rotate(p_rect.angle)
rect(-rect_width / 2, -rect_height / 2, rect_width, rect_height)
popMatrix()
popStyle()
--see explanation of next few lines at bottom below
pushMatrix()
translate(p_img.x, p_img.y)
rotate(p_img.angle)
sprite(img,0,0)
popMatrix()
end
function collide(contact)
if contact.state == BEGAN then --make sound at start of contact
if contact.bodyA.info=="rect" or contact.bodyB.info=="rect" then
sound(SOUND_JUMP, 50)
end
if contact.bodyA.info=="mine" or contact.bodyB.info=="mine" then
sound(SOUND_EXPLODE, 50)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment