Shadow Cast collision detection?

0 favourites
  • 5 posts
From the Asset Store
Particles support animations, collisions, effects and etc.
  • Hi all, I am making a game that relies heavily on detecting when the player is in a shadow and when they are not. I've been working around it by making separate shadow objects and scaling them based on the position of the sun, but it's getting tedious.

    I looked into other methods and found some people saying you could use Line of Sight to simulate the shadow cast behavior, but how would the math work? Since the shadows grow/get longer as the light source moves, I can't wrap my head around using Line of Sight. You'd have to constantly update the range and cone of view.

    Does anybody have any suggestions on how I could achieve a shadow collision detection with the Shadow Cast behavior? It would save me a ton of time.

    Thanks!

  • Hi! You're right, you can use the Line of Sight behavior.

    Just like in real life, you know that a point is in shadow if there is an obstacle between the line that connects the point itself to the light source. We can use this to check if a specific point is on shadow or not. Simply check the LOS between the point coordinates and the sun coordinates.

    If you have a big sprite with complex geometry and you want to check if that sprite is on shadow, you need to repeat the same thing, but with all collision polygon points. If no collision point has line of sight with the sun, then the whole object is in shadow.

    Since Construct doesn't provide a simple way to get the collision polygon points in the event sheet, you can use image points placed at the position of the collision polygon points, and test if those points have line of sight with the sun.

    I made a small working prototype that implement this mechanic: dropbox.com/scl/fi/5ocbuher2dshq0yyz3wqr/Shadow-collision.c3p

  • That's super helpful, thanks. Only other thing is the sun in my game sometimes in close enough to the objects that the shadows aren't infinite (don't extend off the end of the screen). Could I somehow check if the player is standing in the shadow even when the sun doesn't have LOS? Maybe using the distance from the sun to the player and shadow height?

  • Yes that's doable. You need to work with both raycasting, distances and property of triangles.

    Here's the project revised for your needs: dropbox.com/scl/fi/o6yahpuq691w0bvw9edrx/Non-infinite-shadow-collision.c3p

    I made a IsOnShadow(x , y) function that you can use to check if a point is on shadow or not. It works by raycasting from the sun to the point and from the point to the sun, keeping track of the first raycast hit.

    Using this data + the height of the objects and the height of the light, you notice the following triangle similarity, calculate the amount of shadow projected onto the ground, and compare it with the distance from the point to the light exit point.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Works like a charm. I'm in the middle of a game jam, you definitely just saved my game. Thanks a ton!

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