Why doesn’t the box drawn during draw follow the updated position of the polygon? Box.x and box.y show that the polygon is affected by its initial velocity and gravity, but no updates to the drawn position.
Thanks in advance
-- Use this function to perform your initial setup
function setup()
print("Hello World!")
ox= WIDTH/2
oy = HEIGHT/2
box = physics.body(POLYGON, vec2(ox,oy),vec2(ox,oy+20),vec2(ox+20,oy+20),vec2(ox+20,oy) )
box.linearVelocity=vec2(20,0)
box.type = DYNAMIC
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
physics.gravity()
-- This sets the line thickness
strokeWidth(5)
-- Do your drawing here
if box.shapeType == POLYGON then
strokeWidth(10)
points = box.points
for j = 1,#points do
a = points[j]
b = points[(j % #points)+1]
line(a.x, a.y, b.x, b.y)
end
end
print(box.x)
print(box.y)
end
I believe it is because the points are in the local space of the body (and fixed in that space) not in the world space of the viewer. See PhysicsDebugDraw:draw() in the Physics Lab example project, and note the initial translate and rotate that move the view so that it is aligned with the local space.
I have edited your code and added comments to it to explain further my first comment:
function setup()
ox= WIDTH/2
oy = HEIGHT/2
box = physics.body(POLYGON, --<< Relative to centre of mass
vec2(-10, -10), --<< and set counter-clockwise
vec2(10, -10),
vec2(10, 10),
vec2(-10, 10))
box.x = ox --<< Position of centre of mass
box.y = oy --<< Position of centre of mass
box.linearVelocity = vec2(20, 0)
box.type = DYNAMIC -- The type is DYNAMIC by default
end
function draw()
background(0)
physics.gravity() -- Has no effect, without an argument
strokeWidth(5) -- Has no effect, as strokeWidth is reset to 10 below
pushMatrix() --<< Preserve the matrix
translate(box.x, box.y) --<< Move view over the centre of mass
rotate(box.angle) --<< Rotate view for the body's rotation
if box.shapeType == POLYGON then -- Will always be true
strokeWidth(10)
points = box.points -- These are in the local space of the box,
-- relative to the centre of mass and with no rotation
for j = 1, #points do
a = points[j]
b = points[(j % #points) + 1]
line(a.x, a.y, b.x, b.y)
end
end
popMatrix() -- Restore the matrix
end
There is an explanation of the Physics Lab example project on the wiki here. I have extended it to cover in more detail the subject of this discussion.