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

Legacy GM "distance_to_object" giving incorrect/inconsistent results

D

Dieter

Guest
I'm pretty new to programming and gamemaker so I might be missing something very obvious here. I'm working on a small game and one of the spells is a ray of heat (like in Diablo). The beam shouldn't go through walls so I wrote some code that adjusts the size of the beam when it's hitting a wall. Sometimes it works correctly (although the beam flickers) but sometimes it becomes way to short. Through show_debug_message I noticed the problem: distance_to_object gives an incorrect distance. In the added screenshot you can see that it returns a value of 71 pixels, while the actual distance to the wall is 139px (I checked).

Does anyone have a clue what's going on here? The funny thing is, other collisions with the wall work pixel perfect (other spells, the player, etc).

Many thanks in advance!

Code:
var ratio
var instwall
instwall = collision_line(x, y, x + beamendx, y + beamendy, obj_wall, false, true){
    if instwall != noone{
        ratio = distance_to_object(instwall) / lengthofbeam;
        image_xscale = ratio; 
        show_debug_message("ratio is" + string(ratio));
        show_debug_message("distance to wall is" + string(distance_to_object(instwall)))
    } else {
        image_xscale = 1;
    }
}
 

Attachments

NightFrost

Member
What is throwing you off is, distance_to_object() measures distance between bounding box edges, not x/y points.
 
D

Dieter

Guest
Thanks for your reply NightFrost, but I don't think that's it. The bounding box should be overlapping with the sprite of the wall. As you see on the screenshot, the collision is happening a couple of grids before the actual wall and its bounding box. Please correct me if I'm wrong though.
 
D

Dieter

Guest
Aha! I thought about it some more and realized the beam sprite also has a bounding box. I'm guessing that's what you meant. Since these all these bounding boxes made calculations quite unpredictable I searched for alternative functions and found that gamemaker also has a function "point_distance(x1, y1, x2, y2)". Which solved the problem. Thanks again!
 
Top