physics question (collision)

Can someone explain what’s happening here. I have the green dot set up to follow my finger so I can push one of the red dots with it. If I stop pushing a red dot, I can’t push it again until I push the other one. It alternates like that until some point where I can’t push either dot until I restart the program. There are some times where I can push the same dot multiple times, but mostly I have to alternate. I don’t know why the collision isn’t working all the time with the same dot.


function setup()
    g1=physics.body(CIRCLE,40)
    g1.x=200
    g1.y=400
    g1.gravityScale=0  
    
    r1=physics.body(CIRCLE,40)
    r1.x=200
    r1.y=200
    r1.gravityScale=0
        
    r2=physics.body(CIRCLE,40)
    r2.x=200
    r2.y=600
    r2.gravityScale=0   
end

function draw()
    background(40,40,50)
    
    fill(0,255,0,255)    --green dot
    ellipse(g1.x,g1.y,80,80)  
      
    fill(255,0,0,255)    --red dots
    ellipse(r1.x,r1.y,80,80)
    ellipse(r2.x,r2.y,80,80)   
end

function touched(t)
    g1.x=CurrentTouch.x    -- position of green dot
    g1.y=CurrentTouch.y
end

Change that last little bit to

function touched(t)
    g1.x=t.x    -- position of green dot
    g1.y=t.y
end

And give it another try

I still get the same results.

.@Mark - doesn’t solve the problem.

@Dave1707. I suspect it’s something to do with the underlying physics routines. There is an underlying physics.contact object which can be at a point of collision, maintained contact or a point of separation. Manually overriding the position of the green circle is sometimes breaking the embedded physics by letting the green appear inside the red circle. Sorry if this isn’t explained very well

Ah. For some reason, if I bounce back and forth between both red circles, the problem doesn’t occur. If I pick on a single dot, it stops responding.

Interesting problem… Now I’ll be happy to hear from someone with a neat solution.

This happens due the bodies falling asleep. Just set sleepingAllowed on the bodies to false (true by default). Bodies are normally woken up when a collision occurs, however just setting the positions of the bodies doesn’t cause any forces to be applied, just overlap separation. I tested my solution and it works just fine now.

Thanks @John, that works just fine. I was also able to make it work by setting g1.gravityScale=.1 and I was getting ready to post that when I saw your post. I didn’t realize that’s what the sleepingAllowed was used for. Glad I had an example to show how it works.