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

Problem with colliders

PocketForge

Member
sry for bad eng

Hello everyone, I decided to make a laser sight in the game through a loop "for" and the output in draw_line, but ran into a problem - for some reason the colliders of the object (red box), which is an obstacle for the laser, were shifted 32 pixels up, everything was set up correctly, rechecked a hundred times, but still the same problem, the same problem was in the Game Maker 8. This problem exists only for the laser, all other objects interact correctly
 

Attachments

Bryan112

Member
Try this:

CREATE EVENT:
GML:
AIM = point_direction(Oplayer.x,Oplayer.y, mouse_x, mouse_y);

global.max_length = point_distance(Oplayer.x,Oplayer.y,mouse_x,mouse_y);
solid_object = Owall;

for(var i = 0; i < global.max_length; i++){

    global.lx = x + lengthdir_x(i, AIM);
    global.ly = y + lengthdir_y(i, AIM);

    if(collision_point(global.lx, global.ly, global.solid_object, false, true)){
        break;
    }

}
STEP EVENT:

Code:
 AIM = point_direction(Oplayer.x,Oplayer.y, mouse_x, mouse_y);
global.max_length = point_distance(Oplayer.x,Oplayer.y,mouse_x,mouse_y);

if point_distance(Oplayer.x,Oplayer.y,global.solid_object.x,global.solid_object.y)< 600 {
global.max_length = point_distance(Oplayer.x,Oplayer.y,mouse_x,mouse_y);
}
else
{
global.max_length = point_distance(Oplayer.x,Oplayer.y,mouse_x,mouse_y);       
}

}
solid_object = Owall;

for(var i = 0; i < global.max_length; i++){

    global.lx = Oplayer.x + lengthdir_x(i, global.AIM);
    global.ly = Oplayer.y + lengthdir_y(i, global.AIM);

    if(collision_point(global.lx, global.ly, solid_object, false, true)){
        break;
    }

}
DRAW EVENT:
Code:
draw_line_width_color(x+lengthdir_x(6,point_direction(x,y,mouse_x,mouse_y)-90),y+lengthdir_y(6,point_direction(x,y,mouse_x,mouse_y)-90),global.lx,global.ly,1,c_red,c_red);
 

PocketForge

Member
Try this:

CREATE EVENT:
GML:
AIM = point_direction(Oplayer.x,Oplayer.y, mouse_x, mouse_y);

global.max_length = point_distance(Oplayer.x,Oplayer.y,mouse_x,mouse_y);
solid_object = Owall;

for(var i = 0; i < global.max_length; i++){

    global.lx = x + lengthdir_x(i, AIM);
    global.ly = y + lengthdir_y(i, AIM);

    if(collision_point(global.lx, global.ly, global.solid_object, false, true)){
        break;
    }

}
STEP EVENT:

Code:
 AIM = point_direction(Oplayer.x,Oplayer.y, mouse_x, mouse_y);
global.max_length = point_distance(Oplayer.x,Oplayer.y,mouse_x,mouse_y);

if point_distance(Oplayer.x,Oplayer.y,global.solid_object.x,global.solid_object.y)< 600 {
global.max_length = point_distance(Oplayer.x,Oplayer.y,mouse_x,mouse_y);
}
else
{
global.max_length = point_distance(Oplayer.x,Oplayer.y,mouse_x,mouse_y);      
}

}
solid_object = Owall;

for(var i = 0; i < global.max_length; i++){

    global.lx = Oplayer.x + lengthdir_x(i, global.AIM);
    global.ly = Oplayer.y + lengthdir_y(i, global.AIM);

    if(collision_point(global.lx, global.ly, solid_object, false, true)){
        break;
    }

}
DRAW EVENT:
Code:
draw_line_width_color(x+lengthdir_x(6,point_direction(x,y,mouse_x,mouse_y)-90),y+lengthdir_y(6,point_direction(x,y,mouse_x,mouse_y)-90),global.lx,global.ly,1,c_red,c_red);
it works, thanks!
 

Attachments

PocketForge

Member
We'll need to see the code.
STEP EVENT
GML:
for(i=0;i<1024;i+=2)
{
test1=i
if place_meeting(x+handx+lengthdir_x(test1,direction),y-10+lengthdir_y(test1,direction),obj_wall)
exit
}
DRAW EVENT
GML:
draw_line_color(x+handx+lengthdir_x(32,direction),y-10+lengthdir_y(32,direction),x+handx+lengthdir_x(test1,direction),y-10+lengthdir_y(test1,direction),c_red,c_red)
 

Attachments

My assumption would be that handx and the -10 on the y are incorrect. If your gun rotates around the player (which it looks like it does), then these will just become random offsets rather than actual positioning as soon as the gun rotates at all. You have to use lengthdir for both the 32 pixel length and the handx (with the handx offset being applied to the x and the y equally).
 

PocketForge

Member
My assumption would be that handx and the -10 on the y are incorrect. If your gun rotates around the player (which it looks like it does), then these will just become random offsets rather than actual positioning as soon as the gun rotates at all. You have to use lengthdir for both the 32 pixel length and the handx (with the handx offset being applied to the x and the y equally).
I roughly understood what and how to do, but the translator distorts the meaning of your message, so could you write a code for me with a correction if it's not difficult for you?
 
Ok, something like this:

Step Event
Code:
var hand_len = 7; // This might need to be tweaked
var hand_dir = direction; // This might need to be tweaked

var hand_xoffset = lengthdir_x(hand_len,hand_dir);
var hand_yoffset = lengthdir_y(hand_len,hand_dir);

startx = x+hand_xoffset;
starty = y+hand_yoffset;

for(var i=0;i<1024;i+=2) {

   var line_xoffset = lengthdir_x(test1,direction);
   var line_yoffset = lengthdir_y(test1,direction);

   endx = startx+line_xoffset;
   endy = starty+line_yoffset;

   test1 = i;
   if (position_meeting(endx,endy,obj_wall)) {
      break;
   }
}
Draw Event
Code:
draw_line(startx,starty,endx,endy);
You can see I've changed where the line starts from a flat offset (handx and y-10) into one that should rotate around the player as the player rotates (startx and starty), so in essence, we are rotating the start and the end of the line to face the proper direction. In addition, I noticed you were using place_meeting() which actually moves the whole instance that is running the code to the position and checks its bounding box for a collision. You don't want that. You want to use position_meeting() which simply checks a single pixel position for a collision.
 

PocketForge

Member
Ok, something like this:

Step Event
Code:
var hand_len = 7; // This might need to be tweaked
var hand_dir = direction; // This might need to be tweaked

var hand_xoffset = lengthdir_x(hand_len,hand_dir);
var hand_yoffset = lengthdir_y(hand_len,hand_dir);

startx = x+hand_xoffset;
starty = y+hand_yoffset;

for(var i=0;i<1024;i+=2) {

   var line_xoffset = lengthdir_x(test1,direction);
   var line_yoffset = lengthdir_y(test1,direction);

   endx = startx+line_xoffset;
   endy = starty+line_yoffset;

   test1 = i;
   if (position_meeting(endx,endy,obj_wall)) {
      break;
   }
}
Draw Event
Code:
draw_line(startx,starty,endx,endy);
You can see I've changed where the line starts from a flat offset (handx and y-10) into one that should rotate around the player as the player rotates (startx and starty), so in essence, we are rotating the start and the end of the line to face the proper direction. In addition, I noticed you were using place_meeting() which actually moves the whole instance that is running the code to the position and checks its bounding box for a collision. You don't want that. You want to use position_meeting() which simply checks a single pixel position for a collision.
Thank you very much, and I also realized that all I had to do was change place_meeting to position_meeting
 
Top