• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
AhmedCoeia

Getting distance between light source and four vertices

11 posts in this topic

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...

 

0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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!
2

Share this post


Link to post
Share on other sites

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 ?

0

Share this post


Link to post
Share on other sites

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!
2

Share this post


Link to post
Share on other sites

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... 

0

Share this post


Link to post
Share on other sites

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 )
2

Share this post


Link to post
Share on other sites

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

playerdistance can be from 1 to 80

0

Share this post


Link to post
Share on other sites
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
1

Share this post


Link to post
Share on other sites

Thanks for your help.

 

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));

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0