• 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!

collision line detection

S

stepup2000

Guest
So im making a zombie stealth game, so the zombies have to follow the player when they see the player, i did this with a collision line that is drawn towards the point they are facing. The only problem with this method is that the collision line is very thin. So basically i have to stand exactly in front of the zombie otherwise he wont see me. Does anyone know if i can make the collision line thicker so the zombie has a larger detection radius? or a different method

//set view of zombie
var view_distance = 600;
var tx = x+lengthdir_x(view_distance,image_angle);
var ty = y+lengthdir_y(view_distance,image_angle);


if (collision_line(x,y,tx,ty,obj_man,true,false)) { //checks if the zombie can see the player
if (collision_line(x,y,obj_man.x,obj_man.y,obj_wall,true,false)) = noone { // checks if there are no walls in between the player and the zombie
alert = 1 // makes him follow the player
audio_play_sound(so_zombie_growl, 1, false) //plays zombie audio
}
 

Bentley

Member
es anyone know if i can make the collision line thicker so the zombie has a larger detection radius?
First idea that comes to mind is to use point_in_rectangle. I think that'd work. You could also check two or 3 collision lines. 1 line per zombie eye : ) ?
 
C

coryru

Guest
Try using a separate collision mask. So that way you can set the exact shape you want.
 
You'll have to extend the x2,y2 and x3,y3 in the point_in_triangle with lengthdir so that it properly follows the zombies direction, but this'll let you know if they're in the zombie's LOS:
Code:
if (point_in_triangle(player_x,player_y,zombie_x,zombie_y,x2,y2,x3,y3)) {
   if (!collision_line(zombie_x,zombie_y,player_x,player_y,obj_solid,false,true)) {
      // Player is within the triangle cone of vision AND there's no solid objects blocking LOS
   }
}
 
S

stepup2000

Guest
You'll have to extend the x2,y2 and x3,y3 in the point_in_triangle with lengthdir so that it properly follows the zombies direction, but this'll let you know if they're in the zombie's LOS:
Code:
if (point_in_triangle(player_x,player_y,zombie_x,zombie_y,x2,y2,x3,y3)) {
   if (!collision_line(zombie_x,zombie_y,player_x,player_y,obj_solid,false,true)) {
      // Player is within the triangle cone of vision AND there's no solid objects blocking LOS
   }
}
Thank you it worked! this is how i did it:

var inst = instance_nearest(x, y, obj_man);
if instance_exists(inst)
{
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);

if point_in_triangle(inst.x, inst.y, x, y, x1, y1, x2, y2) {
if !(collision_line(x,y,obj_man.x,obj_man.y,obj_wall,true,false)) { ///do something
}
}
}
 
Last edited by a moderator:
Top