Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Help needed with directional lights

Author
Message
Robert The Robot
19
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 15th Jun 2007 19:16
I want to rotate a directional light by altering each axis in turn. The code I use sets the directional light angle using Cos(Angle_Value), but the light only rotates through to 180 degrees (-1) and then moves back to 0 (1).

Is there a way to smoothly rotate the light the full 360 degrees about a single axis automatically adjusting the other two axis as needed?

(The only example I could find was in MagicWorld's source code:

but I can't understand how this bit works, and it doesn't allow rotation in the Z-axis.)

On our way 'ome, on our way 'ome...
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 15th Jun 2007 23:05
Hello,

There are a couple of ways to handle this. First thing to note is that a directional light casts all of it's light in a direction determined by a vector. When you set a point, you are really telling the directional light to beam in that direction - nothing new there. So the approach is to take a starting point and cast a vector away from that starting point to a new point - this will be the direction of the light - this is probably review, but sometimes it just helps to say outloud what is going on.

DBC makes it pretty easy using the NEWXVALUE, NEWYVALUE, and NEWZVALUE functions. All we have to supply is the starting point, the angle and some step value. The step value is important because combined with the angle, it will actually influence the direction of the vctor. For a very straight forward along a single path cicling light, use the same angle for each value and use the same step value. The x,y and z returned is the vector away from the starting point - thus the direction. Here's an example:



Enjoy your day.
Robert The Robot
19
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 17th Jun 2007 17:52
Thanks for your example, but I'm still having a bit of trouble with this.

I've altered your code snippet a little to try and show the kind of thing I'm after. It now uses the arrow keys to manipulate each axis but if you rotate just one axis at a time, the light still cycles back and forth.

However, if you set (say) the X and Z values to be 1000 out of step with each other, set y# to 0 and then rotate both together you get a perfectly smooth rotation about the y-axis.



I can't figure out how to update the y-axis by changing the y-axis value and not altering the x- and z-axis.

Also, you said that
Quote: "The x,y and z returned is the vector away from the starting point - thus the direction"


How can this be? I thought vectors needed a size and a direction but there is only one value for each axis?

On our way 'ome, on our way 'ome...
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 18th Jun 2007 02:55 Edited at: 20th Jun 2007 18:40
Hi RTR,

Quote: "However, if you set (say) the X and Z values to be 1000 out of step with each other, set y# to 0 and then rotate both together you get a perfectly smooth rotation about the y-axis."


That is correct. That is why I was saying earlier the angle and the step value combined influence the direction of the light. Think of each of the step values as an influence (in the positive and negetive direction). The greater the step value for an individual ordinate (x y or z), the greater the influence towards the axis and direction. You don't really have to use the NEWXVALUE, NEWYVALUE, and NEWZVALUE - they just make it simple to get a new end point from a starting point. All you need are positive or negetive values for x, y and z. If you start with 0,1,0, that means the light is beaming straight up along y. 1,1,0 means a 45 degreangle towards the upper right (assuming you are looking down z). Now by increasing or decreasing the coordinates, you are in essence setting the direction of the light. The larger the number, the greater the influence along that particular axis.

I can see how this is a bit tricky. When I was talking about a vector, I was talking about a directional vector. If you are familiar with normals, that may help to conceptualize what I'm trying to say. A normal is the directional vector away from a polygon that light is "reflected". The step value of newxvalue, newyvalue, and newzvalue can be thought of as the outside of the radius from the initial value. In combination they make up the direction. Since a directional light is infinite, the size of the vector doesn't have influence on intensity (as it would with a normal), but slight variances in the step value of x y and z will alter the direction of the light somewhat. Even though that makes sense in my head, it sounds terrible the way I'm writing it.

A point in space takes at least to components to make a coordinate. If you want to identify a point in 2d, you have x and y. Each has to have a value in order to identify the point. They have to work together. Now, in 3d, you have three points that have to work together. I think your mistake is in that you are treating x,y and z as their own thing. Changing only the x value will not make a rotation around the x axis. The same goes for y and z. You have to treat them as friends and they all have to work together in a common way. A rotation around the x axis is determined by a change in the y coordinate and the z coordinate. The angle that this change influences is the x angle.

A rotation around y is determined by a change in the x coordinate and the z coordinate. The angle here is the y angle.

So what about around z? You guessed it x and y and the angle is z.

So if you want to change the rotation of the directional light, and you want to do it interms of an angle you have to change the proper coordinates and angle. It will always be at least in pairs.

But again, just changeing the coordinate values will change the direction. I rewrote the example so that you could change the rotation using the keyboard. However, I did run into problems using the NEWVALUE functions when it came to the z axis. I used a little trig to solve that.

[EDIT]
In my estimation, there seems to be a problem with the NEWYVALUE function in relation to the NEWXVALUE fucntion. The relationship between the two should be 90 degrees, but the values returned are at 180 which would cause a problem with rotation around the z axis. This can be solved in a couple of different ways, one way is to use trig, the other is just to add 90 degrees to the angle in NEWYVALUE if x and y are using the same angle (the angle around z axis). This 180 offset probably has to do with Euler angles but at any rate it's easy enough to solve by adding 90 degrees. I removed the trig and used the NEWYVALUE function in the example - also I forgot to mention all directions are realtive to the origin (0,0,0)

Anyway here's the updated example. You'll notice a bit of a jump when you switch axis. I didn't compensate for changing angles between the same axis so the coordinate values can change suddenly. I've written enough for now and the example should still show you how the method works:

[EDIT]


Enjoy your day.
Robert The Robot
19
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 20th Jun 2007 22:41
Thanks Latch!

I see what you mean about the jump when you change axis, but I hope it won't affect my program too drastically! In case it does, though, I'm trying to work on a way of locking a specific axis - either by making the new angle equal the old one or by locking the axis direction (so if you rotate about Y and then switch to X, the Z value stays the same).

Haven't had much success with that last option, but the first one seems promising...

Thanks once again!

On our way 'ome, on our way 'ome...
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 22nd Jun 2007 20:06 Edited at: 22nd Jun 2007 20:10
You're welcome - but I think I was making more difficult than it had to be. Even with the NEWVALUE commands that are supposed to cut out the trig, it's hard to deal with the rotation order.

It dawned on me to use a much simpler method:

SET LIGHT TO OBJECT ORIENTATION.

All you have to do is rotate and invisible object, then set the directional light to it's orientation. Makes smooth and easy rotation of the directional light. I rewrote the example to this end. I used a plain (for low poly) and hid it. The only thing to be aware of is that after an axis is rotated, it influences the position of the other axes. So if you spin the light around z say, when you rotate x or y, it may be at a strange angle that isn't stright around the center of the particular axis. But this method does give you rotational freedom. Here's the example:



Enjoy your day.
Robert The Robot
19
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 25th Jun 2007 12:40
This is fantastic!

I can rotate a directional light with ease now, and by using the Light Direction X, Y and Z commands I can find and store the light's exact details. (I needed that for an application I've been working on that creates lighting effects using DB's internal light engine.)

Thanks once again Latch!

On our way 'ome, on our way 'ome...

Login to post a reply

Server time is: 2026-07-05 18:18:06
Your offset time is: 2026-07-05 18:18:06