Allegro.cc - Online Community

Allegro.cc Forums » Game Design & Concepts » [Math] Vectors

Credits go to Arvidsson, Evert, Sebastian Mineur, Tobias Dammers, and X-G for helping out!
This thread is locked; no one can reply to it. rss feed Print
[Math] Vectors
Onewing
Member #6,152
August 2005
avatar

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.

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Arvidsson
Member #4,603
May 2004
avatar

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.

<math>\theta = arccos \Big( \frac{\bar{a} \cdot \bar{b}}{|\bar{a}||\bar{b}|}\Big)</math>

X-G
Member #856
December 2000
avatar

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.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Evert
Member #794
November 2000
avatar

Mathematically, a vector is an element of a vector space. As a concrete example, think of the real euclidean plane (<math>\mathbb{R}^2</math>), which is a vector space consisting of points <math>(x, y)</math> (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).
In down-to-earth terms, the vector <math>(x y)</math> is the vector that takes you from the origin of your coordinate frame to the point <math>(x, y)</math> in the plane.
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.

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.
The length of a vector (or norm) is defined as the square root of the inner product of the vector with itself. It can be shown that for two vectors the inner product never exceeds the product of the norms of the two vectors (if I rememeber correctly that's called the Cauchy-Schwarz inequality). This can then be parameterised as <math>\vec v \cdot \vec w = v w \cos(\vartheta)</math> where this defines <math>\vartheta</math>, which can be interpreted as the angle between the two vectors. You can check for yourself that this corresponds to your normal understaning of the angle between two lines in a two dimensional plane.

Onewing
Member #6,152
August 2005
avatar

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.

:)

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Sebastian Mineur
Member #6,943
February 2006
avatar

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
avatar

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!

------------
Solo-Games.org | My Tech Blog: The Digital Helm

X-G
Member #856
December 2000
avatar

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.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Onewing
Member #6,152
August 2005
avatar

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

------------
Solo-Games.org | My Tech Blog: The Digital Helm

X-G
Member #856
December 2000
avatar

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

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Onewing
Member #6,152
August 2005
avatar

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

------------
Solo-Games.org | My Tech Blog: The Digital Helm

X-G
Member #856
December 2000
avatar

Your planet has extremely weird geometry. At first I thought it looked like a cylinder, but that can't really be right either.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Onewing
Member #6,152
August 2005
avatar

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

------------
Solo-Games.org | My Tech Blog: The Digital Helm

X-G
Member #856
December 2000
avatar

Point is, without a proper geometrical representation of it, you can't get "realistic" lighting. I can't help you there.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Onewing
Member #6,152
August 2005
avatar

Personally, I really don't think this:
http://comp.uark.edu/~spsilve/terrain8.JPG
is that bad. I might just have to stick with the old algorithms, but it couldn't hurt to learn something new as well.

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.

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Tobias Dammers
Member #2,604
August 2002
avatar

As long as you do nothing but top-down view, a simple emboss filter might be all you need. Or just use the slope in 45deg-direction to calculate a light intensity (full intensity at full up-left slope dir, zero at low-right dir).

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Onewing
Member #6,152
August 2005
avatar

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 45deg-direction to calculate a light intensity (full intensity at full up-left slope dir, zero at low-right dir).

I'm not sure what you mean by "full up-left slope, zero at low-right dir".

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Tobias Dammers
Member #2,604
August 2002
avatar

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

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Go to: