Physics joints

I am trying to use the “REVOLUTE” joint type but the bodies that I am trying to attach just spring apart. I think the issue is that codea thinks the bodies have collided and pushed them apart Here is my code so far:


--# Main
--Main
function setup()
    
    FPS=0
    parameter.watch("FPS")
    ground = PhysicsEdge(vec2(0,50),vec2(WIDTH,50))
    box = PhyiscsPolygon(WIDTH/2,HEIGHT/2,
    {
    vec2(-50,-50/2),
    vec2(50,-50/2),
    vec2(50,50/2),
    vec2(-50,50/2)
    })
    wheel = PhysicsCircle(box.object.x,box.object.y,30)
    axle=physics.joint(REVOLUTE,box,wheel,vec2(wheel.object.x,wheel.object.y))
end

function draw()
    FPS=1/DeltaTime
    background(0)
    box:draw()    
    ground:draw()
    wheel:draw()
    
end

--# PhyiscsPolygon
PhyiscsPolygon = class()

function PhyiscsPolygon:init(x,y,v)  
    self.vertices = v
    self.object = physics.body(POLYGON,unpack(self.vertices))
    self.object.x = x
    self.object.y = y
    self.object.angle = 0
    
end

function PhyiscsPolygon:draw()
    pushMatrix()
    
    translate(self.object.x,self.object.y)
    rotate(self.object.angle)
    pushStyle()
    stroke(255)
    strokeWidth(5)
    
    local i= 1
    while i < #self.vertices do
        line(self.vertices[i].x,self.vertices[i].y,self.vertices[i+1].x,self.vertices[i+1].y)
        i = i + 1       
    end
    line(self.vertices[i].x,self.vertices[i].y,self.vertices[1].x,self.vertices[1].y)
    popStyle()
    
    popMatrix()
end

--# PhysicsCircle
PhysicsCircle = class()

function PhysicsCircle:init(x,y,r)
    self.radius = r
    self.object = physics.body(CIRCLE,self.radius)
    self.object.x = x
    self.object.y = y
    self.object.angle = 0
    
end

function PhysicsCircle:draw()
    pushStyle()
    pushMatrix()
    
    translate(self.object.x,self.object.y)
    rotate(self.object.angle)
    
    noFill()
    stroke(255)
    strokeWidth(5)
    ellipseMode(RADIUS)
    
    ellipse(0,0,self.radius)
    line(0,0,self.radius -5,0)
    
    popMatrix()
    popStyle()
end

--# PhysicsEdge
PhysicsEdge = class()

function PhysicsEdge:init(p1,p2)
    self.object = physics.body(EDGE,p1,p2)
    self.p1 = p1
    self.p2 = p2
end

function PhysicsEdge:draw()
    pushStyle()
    stroke(255)
    strokeWidth(5)
    line(self.p1.x,self.p1.y,self.p2.x,self.p2.y)
    popStyle()
end

function PhysicsEdge:touched(touch)
    
end

@Coder I didn’t look thru your code to figure out what’s wrong, but here’s an example that uses REVOLUTE. It might be better if you look thru this code and then try to make corrections to yours.


function setup()    
    a1=physics.body(CIRCLE,0)
    a1.x=0
    a1.y=0
    a1.type=STATIC
    
    c1=physics.body(CIRCLE,15)
    c1.x=40
    c1.y=700
       
    p1=physics.body(POLYGON,
        vec2(0,0),
        vec2(-100,-30),
        vec2(100,-30))
    p1.x=0
    p1.y=0
    
    p2=physics.body(POLYGON,
        vec2(0,0),
        vec2(-100,-50),
        vec2(100,-50))
    p2.x=100
    p2.y=300
    
    p3=physics.body(POLYGON,
        vec2(0,0),
        vec2(-100,-50),
        vec2(100,-50))
    p3.x=-30
    p3.y=350
  
    j1=physics.joint(REVOLUTE,a1,p1,vec2(0,0))
    j2=physics.joint(REVOLUTE,a1,p2,vec2(100,300))   
    j3=physics.joint(REVOLUTE,a1,p3,vec2(-30,350))      
end

function draw() 
    background(40, 40, 50)
    
    if c1.y<-200 then
        c1.y=700
        c1.x=40
        c1.linearVelocity=vec2(0,0)
    end
    
    pushMatrix()
    translate(200,200) 
    
    ellipse(p1.x,p1.y,2,2)                            
    ellipse(p2.x,p2.y,2,2) 
    ellipse(p3.x,p3.y,2,2)           
    
    ellipse(c1.x,c1.y,c1.radius*2)
 
    noFill()
    stroke(255)
    strokeWidth(2)
    
    xx=p1.angle
    rotate(xx)
    line(0,0,-100,-30)
    line(-100,-30,100,-30)
    line(100,-30,0,0)
    popMatrix()
    
    pushMatrix()
    translate(300,500)  
    xx=p2.angle
    rotate(xx)
    line(0,0,-100,-50)
    line(-100,-50,100,-50)
    line(100,-50,0,0)
    popMatrix()
    
    pushMatrix()
    translate(170,550)  
    xx=p3.angle
    rotate(xx)
    line(0,0,-100,-50)
    line(-100,-50,100,-50)
    line(100,-50,0,0)
    popMatrix()
end

I think the issue is something to do with the joint

@Coder there is no issue with the REVOLUTE joint as far as I can tell, I use it in my game all the time. The issue you have is that you are trying to make a joint between two lua objects (classes) not the body object you define inside it. physics.joint(REVOLUTE,wheel.object,box.object… is the correct way.

@Luatee thanks I thought it might be something to do with classes because I got it to work without them