
[Math] Vectors 
Onewing
Member #6,152
August 2005

I'm having some problems with my vector math. I think the problem is in setting them up. From the screenshot in the linked thread, you see how it lights incorrectly, shading the light side and lighting the shaded side. When I make the Z part of the Sun vector negative, it seems to shade the right side (however, it completely blacks out the other side with no shading at all). A vector has magnitude, and I think that is where I'm getting confused. In the code from the linked thread, you see I'm setting the Sun vector up to be just the position of the sun at (sun, 240, MAX_HEIGHT), where sun is the position of the sun on the horizontal axis and 240 is the screen resolution divided by 2. But, isn't that like drawing an arrow from (0,0,0) to (sun,240,MAX_HEIGHT), considering it's a vector? Should the sun vector actually be (0, 0, MAX_HEIGHT)? And what exactly is the DotProduct producing for two vectors? The CrossProduct makes sense to me.  
Arvidsson
Member #4,603
May 2004

Quote: And what exactly is the DotProduct producing for two vectors? It produces a scalar quantity. It is also closely knit to lengths and angels between the two vectors being multiplied.

XG
Member #856
December 2000

You can sort of think of it as the projection of one vector onto the other scaled by the other's length. If both vectors are normal, it's the cosine of the angle between them.  
Evert
Member #794
November 2000

Mathematically, a vector is an element of a vector space. As a concrete example, think of the real euclidean plane (), which is a vector space consisting of points (ie, a tupel of two real numbers) commonly called the x coordinate and the y coordinate. Almost by definition, these points are vectors (being elements of the vector space). Quote: And what exactly is the DotProduct producing for two vectors?
Geometrically, it's related to the length of the orthogonal projection of one vector onto the other (draw a square with one diagonal; then the sides of the square are the orthogonal projections of the diagonal). Loosely, it's how much of one vector is in the other one. If the vector you are projecting on, you get the component of the vector along the direction of the unit vector. 
Onewing
Member #6,152
August 2005

Thanks all for the help. I understand what the concept a little better, but it's not so natural as say, algebra. Somethings still are kind of foggy. I'm working on the Lambert Shading algorithm, if perhaps anyone has some advice. I've got a flat landscape with a pyramid structure on the top left. The flat ground seems like it is shading right, but the pyarmid looks whack. Also, if I raise the sun vector to a height of y, the ground goes black and only the pyramid lights up. http://comp.uark.edu/~spsilve/terrain11.JPG Any ideas on what's happening? If I need to post some code, let me know (it's still about the same as the code in the linked thread with some minor tweaks). Thanks.
 
Sebastian Mineur
Member #6,943
February 2006

I think the problem is with the points you use for calculating the slope normal. Depending on the order in which you pass them to your cross product function, the resulting vector will either point up, towards the sky, or down, into the ground. I must say that I think your project looks interesting, and it was fun to follow the progress in your threads. Good luck. 
Onewing
Member #6,152
August 2005

Quote: I'm not sure if that answers the first part of your question because I didn't actually understand it, but I hope the answer is in here. Yes and no. I have the position of my sun in the (x,y,z) space. Since the Lambert Shading algorithm requires the Sun to be a vector, I wasn't sure if I should think of it as a vector going from the origin to the position of the sun or if I should think of it as a vector going from the sun down to the planet (like a ray of light). I've since come to the conclusion that it is option 1, considering a vector going down would not produce the proper light direction. Quote: I think the problem is with the points you use for calculating the slope normal. I've thought that too. I'm going to try pluggin' some numbers down on paper and see if that is in fact the culprit. However, if it is, I'm not sure how I'm going to set up the points needed for the vectors. Quote: and it was fun to follow the progress in your threads. Thanks. Fladimir really got me going in the opposite direction though. I originally planned to have grass, wind and clouds in the game by this weekend. Guess I'm going to have to step it up!  
XG
Member #856
December 2000

Quote: considering a vector going down would not produce the proper light direction. That's exactly what such a vector would do. Not having actually read the algorithm, I am surmising that it expects the light direction. Since the rays of sunlight are mostly parallel when they hit the earth, one can approximate the sun as being infinitely far away and represent all incoming light with a single light direction; note direction. Most shading algorithms expect the vector from the light source to the point. Thus, your light vector should point downwards, into your planet.  
Onewing
Member #6,152
August 2005

I'm using the vector from the origin to the point being shaded, vPoint = {x,y,z}, and the vector from the origin to the sun, vSun = {sun_x,sun_y,sun_z}, and calculating the light direction by: lightDir = vSubtract(vPoint, vSun); If vSun were going straight down as in {0,0,5000}, then lightDir would be: {x,y,z  5000}. Also, I wouldn't be able to actually position the sun anywhere. Quote: one can approximate the sun as being infinitely far away and represent all incoming light with a single light direction Scratch what I said above, your saying lightDir should be vSun, yes? That way, the light direction is always coming straight down, like {0,0,5000}. I'll fiddle with it and see what I can get, although it's going to get a little tricky because my world is both flat and spherical at the same time (which is mathematically impossible).  
XG
Member #856
December 2000

Quote: Scratch what I said above, your saying lightDir should be vSun, yes? That way, the light direction is always coming straight down, like {0,0,5000}. That's the usual approximation when dealing with illumination from things like the sun, although you might want to tilt it slightly to the side to make it look more realistic. (Look at the sun at noon. It's not directly overhead. The same goes for any time of day.)  
Onewing
Member #6,152
August 2005

Quote: although you might want to tilt it slightly to the side to make it look more realistic. First I got to get it working, then I might. I was also thinking about how I was going to make it work for my layout. The entire planet is supposedly flattened out to fit the screen resolution. Hence, at 0 = X = MAXX + 1. Thus, at each point away from the sun's position, the sun is a little further away from the planet. Perhaps the Z of vector lightDir can be figured by the distance the sun is from the point to be shaded/lighted. A possible graphical explanation: http://comp.uark.edu/~spsilve/sun.JPG [edit]  that 'GFX_W' on the graph should read 'MAXX'.  
XG
Member #856
December 2000

Your planet has extremely weird geometry. At first I thought it looked like a cylinder, but that can't really be right either.  
Onewing
Member #6,152
August 2005

I said: my world is both flat and spherical at the same time (which is mathematically impossible). Perhaps I'll make a little more realistic world for Cosmos 2 (and probably use OpenLayer).  
XG
Member #856
December 2000

Point is, without a proper geometrical representation of it, you can't get "realistic" lighting. I can't help you there.  
Onewing
Member #6,152
August 2005

Personally, I really don't think this: Quote: you can't get "realistic" lighting. Fake lighting is okay, especially since it is not an integral part of the game and can be turned off. Quote: I can't help you there. What are you talking about? You can and have! If I get a chance to work on Cosmos 2, I can make it more believable and realistic from the knowledge here.  
Tobias Dammers
Member #2,604
August 2002

As long as you do nothing but topdown view, a simple emboss filter might be all you need. Or just use the slope in 45degdirection to calculate a light intensity (full intensity at full upleft slope dir, zero at lowright dir).  
Onewing
Member #6,152
August 2005

http://comp.uark.edu/~spsilve/terrain11a.JPG I think I got the shading algorithm to work, even though I'm not going to use it. It's nice just to know that I can get it to work for future projects. Although, when the light source is directly above a hill (or pyramid...), it doesn't light up. There has to be somewhat of an angle or it doesn't light up for some reason. Oh well, thanks for the help everyone (and have some credits). You will also get some representation in the project as well. [edit] Quote: Or just use the slope in 45degdirection to calculate a light intensity (full intensity at full upleft slope dir, zero at lowright dir). I'm not sure what you mean by "full upleft slope, zero at lowright dir".  
Tobias Dammers
Member #2,604
August 2002

Polar coords, my friend. Translate x and y slopes into direction and elevation. At upleft direction (mathematically: PI*3/2 or 135 deg), the light intensity will be at max.  
