Page 1 of 1

World:rayCast() callback order

Posted: Sun Nov 30, 2014 8:23 pm
by nfey
Casting a ray through the map has big chances of hitting more than one entity. For each entity, the callback method is called.
The wiki says that:
You cannot make any assumptions about the order of the callbacks.
http://www.love2d.org/wiki/World:rayCast

However, I need to get these callbacks in a manner that allows me to make assumptions :). Specifically, I'm tracing a ray for a bullet's trajectory, starting from the gun. As the bullet travels on it's path, for each entity it encounters, I need to check against a chance to hit. If a hit is registered, that entity loses health. If the bullet misses, we keep tracing.

The solution I'm thinking about is to fill in a table with each ray collision, sort the entries based on distance to the gun ( or maybe the "fraction" parameter from the callback? ) and go from there.

However, won't this break down when I cast more than one ray at a time? Or is each World:rayCast() call a blocking call (that is to say, for each ray trace, all callbacks will be made before the World:rayCast() call ends)?

PS: I'm new to Lua and not very certain of how multi-threading is handled.

Re: World:rayCast() callback order

Posted: Mon Dec 01, 2014 4:02 am
by Azhukar
nfey wrote:Or is each World:rayCast() call a blocking call (that is to say, for each ray trace, all callbacks will be made before the World:rayCast() call ends)?
Yes. All related ray callbacks are called right after they rayCast in the same thread. This has nothing to do with multithreading, it's just a way of communicating with the framework.

Distance sort would be my solution too.

Re: World:rayCast() callback order

Posted: Mon Dec 01, 2014 9:31 am
by nfey
Sorry, I might not have been clear enough - I was thinking that each rayCast() call went to it's own thread; things would've been a bit more complicated if it were so.
Thanks for the clarification :)