[SOLVED] Physics body drawing problem

I am trying to create a simple brick class.
Here is what I am using to render the brick:

    pushMatrix()
    strokeWidth(0)
    fill(255, 45, 0, 255)
    translate(self.body.x, self.body.y)
    rotate(self.body.angle)
    rect(0,0,self.w,self.h)
    popMatrix()

(sorry for no code tags, don’t know how) EDIT: Added code tags (Simeon)

This works well for one brick, but a second doesn’t. Anything I’m doing wrong?

Might be easier to see if you posted your full code. I can’t see anything particularly wrong with that, but it might be to do with interaction between different bits of the code.

Sure thing:


Brick = class()

function Brick:Setup(x,y,w,h,r,bounciness)
    -- you can accept and set parameters here
    self.x = x
    self.y = y
    self.w = w
    self.h = h
    self.r = r
    
    self.body = physics.body(POLYGON, 
                vec2(0,0),vec2(0,h),vec2(w,h),vec2(w,0))
    
    self.body.x = x
    self.body.y = y
    
    
    self.body.angle = r
    
    self.body.restitution = bounciness
    
end

function Brick:Update()
    
    self.x = self.body.x
    self.y = self.body.y
    
    self:draw()


end

function Brick:draw()
    pushMatrix()
    strokeWidth(0)
    fill(255, 45, 0, 255)
    translate(self.body.x, self.body.y)
    rotate(self.body.angle)
    rect(0,0,self.w,self.h)
    popMatrix()
end

And then main



function SetupBricks()
    
    b1 = Brick -- create the brick
    -- setup the brick. 
    -- arguments: x position, y position, width, height, rotation 
    --(in degrees), bouciness
    -- about bounciness - 1 would mean it bounces as high as where it 
    --fell from.
    -- colour coming soon, just working on an easy to use system
    b1:Setup(300,300,30,150,40, 0.2)
    
    b2 = Brick
    b2:Setup(450,450,30,150,40,0.3)
end

-- This function gets called once every frame
function draw()
    
    -- This sets a sky blue background color 
    background(124, 164, 176, 255)

    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
    
    DrawEnvironment()
   
    b1:Update()
    b2:Update()
  
    
end

function DrawEnvironment()
    
    strokeWidth(0)
    fill(148, 114, 56, 255)
    rect(-1,-1,WIDTH+30,80)
    fill(40, 130, 29, 255)
    rect(-1,75,WIDTH+2, 20)
    
end

function setup()
    supportedOrientations(LANDSCAPE_ANY)
    physics.gravity(0,-200)
    SetupBricks()
    local floor = physics.body(POLYGON, vec2(0,0),vec2(WIDTH,0),vec2(WIDTH,95), vec2(0, 95))
    floor.type = STATIC
    floor.angle=0
    floor.y = -1.5
end


Thanks in advance.

Ah I think I spotted it. You are creating your bricks like this:

b1 = Brick

When they should be created like this.
(Even if they have no constructor arguments.)

b1 = Brick() 

The reason the first method doesn’t work is because you are setting both b1 and b2 to the same class metatable, whereas calling Brick() will actually create a new brick instance and return it to you.

without init it was using the same object

function Brick:init(x,y,w,h,r,bounciness)
 b1 = Brick(300,300,30,150,40, 0.2) -- create the brick
    
    --b1:Setup(300,300,30,150,40, 0.2)
    
    b2 = Brick(450,450,30,150,40,0.3)

Edit: I’m a bit slower than @Simeon

@Ipda41001 “init” isn’t strictly necessary, but the function call parentheses used to instantiate a class name are necessary.

However this is what “init” is for, so I’d recommend using it as @Ipda41001 suggests.

Thanks you guys, both of your methods worked perfectly.

@micha3l, I’ve been messing around with some of the physics code offered on this forum, and yours is the first one that has worked for me (and is basic enough for me to grasp)!

I just had a couple of questions: why did you use rect() for your floor instead of a points loop? Why are some of the floor numbers negative, and what purpose does floor.y serve? Lastly: why is your update() function necessary and what does it do? (I haven’t found a need for such a function in my code, but yours sure seems to work!)

If you have time to illuminate a very novice programmer on this, I’d be most grateful. Thanks!