breaking joints

I use physics.joint which work fine in Codea, but when I use it in the Codea runtime as a xcode project, the joints seem to break (also in the 1.5 beta). Anyone seen anything similar? Is the implementation different or is it just coincidence that it happens?

-- connecting two circle bodys
function RigidJoint:init(a,b)
    self.a, self.b = a,b
    local v1,v2 = vec2(a.body.x, a.body.y), vec2(b.body.x, b.body.y)
    local mid = v1 + (v2-v1)*0.5
    
    self.w, self.h = (v1-v2):len(),50
    local hw,hh = self.w/2,self.h/2    
    self.body = physics.body(POLYGON,
            vec2(-hw,hh), 
            vec2(-hw,-hh), vec2(hw,-hh), vec2(hw,hh))
    self.body.x = mid.x
    self.body.y = mid.y

    local j = physics.joint(REVOLUTE, a.body, self.body, a.body.position) 
    local j2 = physics.joint(REVOLUTE, b.body, self.body, b.body.position)
end

Finally I figured it out! :slight_smile:

Don’t forget to save your joint objects somewhere, otherwise the garbage collector will remove them, and suddenly your joints break (or rather disappear). So when I changed the code above to


   self.j = physics.joint(REVOLUTE, a.body, self.body, a.body.position) 
   self.j2 = physics.joint(REVOLUTE, b.body, self.body, b.body.position)

Then the code started working correctly!

THANK YOU! I was really trying hard to understand what was going on with my code, suddenly I realized #body.joints was becoming ZERO at some random point. Could not figure out why until I find your post. Thank you a lot @tnlogy.

Happy to hear that my mistakes helped you! :slight_smile:

Thanks, I had the same problem, but in codea itself. Though I would expect the bodies the selfs to have references to the joints, since they have a joints property. Anyway, this does fix my problem, thx