collision detection

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
synaptic
Posts: 11
Joined: Thu Jun 04, 2009 10:47 pm

collision detection

Post by synaptic » Tue Aug 25, 2009 10:18 pm

Yup, it's me again:

I have two images moving randomly around. I would like them to (at the very least) stop if they touch. I'm not sure I understand the basics of how to detect collisions.

I've got something like

Code: Select all

on mouseup

put random(500), random(500) into LOC1
put random(500), random(500) into LOC2

move img "img1" to  LOC1 without waiting
move img "img2" to LOC2 without waiting

repeat until the loc of img "img1" = LOC1
  if intersect(img "img1", img "img2) then
  stop moving img "img1"
  stop moving img "img2"
end repeat

end mouseup
But when I run it, it doesn't do anything at all. Without the repeat part, the images move to where they are supposed to be without incident.... What have I fundamentally misunderstood this time?

Thanks!
Last edited by synaptic on Tue Aug 25, 2009 10:21 pm, edited 1 time in total.

synaptic
Posts: 11
Joined: Thu Jun 04, 2009 10:47 pm

Post by synaptic » Tue Aug 25, 2009 10:19 pm

Also, I do have animation engine, and have tried to use its fancy features, with the same result as above.

synaptic
Posts: 11
Joined: Thu Jun 04, 2009 10:47 pm

Post by synaptic » Wed Aug 26, 2009 3:32 am

I just realized that this should be in the Enterprise section... don't know how to move it...

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Post by bn » Wed Aug 26, 2009 10:40 am

Hi synaptic,
if you have animation engine then you could ask in the animation engine section.
Your script does not work because the repeat loop locks up the engine.
I changed your script to a non blocking one.
I declare 2 script local variables at the top of the script outside any handler. That way any handler in that script can access these variables. This way the final destination of img1 and img2 can be accessed.
Then I use a send in time construct to free Revolution from the lock-up. watchForCollision tests whether the images arrived at their destination, if not yet -> test for collision.

Code: Select all

local LOC1, LOC2
on mouseup
    put random(500), random(500) into LOC1 
    put random(500), random(500) into LOC2 
    
    move img "img1" to  LOC1 without waiting 
    move img "img2" to LOC2 without waiting 
     
    send watchForCollision to me in 130 milliseconds -- play with the intervall to let the two images get apart once they collided
end mouseup

on watchForCollision
    if loc of image "img1" <> loc1 or the loc of image "img2" <> LOC2 then
        if intersect(img "img1", img "img2") then 
            stop moving img "img1" 
            stop moving img "img2" 
            exit watchForCollision
        end if
        send  watchForCollision to me in 5 milliseconds
    end if
end watchForCollision
one problem with this is that once the images did collide then they have a tendency to collide again in the next move. In this version I allowed 130 milliseconds for the first collision test to let them get apart. You probably would want to test for collision before the move.
The subsequent tests for collision are done every 5 milliseconds until both images are at their final destination.

All that said, if you have animation engine then there are probably more elegant solutions then this one and I would try to get animation engine to work. (I dont have AE). And Malte (the author of animation engine) is very supportive.
regards
Bernd

malte
Posts: 1098
Joined: Thu Feb 23, 2006 8:34 pm
Contact:

Post by malte » Wed Aug 26, 2009 7:43 pm

Hi,

fwiw, here is an animationengine solution:

In the mouseUp handler:

Code: Select all

on mouseup
    local LOC1,LOC2
    put random(500), random(500) into LOC1
    put random(500), random(500) into LOC2  
    aeLockMoves
    aeMoveTo the long id of img 1,LOC1,1000
    aeMoveTo the long id of img 2,LOC2,1000
    aeUnlockMoves
end mouseup
At card level:

Code: Select all

on aeEnterFrame
    if intersect(img 1,img 2) then
        aeStopMoving the long id of img 1
        aeStopMoving the long id of img 2
    end if
end aeEnterFrame
cheers,

Malte

Post Reply