• Hey! Guest! The 40th (!!!) GMC Jam will take place between February 25th, 12:00 UTC to March 1st 12:00 UTC. Why not join in this very special anniversary jam! Click here to find out more!

SOLVED Cone of vision

Solthall

Member
Hello.

I'm trying to create a cone of vision for my enemy object. I am able to draw the cone I want:

Create event:
coneOfSightLeft = 45;
coneOfSightRight = -45;
coneOfSightLength = 600;

Draw event:
draw_triangle(x, y, x+lengthdir_x(coneOfSightLength,coneLeft), y+lengthdir_y(coneOfSightLength,coneLeft), x+lengthdir_x(coneOfSightLength,coneRight), y+lengthdir_y(coneOfSightLength,coneRight), false);
draw_set_alpha(0.1);

I want to check for the player object using the point_in_triangle function. Haven't used that before so after messing arround with it I just cant get the point_in_triangle to correspond with the cone I have drawn!

It should be something like this:

Step event:
var player = instance_nearest(x, y, obj_player);
if instance_exists(player)
{
var x1 = x + lengthdir_x(?); //what to put in here to replicate the shape and size of the drawn vision cone
var y1 = y + lengthdir_y(?);
var x2 = x + lengthdir_x(?);
var y2 = y + lengthdir_y(?);
if point_in_triangle(player.x, player.y, x, y, x1, y1, x2, y2)
{
can_see = true;
}
else
{
can_see = false;
}
}
 

woods

Member
(from the manual) https://docs.yoyogames.com/source/d... collisions/collisions/point_in_triangle.html

var x1 = x + lengthdir_x(100, image_angle - 45); //this is 100 pixels away at an angle of 45^
var y1 = y + lengthdir_y(100, image_angle - 45);
var x2 = x + lengthdir_x(100, image_angle + 45);
var y2 = y + lengthdir_y(100, image_angle + 45);

...according to your coneOfSightLength = 600; //shouldn't that be 600 instead of 100? ..the length of your vision cone

var x1 = x + lengthdir_x(600, image_angle - 45);
var y1 = y + lengthdir_y(600, image_angle - 45);
var x2 = x + lengthdir_x(600, image_angle + 45);
var y2 = y + lengthdir_y(600, image_angle + 45);


its late ...err early.. however you wanna look at it. unless i am reading the example wrong ;o)
 
Top