Skip to content

Instantly share code, notes, and snippets.

@interactivenyc
Last active April 17, 2016 20:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save interactivenyc/ef242d808a620424f19014c9e7c65494 to your computer and use it in GitHub Desktop.
Save interactivenyc/ef242d808a620424f19014c9e7c65494 to your computer and use it in GitHub Desktop.
Physics Noob
function setup()
supportedOrientations(LANDSCAPE_ANY)
-- displayMode(FULLSCREEN_NO_BUTTONS)
top = physics.body(EDGE, vec2(0, HEIGHT), vec2(WIDTH, HEIGHT))
top.type = STATIC
top.restitution = 0
bottom = physics.body(EDGE, vec2(0, 0), vec2(WIDTH, 0))
bottom.type = STATIC
bottom.restitution = 0
shipPoints = {
--Nose
vec2(WIDTH/10,0),
--Rect top
vec2(WIDTH/20,HEIGHT/20),
vec2(-WIDTH/20,HEIGHT/20),
--Wing top
vec2((-WIDTH/20)-(WIDTH/30),HEIGHT/10),
vec2((-WIDTH/10)-(WIDTH/25),HEIGHT/10),
vec2(-WIDTH/10,HEIGHT/20),
--Butt
vec2(-WIDTH/10,(HEIGHT/20)-(HEIGHT/50)),
vec2((-WIDTH/10)-(WIDTH/50),(HEIGHT/20)-(HEIGHT/50)),
vec2((-WIDTH/10)-(WIDTH/50),(-((HEIGHT/20)-(HEIGHT/50)))),
vec2(-WIDTH/10,(-((HEIGHT/20)-(HEIGHT/50)))),
--Bottom wing
vec2(-WIDTH/10,(-(HEIGHT/20))),
vec2((-WIDTH/10)-(WIDTH/25),(-(HEIGHT/10))),
vec2((-WIDTH/20)-(WIDTH/30),(-(HEIGHT/10))),
--Rect bottom
vec2(-WIDTH/20,(-(HEIGHT/20))),
vec2(WIDTH/20,(-(HEIGHT/20)))
}
ship = physics.body(POLYGON,unpack(shipPoints))
ship.x = WIDTH/2
ship.y = HEIGHT/2
ship.type = DYNAMIC
ship.interpolate = true
ship.gravityScale = 0
ship.restitution = 0
end
function draw()
background(0, 0, 0, 255)
if CurrentTouch.state == BEGAN or CurrentTouch.state == MOVING then
ship:applyForce(vec2(0,800))
if CurrentTouch.x<WIDTH/2 then
ship.angle = ship.angle + 1
else
ship.angle = ship.angle - 1
end
else
ship:applyForce(vec2(0,-800))
end
drawBody(ship)
drawBody(top)
drawBody(bottom)
end
function drawBody(body)
pushStyle()
pushMatrix()
strokeWidth(5)
stroke(255, 255, 255, 255)
translate(body.x, body.y)
rotate(body.angle)
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 or 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.0)
line(0,0,body.radius-3,0)
ellipse(0,0,body.radius*2)
end
popMatrix()
popStyle()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment