I’m just starting to dig into codeas built in physics, and have been playing around with some basic shapes and physics types. Everything’s been fairly straight forward, but I’m a little confused about the use of physics.body EDGE. Are edges generally meant to remain stationary?
My goal is to simply be able to control the pitch of the floor with my 2 parameters. So far I can raise the line/edge easily enough, but I am unable to lower the edge once raised, it almost seems as though I’m creating many ghost edges that get left behind… then Codea crashes. This behavior leads me to believe that I may be going about things incorrectly.
How can I make my line as drawn behave more like a solid object?
function setup()
-- parameters to control the line
-- line starting point
x1=0
parameter.number("y1",0,HEIGHT/3,100)
-- line end point
x2=WIDTH
parameter.number("y2",0,HEIGHT/3,100)
-- line thickness
lw = 10
-- create an image to represent the physics object
img=readImage("Platformer Art:Crate")
id=img.width/2 -- image dimensions
--create the physics object
p_img = physics.body(POLYGON,vec2(-id,id),vec2(-id,-id),
vec2(id,-id),vec2(id,id))
p_img.x = WIDTH/2
p_img.y = HEIGHT/2
p_img.angle=math.random(0,360)
p_img.gravityScale = 1 -- gravity
p_img.restitution = 0.7 -- bounce
p_img.friction = 0.001 -- friction
p_img.linearDamping = .3 -- drag
p_img.linearVelocity = vec2(math.random(400),math.random(400))
p_img.info="box" -- object name
CreateWalls()
end
function draw()
background(10,10,20) stroke(255) strokeWidth(lw)
-- Draw line
line(x1,y1,x2,y2)
-- Move the edge along with line?
physics.body(EDGE,vec2(x1,y1),vec2(x2,y2))
-- draw the box
pushMatrix()
translate(p_img.x, p_img.y)
rotate(p_img.angle)
sprite(img,0,0)
popMatrix()
end
--creates the frame
function CreateWalls()
WallL = CreateEdge(1,1,1,HEIGHT-1,1.0)
WallR = CreateEdge(WIDTH-1,0,WIDTH-1,HEIGHT-1,0.9)
WallB = CreateEdge(1,1,WIDTH-1,1,0.2)
WallT = CreateEdge(1,HEIGHT-1,WIDTH-1,HEIGHT-1,0.7)
end
--creates one Edge/wall
function CreateEdge(x,y,x1,y1,r)
local w = physics.body(EDGE,vec2(x,y),vec2(x1,y1)) -- vec2
w.restitution=r
return w
end