Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

Physics joints - disconnecting?

in Questions Posts: 81

Hi all,

I realize a code sample would probably help but, in general: when/how can a physics joint (REVOLUTE type) become separated? It there some force calculation? If so, is there a flag I can set on the physics bodies or joint to prevent separation? I am sure that I’ve created the joint properly, and the separation behavior is not consistent. It will happen during one execution, and then not on the next. And more often the joint remains connected. But sometimes it doesn’t.

Thanks!

Tagged:

Comments

  • dave1707dave1707 Mod
    Posts: 7,749

    Look in the reference under physics.joint. There’s a variable called reactionForce. That might be what you’re after.

  • dave1707dave1707 Mod
    Posts: 7,749

    You didn’t provide an example, so here’s one of mine that uses REVOLUTE. Drag your finger around the screen. The joints stay connected. But then I don’t know how much force your putting on your joints.

    displayMode(FULLSCREEN)
    
    function setup()
        physics.continuous=true
        physics.gravity(0,0)
        p1=physics.body(CIRCLE,10)
        p1.x=200
        p1.y=320
    
        p2=physics.body(CIRCLE,5)
        p2.x=200
        p2.y=300
        p3=physics.body(CIRCLE,5)
        p3.x=130
        p3.y=300
        p4=physics.body(CIRCLE,5)
        p4.x=270
        p4.y=300
    
        p5=physics.body(CIRCLE,5)
        p5.x=200
        p5.y=200
        p6=physics.body(CIRCLE,5)
        p6.x=100
        p6.y=200
        p7=physics.body(CIRCLE,5)
        p7.x=300
        p7.y=200
        jnt1=physics.joint(REVOLUTE,p1,p2,p1.position)
        jnt2=physics.joint(REVOLUTE,p2,p3,p2.position)
        jnt3=physics.joint(REVOLUTE,p2,p4,p2.position)
        jnt4=physics.joint(REVOLUTE,p2,p5,p2.position)
        jnt5=physics.joint(REVOLUTE,p5,p6,p5.position)
        jnt6=physics.joint(REVOLUTE,p5,p7,p5.position)
        fill(255)
        stroke(255)
        strokeWidth(2)
    end
    
    function draw()
        background(0)
        ellipse(p1.x,p1.y,20)
        ellipse(p2.x,p2.y,10)
        ellipse(p3.x,p3.y,10)
        ellipse(p4.x,p4.y,10)
        ellipse(p5.x,p5.y,10)
        ellipse(p6.x,p6.y,10)
        ellipse(p7.x,p7.y,10)
        line(p1.x,p1.y,p2.x,p2.y)
        line(p2.x,p2.y,p3.x,p3.y)
        line(p2.x,p2.y,p4.x,p4.y)
        line(p2.x,p2.y,p5.x,p5.y)
        line(p5.x,p5.y,p6.x,p6.y)
        line(p5.x,p5.y,p7.x,p7.y)
    end
    
    function touched(t)
        if t.state==MOVING then
            p1.linearVelocity=vec2(t.deltaX*20,t.deltaY*20)
        end
    end
    
  • Posts: 71

    Hey, @dave1707, I thought your revolute man needed some boundaries. It's fun watching him dance around the screen.

    -- Revolute-Man V2
    -- Inspiration by: dave1707
    
    -- Mods by Scotty
    
    displayMode(STANDARD)
    
    function setup()
        print("Use touch to set Revolute-Man in motion.")
    
        parameter.integer("use_accelerometer", 0, 1,0)
    
    
    
        X = WIDTH/2
        Y = HEIGHT/2
        neck     =  20
        arm      =  70
        body     = 180
        leg      = 100
    
        physics.continuous=true 
        physics.gravity(0,-10)
    
    
        p1=physics.body(CIRCLE,10)
        p1.x=X
        p1.y=Y
    
        p2=physics.body(CIRCLE,5)
        p2.x=X
        p2.y=Y-neck
    
        p3=physics.body(CIRCLE,5)
        p3.x=X-arm
        p3.y=Y-neck
    
        p4=physics.body(CIRCLE,5)
        p4.x=X+arm
        p4.y=Y-neck
    
        p5=physics.body(CIRCLE,5)
        p5.x=X
        p5.y=Y-neck-body
    
        p6=physics.body(CIRCLE,5)
        p6.x=X-leg
        p6.y=Y-neck-body
    
        p7=physics.body(CIRCLE,5)
        p7.x=X+leg
        p7.y=Y-neck-body
    
        jnt1=physics.joint(REVOLUTE,p1,p2,p1.position)
        jnt2=physics.joint(REVOLUTE,p2,p3,p2.position)
        jnt3=physics.joint(REVOLUTE,p2,p4,p2.position)
        jnt4=physics.joint(REVOLUTE,p2,p5,p2.position)
        jnt5=physics.joint(REVOLUTE,p5,p6,p5.position)
        jnt6=physics.joint(REVOLUTE,p5,p7,p5.position)
    
        fill(255)
        stroke(255)
        strokeWidth(2)
    
        lineWidth = 4
        -- Draw a boarder, one pixel in, using the same values for vec2() and lines.
        -- Going CCW around the screen we have:
    
        LLX = lineWidth/2
        LLY = lineWidth/2
    
        LRX = WIDTH-lineWidth/2
        LRY = lineWidth/2
    
        URX = WIDTH-lineWidth/2
        URY = HEIGHT-lineWidth/2
    
        ULX = lineWidth/2
        ULY = HEIGHT-lineWidth/2
    
        CreateWalls() -- this will create all the required walls or bountries. CALLED FROM setup()
    
        defaultGravity = physics.gravity()
    
    end
    
    
    function draw()
    
        if use_accelerometer == 1 then
            physics.gravity(Gravity)
        else
            physics.gravity(defaultGravity)
        end
    
        -- Draw Revolute-Man
        background(0)
        fill(255, 255, 255, 255)
        stroke(255, 255, 255, 255)
        strokeWidth(2)
        ellipse(p1.x,p1.y,20)
            --face
            pushStyle()
            fill(0, 0, 0, 255)
            stroke(0, 0, 0, 255)
            ellipse(p1.x+4,p1.y+4,4)
            ellipse(p1.x-4,p1.y+4,4)
            ellipse(p1.x,p1.y-4,8)
        popStyle()
        ellipse(p2.x,p2.y,10)
        ellipse(p3.x,p3.y,10)
        ellipse(p4.x,p4.y,10)
        ellipse(p5.x,p5.y,10)
        ellipse(p6.x,p6.y,10)
        ellipse(p7.x,p7.y,10)
    
        line(p1.x,p1.y-8,p2.x,p2.y)
        line(p2.x,p2.y,p3.x,p3.y)
        line(p2.x,p2.y,p4.x,p4.y)
        line(p2.x,p2.y,p5.x,p5.y)
        line(p5.x,p5.y,p6.x,p6.y)
        line(p5.x,p5.y,p7.x,p7.y)
    
    
        -- Draw four sides (different colors so you know what's going on)
        strokeWidth(lineWidth)
        stroke(255, 0, 0, 255)    
        line(LLX,LLY,LRX,LRY) -- sending same two x,y pairs as sent to CreateWall()
        stroke(255, 0, 204, 255)        
        line(LRX,LRY,URX,URY)
        stroke(134, 0, 255, 255)    
        line(URX,URY,ULX,ULY) 
        stroke(0, 255, 55, 255)        
        line(ULX,ULY,LLX,LLY)       
    
    end
    
    
    
    function touched(t)
        if t.state==MOVING then
            p5.linearVelocity=vec2(t.deltaX*20,t.deltaY*20)
        end
    end
    
    
    
    -- I can't take full credit for the following two functions.
    -- This function() calls CreateWall() which in turn creates a physical edge-type body.
    -- Sending the same values as you would use to draw lines on top of the edges.
    function CreateWalls()
        local r=2.5
        leftWall   = CreateWall(LLX,LLY,ULX,ULY,r) -- sending two x,y pairs and restitution
        rightWall  = CreateWall(LRX,LRY,URX,URY,r) 
        bottomWall = CreateWall(LLX,LLY,LRX,LRY,r) 
        topWall    = CreateWall(ULX,ULY,URX,URY,r)
    end
    
    
    -- This function() does the actual creation of a physical edge-type body.
    -- Line locs are converted into vecs().
    function CreateWall(x,y,x1,y1,r)
        local w = physics.body(EDGE,vec2(x,y),vec2(x1,y1)) -- using two vec2() pairs
        w.restitution=r
        return w
    end   
    
  • dave1707dave1707 Mod
    Posts: 7,749

    @Scotty I like it. Makes it a lot more interesting to watch.

  • Posts: 71

    Thanks

Sign In or Register to comment.