GML [Solved] Sticky bullets on nearest surface

Papa Doge

Member
Hello,

I'm playing around with some code to test different ways of finding the nearest collision using the collision_line function and I've created something that does what I want it to do but seems to crash the game pretty regularly.

I'm down to trash this whole concept and try something different but this is the closest I've gotten to my goal...

Goal = Click to draw a line from the player to the nearest surface then create a "sticky bullet" that instantly appears on the nearest surface.

Here's what I have in the player's "shooting" section...

Code:
// THROW

if (key_throw) {
  
    throwAngle = point_direction(x,y,mouse_x,mouse_y);
  
    var travelDistance = 1;
  
    throwX = x + lengthdir_x(32,throwAngle);
    throwY = y + lengthdir_y(32,throwAngle);
    targetX = x + lengthdir_x(travelDistance,throwAngle);
    targetY =  y + lengthdir_y(travelDistance,throwAngle);
  
    while (!collision_line(throwX,throwY,targetX,targetY,par_obj_collisions,false,true)) {
        show_debug_message("I'm hitting a wall here...");
        targetX = x + lengthdir_x(travelDistance,throwAngle);
        targetY = y + lengthdir_y(travelDistance,throwAngle);
        travelDistance++;
    }
  
    show_debug_message("Free as a bird...");
  
}
What I'm doing is drawing a 1 pixel line to start and then adding to the length of the line while there is no collision.

I don't have the bullet layer, sprite, and all that other stuff set up yet so I'm debugging with a draw event that places a red circle where the "sticky bullet" will spawn.

Here's my debugging draw events...

Code:
draw_line_color(throwX,throwY,targetX,targetY,c_red,c_yellow);

draw_circle_color(targetX,targetY,8,c_red,c_red,true);
----------

I suspect my use of a while loop is what's causing the crashing issue. I want the "sticky bullet" to appear instantly so I need this nearest surface calculation to happen in a single step.
 

Papa Doge

Member
I'd like to add an image too of how it looks when it's working so you can see the solution I'm working towards but the image uploader is busted for some reason...
 
Last edited:
your problem currently is that you have nothing to stop the object from travelling outside the room and into infinity stretching your step until the game crashes.

if (key_throw) {

throwAngle = point_direction(x,y,mouse_x,mouse_y);

var travelDistance = 1;

travel=true;

throwX = x + lengthdir_x(32,throwAngle);
throwY = y + lengthdir_y(32,throwAngle);
targetX = x + lengthdir_x(travelDistance,throwAngle);
targetY = y + lengthdir_y(travelDistance,throwAngle);

while travel=true{
if collision_line(throwX,throwY,targetX,targetY,par_obj_collisions,false,true)
show_debug_message("I'm hitting a wall here...");
else{
targetX = x + lengthdir_x(travelDistance,throwAngle);
targetY = y + lengthdir_y(travelDistance,throwAngle);
travelDistance++;
if (throw_x>room_width) or (throw_x<0) or (throw_y>room_height) or (throw_y<0)
travel=false;
}
}

show_debug_message("Free as a bird...");

}
 
Top