Shadows and also a light source question

Is there an easy way to implement shadows, i am only using lines to draw my objects, i.e. a collection of lines to implement a tree.

Also in my “world” i have a sun, i was wondring if i could use this sun as my light source, and if the sun moved the light source would also be different, this light source will hopefully be used to work out and draw the shadows.

This is what i guess the prog will have to know in order to draw the shadow.

1: The plane to draw on, i.e. y axis is 0
2: The angle of the sun
3: The position of the lines

The prog i guess will then work out the new length of the shadow depending on the suns angle in relation to the current line.

Am i thinking in the right way?? if so what do i have to do to implement this?

Remeber i am using nothing complex as my object just plain simple 3d lines.


you are on the right track. Here’s a simple solution:

P’ = P - (dot(P,N)/dot(S,N)) * S.

P’= new vertex position on the shadow plane
P = original vertex position
S = direction of sun light (unit vector)
N = normal of shadow plane (distance is assumed to be 0)

So, you could project each line vertex using this equation, and then blend the projected line in black, for example, to simulate a shadow.


If your shadow plane is the plane y=0, then you could simplify all this to:

P’ = P - (P.y / S.y) * S.

And the dot, i gues this a maths thing that i can see on ultimategameprogramming as i remeber seeing it somewhere in a “tutorial”, its something to do with the normal is it not?

Oops! Yeah, it’s a math thing. An easy one though.

dot(A,B) = A.xB.x + A.yB.y + A.z*B.z

A normal vector N is any vector divided by its length:

N = V / length(V).

The length of a vector is: length(V) = sqrt( dot(V,V) ).

so just quickly if my vector, i.e. branch has these coords:

start.x = 0
start.y = 0
start.z = 0

direction.x = 0
direction.y = 1
direction.z = 0

(This is a straight line going up)

and a length of 4

what would the vector be? i guess it will be ( start.x + (direction.x * length) )+ ( start.y + (direction.y * length) ) + ( start.z + (direction.z * length) ) / length;

??? As start.x * direction.x * length is where the line ends.

sorry i think i understood your message wrong, i have no re read it and i understand whats happoening, funnily enough i actually use the same thing somewhere in my program.


If your shadow plane is the plane y=0, then you could simplify all this to:

P’ = P - (P.y / S.y) * S

or without the vector notation

P.x’ = P.x - (P.y / S.y) * S.x
P.y’ = P.y - (P.y / S.y) * S.y
P.z’ = P.z - (P.y / S.y) * S.z

This will work for any point P in your world.

gotcha cheers will try that and let you know.