fixed an allusive bug with a very annoying error message

I had some code where I take the square root ((my speed.x- bullet speed.x)squared + (my speed.y-bullet speed.y)squared). I started getting the ridiculous error message that I can't take the square root of a negative number. Unless the speed of my objects is an imaginary number. It should not be possible for my (speed.x-bullet speed.x) squared to be negative. So it took me a while to find out what was wrong. Finally I found out where the problem was.

physics_apply_force(x, y, N2*3000 *chasenem2.phy_speed_y/chasenem2.phy_speed, N*3000 *chasenem2.phy_speed_x/chasenem2.phy_speed);

For some reason Gamemaker Studio hated is when I divided by chasenem2.phy_speed. I I was applying the y componant of chasenem2 in the x direction and the x component in the y direction to get my ship to dodge the projectile refered to as chasenem2. But I did not what the speed I dodge to be dependent on the speed of the projectile. So I divided by chasenem2.phy_speed to that I dodge at the same speed no matter how fast the projectile moves. But then I started getting ridiculous error messages in other places in my code.  Now my code is the following and the strange error messages no longer appear and the game has been running overnight and most of today without crashing. I am annoyed at Gamemaker Studio right now but the problem seems to be fixed.

var impulse8 = 100000;
var sumspeed = phy_speed + chasenem2.phy_speed;
var savedistance = distance_to_object(chasenem2);
var speedcloser = sqrt(sqr(phy_speed_x - chasenem2.phy_speed_x) + sqr(phy_speed_y - chasenem2.phy_speed_y));
var speedcompare = (phy_speed + chasenem2.phy_speed);
var try = speedcloser/speedcompare;
var dirbullet = point_direction(x,y, chasenem2.phy_linear_velocity_x, chasenem2.phy_linear_velocity_y);
var lxdb = lengthdir_x(impulse8, dirbullet);
var lydb = lengthdir_y(impulse8, dirbullet);

if (point_distance(x,y, chasenem2.x,chasenem2.y) < 1300)
var distance = sqrt(sqr(x-chasenem2.x) + sqr(y-chasenem2.y))
var prevdist = sqrt(sqr(phy_position_xprevious - chasenem2.phy_position_xprevious) + sqr(phy_position_yprevious- chasenem2.phy_position_yprevious))


if (distance < prevdist)


physics_apply_force(x,y, N2 * lydb, N * lxdb);



Treacherous-Space.dmg 344 MB
Jun 05, 2019
Demo.dmg 344 MB
Jun 05, 2019
Treacherous Space.exe 352 MB
Jun 05, 2019
Demo.exe 352 MB
Jun 05, 2019

Get Treacherous Space

Leave a comment

Log in with to leave a comment.