• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Getting distance between light source and four vertices

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

### #1AhmedCoeia  Members

Posted 27 February 2014 - 10:06 AM

if I have a plane which has four vertices, as shown in the following figure. I would like based on the distance between the light source and positions of those four vertices, to scale( shrink or extend )the plane accordingly to that distance. What maths involved in that?

In plain words, I have a "directional" light source in the scene, and I have a plan with 4 vertices, I would like to shrink or extend ( scale) the four vertices positions, based on how far or how close I'm to the directional light. so that I can simulate a shadow...

### #2nobodynews  Members

Posted 27 February 2014 - 01:03 PM

Distance from where? Center of the 4 vertices? Nearest vertex? Nearest point of the nearest edge on the plane? Assuming you decide on a point, the way you calculate a distance in 3d is remarkably similar to calculating the distance in 2d. in 2d the distance is sqrt((x2-x1)^2+(y2-y1)^2) In 3d it's sqrt((x2-x1)^2+(y2-y1)^2+(z2-z1)^2) Where the x, y, and z coordinates correspond to the points of the light and wherever on the plane you picked. Order doesn't matter. Now, depending on how you plan on actually utilizaing the distance calculated you might not *need* to calculate the sqrt of the 3 squares. But without knowing more about what you're doing I couldn't advise either way.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

### #3cozzie  Members

Posted 27 February 2014 - 01:31 PM

Are you sure you want to use a directional light for this? Because that has no "position", it's "infinite". In that case I think you need some sort of reference to measure from. Another option could be to use a spot light.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

### #4AhmedCoeia  Members

Posted 27 February 2014 - 01:32 PM

Hi nobdoynews,

I have a directional light. That plane is a decal projected on the floor as a shadow. I have the 4 vertices of the decal. I would like based on the directional light on the scene, to modify, scale the 4 vertices according to that light, to simulate the shadow behaviour. The directional light in unity has a position. I do not what maths to do, so that I get a scale value, and scale the vertices of the plane.

@Cozzie,

Yea, I have a directional light in the scene, I only have its position in the scene.

Edited by AhmedCoeia, 27 February 2014 - 01:33 PM.

### #5Lactose  GDNet+

Posted 27 February 2014 - 01:42 PM

A directional light's position does not matter. It just uses the other settings (like angle, color, etc.), and places it infinitely far away.

If you put a directional light pointing straight down underneath a bridge, everything will be lit from above (including all the stuff that's above the bridge).

In short, distance to a directional light does not make sense.

Looking at the documentation, you might want to use either Point or Spot.

http://docs.unity3d.com/Documentation/Components/class-Light.html

As nobodynews said, getting a distance between two points (in this case, P1 and L) is done by summing up the difference in all dimensions, the taking the square root of that:

dist = sqrt( (P1.x - L.x)^2 + (P1.y - L.y)^2 + (P1.z - L.z)^2 )

---

EDIT: Corrected who I attributed distance to.

Edited by Lactose!, 27 February 2014 - 01:43 PM.

Hello to all my stalkers.

### #6AhmedCoeia  Members

Posted 27 February 2014 - 01:46 PM

but is there any other way to do that effect using directional light ?

I just want to shrink or extend the plane based on light or based on how far or how close that plane ?

### #7Lactose  GDNet+

Posted 27 February 2014 - 01:49 PM

With a directional light, no.

Everything is infinitely far away from a directional light. Thus there can be no concept of how far/close you are to the light.

Directional light does not have a position, even though it might look like it does in Unity.

---

EDIT: Meaning you'll have to use a different kind of light (which has a position), or you need to figure out a different way to do it (i.e. not based on your light).

Edited by Lactose!, 27 February 2014 - 01:50 PM.

Hello to all my stalkers.

### #8AhmedCoeia  Members

Posted 27 February 2014 - 01:52 PM

I made the following but it seems its working but doesn't make any sense...

I dot product the position of the light and the each vertex position, I got a scalar value, then I divided it by 1000, then I lerp between from 1 to 0, and t = final_scale

When I get farway, the plane gets shrinked...

### #9Kryzon  Prime Members

Posted 27 February 2014 - 03:54 PM

I don't remember any platform games changing the shadow size.
They would mostly fade the shadow away when the character is off the ground.

What you could do is perform raycast right below the player and, based on the distance that he is to the ground, fade the shadow away.

Const MAX_PLAYER_DISTANCE:Float = 50.0

...

shadowAlpha = 1.0 - ( Min( playerDistance, MAX_PLAYER_DISTANCE ) / MAX_PLAYER_DISTANCE )

### #10AhmedCoeia  Members

Posted 28 February 2014 - 02:51 PM

I'm interested to know how did you limit the shadowalpha by the above equation ?

playerdistance can be from 1 to 80

### #11Kryzon  Prime Members

Posted 28 February 2014 - 04:34 PM

It's the Min( a, b ) function. It returns the smallest of the two numerical arguments that you pass to it.

One of the arguments is the actual player distance to the ground directly below him, which is unknown and could be anything from 'zero' to 'infinity,' but always positive since the player is always above the ground even when touching it.
The other argument is the distance from the ground that I want the shadow to be completely faded. Anything less than this, the shadow will be partially visible to some degree.

With these arguments, the function will return something between 'zero' and 'max_player_distance,' which is mapped to the [0, 1] region by dividing it by the biggest of the known values, the maximum player distance. This yields 'result.'
Since the shadow should be more transparent the higher the player is, the ideal alpha value is the unit complement of 'result,' instead of 'result' itself ( alpha = 1.0 - result ).
The alpha value is '1.0' when the player is on the ground (fully visible), and 'zero' when he's at the maximum distance or greater (fully transparent).

- - - - -
Another way would be to replace the Min( a, b ) function with a Clamp( val, a, b ) function, which clamps 'val' between the 'a' and 'b' range: Clamp( playerDistance, 1, 80 )
These are common numerical functions that should come with whatever engine or library that you're using, but in any case this is their source:

Function Min:Float( a:float, b:float ) 'Or the ternary operation: (a < b) : a ? b'

If a < b Return a
Return b

End Function

Function Clamp:Float( val:float, a:float, b:float ) 'Range parameter "a" is assumed to be smaller than range parameter "b."'

If val <= a Return a
If val >= b Return b
Return val

End Function

Edited by Kryzon, 01 March 2014 - 02:48 PM.

### #12AhmedCoeia  Members

Posted 03 March 2014 - 03:08 AM

To make my self sure, If I have the alpha from 0 to 255, so my equation would be correct ?

byte fadingALpha = Convert.ToByte(255 - (Math.Min(distance, DistanceMax) / DistanceMax));

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.