I don't understand the intention of the above code. For determining if two circular regions are colliding, just see if the distance between their centers is smaller than their combined radii. If object1 has a radius r1 and object2 has a radius r2,
return (point_distance(object1.x,object1.y,object2.x,object2.y) < r1+r2)
If you want this object to move away from the other object until it no longer collides, simply do (in this example)
Using gml Syntax Highlighting
intersectingDistance = point_distance(object1.x,object1.y,object2.x,object2.y) - (r1+r2)
if (intersectingDistance <= 0)
return false;
angle = degtorad(point_direction(object1.x,object1.y,object2.x,object2.y))
x -= cos(angle)*intersectingDistance
y -= sin(angle)*intersectingDistance
return true;
No looping required.
Furthermore, why are you inconsistent with how you determine x and y components of vectors? In the while loop, you use lengthdir_x() and lengthdir_y() to find the vector (which does not require converting to radians) yet you use cos() and sin() to determine the vectors when moving back... x -= lengthdir_x(0.2,col) and y -= lengthdir_y(0.2,col) would've done the same thing with far fewer operations and less conversion gobbledygook. And degtorad is a far better way to convert degrees to radians than multiplying by pi/180, as the function takes into account how radians (in Game Maker) go clockwise, rather than counterclockwise as with degrees.
...And while I'm at it, here's a more legible version of my previous moving-closer-to-another-angle script:
Name: moveToAngle(startDirection,goalDirection)
Program: GameMaker 8.0 (Should be fine in later versions, too)
User: Magnemania
Registered: No
Using gml Syntax Highlighting
//argument0: current direction
//argument1: direction to face
compareDirection = argument1;
compareDirection -= argument0;
if (compareDirection < 0)
compareDirection = 360+compareDirection;
if (compareDirection > 180)
plusMinus = -1;
else
plusMinus = 1;
return plusMinus;
I don't understand the intention of the above code. For determining if two circular regions are colliding, just see if the distance between their centers is smaller than their combined radii. If object1 has a radius r1 and object2 has a radius r2,
return (point_distance(object1.x,object1.y,object2.x,object2.y) < r1+r2)
If you want this object to move away from the other object until it no longer collides, simply do (in this example)
[syntax=gml]
intersectingDistance = point_distance(object1.x,object1.y,object2.x,object2.y) - (r1+r2)
if (intersectingDistance <= 0)
return false;
angle = degtorad(point_direction(object1.x,object1.y,object2.x,object2.y))
x -= cos(angle)*intersectingDistance
y -= sin(angle)*intersectingDistance
return true;[/syntax]
No looping required.
Furthermore, why are you inconsistent with how you determine x and y components of vectors? In the while loop, you use lengthdir_x() and lengthdir_y() to find the vector (which does not require converting to radians) yet you use cos() and sin() to determine the vectors when moving back... x -= lengthdir_x(0.2,col) and y -= lengthdir_y(0.2,col) would've done the same thing with far fewer operations and less conversion gobbledygook. And degtorad is a far better way to convert degrees to radians than multiplying by pi/180, as the function takes into account how radians (in Game Maker) go clockwise, rather than counterclockwise as with degrees.
...And while I'm at it, here's a more legible version of my previous moving-closer-to-another-angle script:
[b]Name:[/b] moveToAngle(startDirection,goalDirection)
[b]Program:[/b] GameMaker 8.0 (Should be fine in later versions, too)
[b]User:[/b] Magnemania
[b]Registered:[/b] No
[syntax=gml]//argument0: current direction
//argument1: direction to face
compareDirection = argument1;
compareDirection -= argument0;
if (compareDirection < 0)
compareDirection = 360+compareDirection;
if (compareDirection > 180)
plusMinus = -1;
else
plusMinus = 1;
return plusMinus;[/syntax]