Algorithm for Direction Change upon Collision

Ender Nafi Elekcioglu endernafi at gmail.com
Fri Nov 8 04:29:29 EST 2013


Hi folks,

I’m dealing with this problem for a week now and I am officially stuck.
A little push, any insight would be great.

It’ll be a particle animation {sparkle effect} for an iOS app.
But for the base structure, I’m trying to make it dead simple;
so, think an Airhockey game in which there are ~20 pucks constantly colliding and changing direction.

TactileMedia’s excellent “bouncer2.rev” stack doesn’t help, ‘cause it’s performance falls short for my situation.
Try it with 4 balls instead of 2 and it starts to crawl. I need at least 20.
AnimationEngine doesn’t help much, its performance is not sufficient enough, likewise.


I had to write my own handlers.
Good news is, I managed to achieve 100+ balls at 30fps without collision detection and near 40 balls at 30fps with collision detection.
Bad news is, my collision handler doesn’t work correctly, hence this question :)

Here’s my algorithm:

* on openCard
-> create pucks
-> set its speed random as a custom property
-> set its destination / direction random as a custom property
-> put the long id’s into an array for further calculations

* on each screenUpdate
** for each puck
-> check any collision via intersect()
-> if yes, update the speed and direction of both collided pucks
-> move each puck {either with updated or standard params}


The problem is: collided pucks can’t get the chance to move in their fresh updated directions.
Apparently intersect() has 2 - 4 pixels threshold; actually it’s not a threshold, well, I don’t know what it is.
Probably, it’s related to the speed of the pucks and screen update delay.
I’ve recorded the stats and looked at the collided grcs’ rects; 
I see that they’re not precisely side-by-side. One’s right is 202 and the other one’s left is 198, for example.

Back to the problem:
They collide, 
collision handler updates their direction, 
but in next screen update they’re still collided because of the aforementioned issue,
so their directions are getting transposed again, and again, and again, … 
After 20 - 30 seconds, all pucks are gathered somewhere just trembling crazily.


I know that I have to create a flag as a custom property saying “this puck’s already handled, don’t check it” to the intersect() function.
But no matter which path I chose to accomplish this, it became dead end.

Any quick ideas, anyone?



Thank you very much,

~ Ender Nafi



More information about the use-livecode mailing list