(SOLVED) How to check if an object is in the light or not?

Now I’m not sure if this even possible but if it is, how do you think it is possible?
I need to hear your ideas around this.

I want something accurate with a moving light (Not static) like this:

If the object is in the light then do this
If the object is not in the light then do that

Again the light object is moving not static.

Need to hear your thoughts on this

Thanks as always : )

I think you can check if the distance between Light and Object is below the value of your Light’s radius then
Do your action

1 Like

That doesn’t take into account the light obstacles.

You could fire a raycast between your light source and the target object, checking against light obstacles as well as the target. However, if only a portion of the object is in the light, it may not get detected until the target point of the raycasting is in line. You could fire off multiple raycasts - to the left, right and centre of the target, but it still won’t be perfect.

1 Like

@Muzan I already did that but it’s not working correctly unfortunately.

@MrMen Your idea sounds like a good start, I never used raycasting before so I’ll take a look if there is an example then if I found one I’ll go from there and test it out, but again I’m not really convinced that it will be perfect like you mentioned but raycasting could be a start, anyway If you have a good example about raycasting let me know about it so I can check it out.

use collision(in my project using collisions work(and yes the light object is not static)))

1 Like

@darkshadow How? could you explain more in details?

use the collision condition(it worked for me in my testing)

you could do an all direction ray cast with a limit that matches the radius of your light
there’s a way to do the all direction, but i can’t think of it off the top of my head

No, that doesn’t work for the shadow. It’s just using the Bounding Box, and detecting a collision if it’s within that. If you place a light obstacle in the way, and the target is in the shadow, it still detects a collision.

I don’t believe this is possible (especially not in GDevelop), for the simple fact that a raycast is a ray (single line) projected from a source to a target position or in a direction. You cannot do a single line all around. It would require a vast number of raycasts. And too many raycasts are a processing burden.

@MrMen I think @LoafKat was talking about this
image

Will this work with the solution you gave @MrMen?

That just the standard raycast action, and in this case, a source position to a target position. It is one that can be used in my solution.

It’s not a raycast to all objects but a raycast action that can be used by any object.

@CorianderGames you could try something like this:

for each enemy
enemy distance to player less than x
five "ray-cast to position rom the enemy’s four corners and center

and @Muzan yes that’s what I meant, I don’t use ray-cast all that much

It is very unusual there are no expressions to get parameters from Lighting layers/light objects. However, what you could do, if you know the light radius of an object (or set it via a varaible):

  1. Pick the nearest light source object to the player. (add all of the lightsources into a group to make this easier). Add all of the lightsources AND obstacles to a separate object group.
  2. Raycast from the player to the nearest member of the “all light objects + obstacle group”. Set up a condition that ensures the object hit IS NOT an obsticle.
  3. Ensure the raycast’s maximum distance is the light radius.

If all of the conditions are met, the player is in the light radius. If all of the conditions aren’t met, the player isn’t in the light radius.

1 Like

@Silver-Streak I like you idea even if it’s not going to be accurate but I want to test this so bad and see if it works, thing is I’m trying at the moment but I just don’t get it that well as It’s almost my first time using raycasting, I know how raycasting works and the idea behind it … but I never used it before so I’m a bit confused:

First of all because I’m so confused let’s work with only 1 light source, 1 obstacle and the player
so let’s edit your idea around only these 3 objects as follows:

  1. Pick the nearest light source object to the player. Add the light source AND obstacle to a separate object group.

  2. Raycast from the player to the nearest member of the “group mentioned above”. Set up a condition that ensures the object hit IS NOT an obsticle.

  3. Ensure the raycast’s maximum distance is the light radius.

.
.

Now let’s dissect what I did and take it one step at a time:

  • Pick the nearest light source object to the player. Add the light source AND obstacle to a separate object group.

Here what I did:

So like I mentioned above I have 1 light source, 1 light obstacle and the player, here is the whole scene running in preview:

Then I have the group which have both the light source and the light obstacle here:

I added the event you mentioned here but I don’t think it’s not needed if we’re working with just 1 light source for now:

.
.

  • Raycast from the player to the nearest member of the “group mentioned above”. Set up a condition that ensures the object hit IS NOT an obsticle.

Now let’s slow things down here and take it step by step because I have questions here…

“Raycast from the player to the nearest member of the “group mentioned above””
How to say that in events?
I have 2 raycast events, one with an angle and one with a position, which one to use? and in both cases what is the angle? and what is the position?
Here is what I did but I have no idea if it’s correct:

“Set up a condition that ensures the object hit IS NOT an obsticle.”
I have no idea how to check if a ray hit an object or not using events?

.
.

  • Ensure the raycast’s maximum distance is the light radius.

This one is easy and if you’re talking about raycast’s maximum distance then I think you mean the raycast event with the angle but like I mentioned above I don’t know what to put in the angle section?

At the moment I’m stuck here:

What does it do basically is change the player color when he is in the light but not when he is under the platform again and it’s far from being accurate but at least it’s a start…

Anyway I’ll be waiting for your answers and explanation on what to do next and if you have an example or if you could make a quick example yourself explaining this I’d really appreciate it.

Also if you want the project file I can send it to you so you test it yourself and add to it and so on…

Anyway I’m waiting for your input on this…
Thanks for helping.

Hmm…thinking about this, I would invert my original thought. I’m pulling some of this from the “basic AI with pathfinding” example…

  1. Add the player and all light obstacles to a group. (We’ll call this “LightTargets”)
  2. Add all of the lights to a group. (We’ll just call it “LightsGroup”)
  3. Set up your Pick event to be "Pick the LightsGroup that is nearest to Player.X();Player.Y()
  4. Cast the ray from LightsGroup.X()/Y() angle LightsGroup.AngleToObject(LightsTarget), max distance (your light radius here), against LightsTarget
  5. Add a subevent. In the conditions, add Number of Player > 0.
  6. Move your “Change tint of player” action to this subevent.

I tested with the lights example and it works for me. You might need to tweak the max distance a bit higher or lower for your desired effect.


https://game-previews.gdevelop-app.com/1633653488892-80822/index.html

1 Like

@Silver-Streak Just tested it but it’s not working correctly…
What you’ve done can be basically done using a normal collision event with the light or not but what I want is this:

When the player in the light >> change the color to red. (Or do anything else, I’m just using color to check if it’s working)
But when the player is in the shadow >> change the color to blue.

Here is what I mean by light and shadow in this picture:
ray1-1

Also the light is moving left to right for example so it’s affecting the shadow angle and light angle both at the same time, I know it’s complicated sorry but I feel like it’s possible, just how is beyond me at the moment…

Hope you got another idea, the example you made doesn’t make a difference between shadow and light so it doesn’t care about if the player is standing in the shadow or light, it just works when the distance between the player and light is less than “150 for example”

I also noticed that it’s not accurate when to change the color of the player like in here, you see the player standing next to the light but no color change is happening:
ray6

But then you move him at the edge but on a diffirent angle and he changes his color:
ray7

What is going on? because I’m noticing this too in my example…

I’ll be waiting for your response and I really hope you have another idea for this or something…

Not much else information that I can offer. I tested it a few different ways and it worked as expected like the above.

image
image

The thing to keep in mind is that it’s going to raycast from the upper left corner of the player sprite, so you might be better off trying from the center of the sprite rather than just X/Y(), and you will need to adjust your distance you’re casting the Ray because it won’t be 1:1 the radius of the light.

For an inverse option (when fully out of the light), you probably just need to select the nearest light to the player and check that it is over X pixels away (whatever number you come up with for the above plus 10-20 pixels or so), or just use the same conditions as above but do <1 instead of >0.

Edit: you could also try doing a “For each lightobjectgroup” event, and just testing it’s distance to the player, THEN do the raycast tests, but I don’t know what that’ll do performance wise.

Hey @CorianderGames, is this what you’re after (red is full shadow, blue if any light is on the object)?


Shadows


It’s a little hacky, but it does the job.

All I did was define 8 points around the object, and if the light overlaps the object (i.e. is in collision with it), then fire a raycast to each point to see if it hits it cleanly :



2 Likes

This is super smart, I forgot that the boundary boxes of lights usually encompasses the radius of the light (and some extra room) as well.

This method might need a “For each lightsource” event due to potential overlaps, but that’d require more detailed testing than I can complete. I think MrMen’s solution is likely to be the most accurate.

1 Like