But this is not me sending all those 800 messages. I just call physics.ray_cast 400 times per frame. The engine does the rest.
I use raycasting as main collision prevention technique. Please don’t mention physics-based solutions — this does not work. Defold does not offer any other collision detection system. So raycasting is the only option if you want to keep integration with the editor.
It is a good option. Almost any Unity platformer 2d asset use raycasts this way.
300…400 raycasts per frame is a pretty normal sutuation in any action-intensive platformer game.
Batching probably can help. It will result in one batch(~8-14 raycasts) per moving entity per frame. Batching large blasts (with many debris pieces) may be also possible.
Sending raycast responses in one packet(not one by one) will also help. Right now almost any code for response handling begins with this:
function raycast_response(self, message)
self.responses[message.request_id] = message
if message.request_id < self.total_ray_count then
return
end
-- all responses are now collected, let's sort them out
...
end
Also good addition — ability to redirect responses to another script component. This may help (sometimes) with logic and code readability.
I do this now by using __dm_script_instance__
__dm_script_instance__ = get_context(self.edge_checker_context)
ray_cast(ray_start, ray_end, GROUND, 1)
__dm_script_instance__ = self
But all of this looks like Kludge - Wikipedia
The only real solution is synchronous raycasts, as @Mathias_Westerdahl says. Synchronous raycasts in turn will allow very nice code level optimizations, that will results in 40%…50% less raycasts. Right now we forced to cast all rays upfront.
Thanks for looking into this.