0 votes

Hi guys,
Here's my problem, I use a directional light to simulate the sun in my game.
It works well a simple pi rotation along x axis allows me to simulate the sun path from sunrise to sunset, and it creates shadows accordingly to the sun position (= rotation as i use a directional light as i said previously).
Now how can i detect if a tile of the ground is in shadow or enlightened ?
My first idea would be to ask the directional light (but i don't know how to do it...)
My 2nd idea would be to cast a ray (which sounds useless to me as the directional light must have already performed a ray cast to produce light (and therefore shadows)). Whatever, if the way to go is to (re)cast such a ray, what would be the "from point" ?
I can't just take a random distance from the scene, as obviously the light and shadows depends on the distance from the directional light to the scene, meanwhile the doc describes a directional light as a light far from the scene... but far ain't a distance i can use as a "from point" far a raycast ;)
Help me please, i'm so lost here XD
Ty for your time guys

in Engine by (16 points)

3 Answers

0 votes

I don't think there is a simple way for the program to know details on these kind of effects, a shader may know if there is light but the rest of the game wont.


For the static part you know which gets sunlight at each light angle (you may need to do some external prototype with a more complete 3D tool or a real mockup to get started), you can create Areas or groups for them, then use rays for the moving ones only.

The rays from the moving tiles need to be as high as the lower obstacle at a low light angle, I guess.

ps: don't try to make it perfect, approximate to get a good feeling, you may find better techniques on the way.

by (7,832 points)
0 votes

A raycast seem to be a correct solution to me. Directional lights don't perform raycasts, they perform projections on the graphic card, which is different and not easy to get from gameplay code.

You can pick any position and throw a ray in the sky where the sun is, at a decently big distance (like camera's far clip distance). If it hits anything, then the point you started from is in shadow. You may want to correctly setup collision shapes, no need for exact match though.
You might also want to exclude transparent stuff or invisible walls, by putting them in a separate collision layer.

Also, you might want to refine what you mean by "tile in shadow". With directional lights, a tile can be fully lit, partially lit, or completely in shadow. If you use a raycast, it all depends from which position on the tile you cast it, as it will give you this information in one single point in space.
But you could find better solutions by thinking about your game rules ;)

by (27,601 points)
0 votes

Ok guys thanks for these details and for your time. I will try with a far away empty and a ray cast and try to fix the correct distance according to visual shadows area.
Or maybe i will try with some other kind of light as i want to be able to know which amount of light a tile receives (for fields and cultures..)

by (16 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.