Why does the physics body show up on the right side of the screen?

I just want to draw a physics body in the center of the screen, using WIDTH/2 and HEIGHT/2. The following code draws it on the far right of the screen and half way up the screen. Would someone take a second look at it and see what I am missing please? Thanks. I have been staring at it all day.

function setup()
    --cliff variables
    cliffWidth = WIDTH/8
    scrollSpeed = -5
    touches = {}
    
    -- Physics variables
    physics.gravity(0,0)
    debugPhysics = PhysicsDebugDraw()
    
    testCliff = Cliff(WIDTH/2,HEIGHT/2,cliffWidth,HEIGHT)
end

function draw()
    background(0, 0, 0, 255)
    
    testCliff:draw()
    
    --handle touches
    for k,touch in pairs(touches) do
        SlappyTouched(touch)
        HappyTouched(touch)
    end
       
            
end

function touched(touch)
    if touch.state == ENDED then
        touches[touch.id] = nil
    elseif touch.state == BEGAN then
        touches[touch.id] = touch
    end
end

function collide(contact)
    if contact.state == BEGAN then 
        print("HIT!")
    end
end





Cliff = class()
    
function Cliff:init(x,y,w,h)
    -- you can accept and set parameters here
    self.x = x
    self.y = y
    self.w = w
    self.h = h

    self.cliffBody = physics.body(POLYGON, vec2(x-w/2,y+h/2), vec2(x-w/2,y-h/2), vec2(x+w/2,y-h/2), vec2(x+w/2,y+h/2))
    self.cliffBody.x = self.x
    self.cliffBody.y = self.y
    debugPhysics:addBody(self.cliffBody)
end


function Cliff:draw()
    debugPhysics:draw()
end


PhysicsDebugDraw = class()

function PhysicsDebugDraw:init()
    self.bodies = {}
    self.joints = {}
    self.touchMap = {}
    self.contacts = {}
end

function PhysicsDebugDraw:addBody(body)
    table.insert(self.bodies,body)
end

function PhysicsDebugDraw:addJoint(joint)
    table.insert(self.joints,joint)
end

function PhysicsDebugDraw:clear()
    -- deactivate all bodies
    
    for i,body in ipairs(self.bodies) do
        body:destroy()
    end
  
    for i,joint in ipairs(self.joints) do
        joint:destroy()
    end      
    
    self.bodies = {}
    self.joints = {}
    self.contacts = {}
    self.touchMap = {}
end

function PhysicsDebugDraw:draw()
    
    pushStyle()
    smooth()
    strokeWidth(5)
    stroke(128,0,128)
    
    local gain = 2.0
    local damp = 0.5
    for k,v in pairs(self.touchMap) do
        local worldAnchor = v.body:getWorldPoint(v.anchor)
        local touchPoint = v.tp
        local diff = touchPoint - worldAnchor
        local vel = v.body:getLinearVelocityFromWorldPoint(worldAnchor)
        v.body:applyForce( (1/1) * diff * gain - vel * damp, worldAnchor)
        
        line(touchPoint.x, touchPoint.y, worldAnchor.x, worldAnchor.y)
    end
    
    stroke(0,255,0,255)
    strokeWidth(5)
    for k,joint in pairs(self.joints) do
        local a = joint.anchorA
        local b = joint.anchorB
        line(a.x,a.y,b.x,b.y)
    end
    
    stroke(255,255,255,255)
    noFill()
    
    
    for i,body in ipairs(self.bodies) do
        pushMatrix()
        translate(body.x, body.y)
        rotate(body.angle)
    
        if body.type == STATIC then
            stroke(255,255,255,255)
        elseif body.type == DYNAMIC then
            stroke(150,255,150,255)
        elseif body.type == KINEMATIC then
            stroke(150,150,255,255)
        end
    
        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()
    end 
    
    stroke(255, 0, 0, 255)
    fill(255, 0, 0, 255)

    for k,v in pairs(self.contacts) do
        for m,n in ipairs(v.points) do
            ellipse(n.x, n.y, 10, 10)
        end
    end
    
    popStyle()
end

function PhysicsDebugDraw:touched(touch)
    local touchPoint = vec2(touch.x, touch.y)
    if touch.state == BEGAN then
        for i,body in ipairs(self.bodies) do
            if body.type == DYNAMIC and body:testPoint(touchPoint) then
                self.touchMap[touch.id] = {tp = touchPoint, body = body, anchor = body:getLocalPoint(touchPoint)} 
                return true
            end
        end
    elseif touch.state == MOVING and self.touchMap[touch.id] then
        self.touchMap[touch.id].tp = touchPoint
        return true
    elseif touch.state == ENDED and self.touchMap[touch.id] then
        self.touchMap[touch.id] = nil
        return true;
    end
    return false
end

function PhysicsDebugDraw:collide(contact)
    if contact.state == BEGAN then
        self.contacts[contact.id] = contact
        sound(SOUND_HIT, 2643)
    elseif contact.state == MOVING then
        self.contacts[contact.id] = contact
    elseif contact.state == ENDED then
        self.contacts[contact.id] = nil
    end
end


When defining the vertices of a physics body the positions are taken as local coords relative to the center of the body(its x and y)

It’s a problem with the positions. Either set the

self.cliffBody = physics.body(POLYGON, vec2(x-w/2,y+h/2), vec2(x-w/2,y-h/2), vec2(x+w/2,y-h/2), vec2(x+w/2,y+h/2))

to

self.cliffBody = physics.body(POLYGON, vec2(0,h), vec2(0,0), vec2(w,0), vec2(w,h))

or

self.cliffBody.x = self.x
self.cliffBody.y = self.y

to

self.cliffBody.x = 0
self.cliffBody.y = 0

(or remove it.)

The cliffBody’s position is in addition to the x- and y- in the code that creates the polygon.

@MrScience101 - I can’t see the problem immediately, but I do suggest you get rid of the debugdraw code unless you definitely need it. It is totally unnecessary for most physics objects and just makes problems like this much harder to debug.

If you look at my explanations of physics you will see you need hardly any code to manage physics bodies, once you have created them.

@MrScience101 - and I think your problem is in the debugdraw code, because the draw function translates to the x,y position of the physics object before drawing it, so if you set that x position to WIDTH/2, debugdraw will translate to WIDTH/2, then draw your object a further WIDTH/2 to the right.

In other words, debugdraw expects you to set up your physics object centred on 0,0. In general, I think that is a good approach, except for objects that won’t move.

As I said above, what I would do is get rid of debugdraw to start with, then it should be much easier to work with.

Thank you so much everyone! I may have Science in my name, but this physics stuff is beating me up pretty good. It works now thanks to all your help and comments