Line of sight - Raycasting

From the Asset Store
Drawing Continuous Line
$0.50 USD
95% off
solution for games like "fruit ninja" and drawing applications
  • Hey folks!

    The official raycasting feature in the LOS behaviour is a nice addition, although i have run into a performance snag regarding how it calculates the raycasting hits. In my example i am using sprites as the FOV. So i create sprites "on start of layout" then i pin those to the player, and then cast to those points.

    My idea (and it's working nicely) was to create raycasting maps via sprite (that you draw), then capture that image with the canvas and populate it to an array (based on different color values etc). Then i create sprites for the walls (from the array), which i intended to use for the official raycasting.

    The issue is this: Since it seems that you are using the "overall" collision model for the actual raycasting, there is a HUGE performance hit when creating medium to large rooms. Since every wall is one sprite, and your collision model is using some sort of optimization (which works fine in most cases).

    I have only 128 rays, but alot of "2d" walls, that makes up the map. The idea of using raycasting is that in theory, the raycasting algo shouldn't be a big performance hog, since you are only testing the first collision (if you want). In Constructs behaviour you are obviously pre calculating stuff.

    Also, I only have 8 pixels of view distance. Still the performance is realy bad. The performance hit is already taken, even before you hit a wall!

    What i mean is, the number of walls shouldn't bog down performance barely at all. Only the amount of rays+distance used for the actual raycasting!

    Is there a better way to gain some performance perhaps?

  • My guess would be 128 rays running every tick.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • What??? Using 128 rays shouldn't be a problem. The real performance hog using the built in LOS raycasting is amount of walls, not rays. If you make a map in the layout editor, say a room of 4 walls, you can use native resolution! 1920x1080. 1920 rays! But as soon as you add more walls to the map, even if you are not hitting them with a raycast, you will get a MASSIVE performance drop.

    When i look at the debuger, it's even calculating collisions, even though nothing is in my FOV! In order words, it's running collision checks with the walls, even though im not hitting them with any rays!

  • Ormus Could you post a demo project? I made a quick test and with 200 rays cast on every tick, the difference in performance between 10 wall objects and 200 objects is not that big.

  • Sure.

    gofile.io/d/b0mY9r

  • The most effective way to massively improve raycasting performance is to test against a single tilemap object.

    EDIT: For moving objects, you can additionally test against these. For static obstacles I would rely on tilemap.

    EDIT 2: When testing against tilemaps, make sure to turn off collison cells on the LOS behavior.

  • Oh wow, you are right Colonel Justice! That was MUUUUCH faster! Thank you!

  • You're welcome.

    Another minor performance tip:

    You can calculate the brightness in the main event without needing the for each loop and evaluating the normal angle via OR conditions.

    Just do: 3dwall set brightness to 100 - (35 * abs(sin(ray.normalangle)))

  • Yea, i tried that also. A nice boost! Thank you again!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)