GameMaker Lighting Flashlight Direction (SOLVED!)

P

_Proxy

Guest
I'm trying to make a cone flashlight with my lighting system but its not keeping the same size as its rotating.

Code:
// Flashlight
if (flash_light) and (flash_light_toggle)
{
    var c = make_colour_rgb(255, 255, 255);
    var dir = point_direction(x, y, mouse_x, mouse_y);
    var buffer_x = lengthdir_x(18, dir);
    var buffer_y = lengthdir_y(18, dir);
    var length_x = lengthdir_x(200, dir);
    var length_y = lengthdir_y(200, dir);
    gpu_set_blendmode(bm_subtract);
    surface_set_target(light);
    draw_triangle_colour
    (
        (x-2) + buffer_x - camera_get_view_x(view),
        (y-2) + buffer_y - camera_get_view_y(view),
        (x-2) + length_x - camera_get_view_x(view),
        (y-2) + length_y - camera_get_view_y(view),
        (x-2) + length_x - camera_get_view_x(view),
        (y-2) - length_y - camera_get_view_y(view),
        c,c,c, false
    )
    surface_reset_target();
    gpu_set_blendmode(bm_normal);
}

buffer_x and buffer_y is the tip of the gun
length_x and length_y is the length of the triangle
(x-2) and (y-2) is the origin on the gun
 

johnwo

Member
You'd best take another look at the three points you use to draw the triangle.

Hint:
When done correctly, you should be able to set the cone's FOV to a specific angle.
 

NeoShade

Member
I haven't tested it, but you could try this... I think I got it right (maybe):

Code:
var c = c_white;

var light_length = 200;
var light_fov = 45;

var dir = point_direction(x, y, mouse_x, mouse_y);

var buffer_x = lengthdir_x(18, dir);
var buffer_y = lengthdir_y(18, dir);

var dir_l = dir + (light_fov / 2);
var dir_r = dir - (light_fov / 2);

gpu_set_blendmode(bm_subtract);
surface_set_target(light);


draw_triangle_colour
(
    (x-2) + buffer_x,
    (y-2) + buffer_y,
    (x-2) + lengthdir_x(light_length, dir_l),
    (y-2) + lengthdir_y(light_length, dir_l),
    (x-2) + lengthdir_x(light_length, dir_r),
    (y-2) + lengthdir_y(light_length, dir_r),
    c, c, c, false
)

surface_reset_target();
gpu_set_blendmode(bm_normal);
 
P

_Proxy

Guest
I haven't tested it, but you could try this... I think I got it right (maybe):

Code:
var c = c_white;

var light_length = 200;
var light_fov = 45;

var dir = point_direction(x, y, mouse_x, mouse_y);

var buffer_x = lengthdir_x(18, dir);
var buffer_y = lengthdir_y(18, dir);

var dir_l = dir + (light_fov / 2);
var dir_r = dir - (light_fov / 2);

gpu_set_blendmode(bm_subtract);
surface_set_target(light);


draw_triangle_colour
(
    (x-2) + buffer_x,
    (y-2) + buffer_y,
    (x-2) + lengthdir_x(light_length, dir_l),
    (y-2) + lengthdir_y(light_length, dir_l),
    (x-2) + lengthdir_x(light_length, dir_r),
    (y-2) + lengthdir_y(light_length, dir_r),
    c, c, c, false
)

surface_reset_target();
gpu_set_blendmode(bm_normal);
It works perfectly!
 
Last edited by a moderator:
Top