1
\$\begingroup\$

I am trying to understand the logic behind Game Maker's point_direction(x1, y1, x2, y2) function, which takes the vector from (x1, y1) to (x2, y2) and returns the counter-clockwise angle it forms from the positive x-axis, in degrees.

Image from the YoYo Games docs illustrating this function.

(Diagram from the YoYo Games documentation of point_direction)

How do I implement this functionality in my own code?

Here's my attempt so far:

//custom_point_direction script

var x1 = argument0;
var y1 = argument1;
var x2 = argument2;
var y2 = argument3;
var targetAngle;

targetAngle = radtodeg(arctan2(y2,x1));


return -targetAngle;

Here's an example of how I want to use this function. This is the step event of an object that should face/point towards the player object:

direction = custom_point_direction(x,y,player_obj.x,player_obj.y);
image_angle = direction;

This does not work like GameMaker's point_direction() function. How do I make my custom_point_direction() work similar to point_direction()?

\$\endgroup\$
  • \$\begingroup\$ If you want the functionality of point_direction(), why don't you try using point_direction()? \$\endgroup\$ – Gnemlock Oct 15 '16 at 23:18
1
\$\begingroup\$

You missed y1 and x2 which not used in your formula, it must be

arctan2(y1 - y2, x1 - x2)

For same with GM you can use this formula:

-radtodeg(arctan2(y1 - y2, x1 - x2)) + 180;

See a small example.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.