World:rayCast() callback order

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
nfey
Citizen
Posts: 63
Joined: Tue Feb 18, 2014 9:50 am
Location: Romania

World:rayCast() callback order

Post 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.
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: World:rayCast() callback order

Post 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.
User avatar
nfey
Citizen
Posts: 63
Joined: Tue Feb 18, 2014 9:50 am
Location: Romania

Re: World:rayCast() callback order

Post 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 :)
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests