• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

3D Getting 3D vectors from x,y,z for light direction

kamiyasi

Member
Hi there. I'm simulating the position of the sun in 3D space like so:

Code:
dayloop += 0.5;

if dayloop > 360 then dayloop -= 360;

global.lightx = lengthdir_x((3600),dayloop*10);
global.lighty = (room_height/2);
global.lightz = lengthdir_y((3600),dayloop*10);
Now what I would like to do is find the vectors that would point those x,y, and z values towards room_width/2, room_height/2, and 0. I want to find these vectors so I can use them in d3d_light_define_direction.
In other words, I want the 3D position of my global.light variables to define the vector of my directional light.
 

Binsk

Member
All you need to do is:
  1. Find the vector that contains the distance from your desired point to your light's position on each axis.
  2. Find the magnitude of the vector.
  3. Divide each component in the vector by its magnitude so as to cancel out the distance and just leave the direction.
An example method of doing so, albeit I suggest you try figuring it out yourself first:
Code:
     // Position of the "sun":
var __fromx = lengthdir_x((3600),dayloop*10),
    __fromy = (room_height / 2),
    __fromz = lengthdir_y((3600),dayloop*10),

     // Position the sun should point to:
    __tox = room_width / 2,
    __toy = room_height / 2,
    __toz = 0,

     // Magnitude of our final vector:
    __magnitude,

     // Directional vector:
    __dirx,
    __diry,
    __dirz;

// Find the distance for each axis:
__dirx = __tox - __fromx;
__diry = __toy - __fromy;
__dirz = __toz - __fromz;

// Find the magnitude of our vector:
__magnitude = sqrt(sqr(__dirx) + sqr(__diry) + sqr(__dirz));

// Remove distance and leave just the direction:
__dirx /= __magnitude;
__diry /= __magnitude;
__dirz /= __magnitude;
All you'd need to do then is use those generated directional values as your vector.
 
Top