Page 1 of 1
collision detection
Posted: Tue Aug 25, 2009 10:18 pm
by synaptic
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!
Posted: Tue Aug 25, 2009 10:19 pm
by synaptic
Also, I do have animation engine, and have tried to use its fancy features, with the same result as above.
Posted: Wed Aug 26, 2009 3:32 am
by synaptic
I just realized that this should be in the Enterprise section... don't know how to move it...
Posted: Wed Aug 26, 2009 10:40 am
by bn
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
Posted: Wed Aug 26, 2009 7:43 pm
by malte
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