DrawGUI in relation to object coordinates with rotating camera

Dycedargb

Member
Basically I want to draw a line from a GUI element to an objects position in the room.

I adapted the following from an old forum topic [solved-translate-gui-to-game-view-coordinates.27101].

The object in the room updates its GUI translated coordinates each step:
GML:
    //get camera position data
    var cam_left = camera_get_view_x(view_camera);
    var cam_top = camera_get_view_y(view_camera);
    var cam_w = camera_get_view_width(view_camera);
    var cam_h = camera_get_view_height(view_camera);
    var cam_right = cam_left + cam_w;
    var cam_bottom = cam_top + cam_h;
  
    //center of camera  coordinates
    var cam_x = (cam_left + cam_right) / 2;
    var cam_y = (cam_top + cam_bottom) / 2;
    //display scale
  
    var dsx = display_get_gui_width() / cam_w;
    var dsy = display_get_gui_height() / cam_h;
  
    //this works without rotation
    gx = (x-cam_left) * dsx;
    gy = (y-cam_top) * dsy;

Then the GUI elements position is kept relative to the object's position on screen and draws a line to its position. (This is just an example):

GML:
    //move to lower right of unit
    x = clamp(owner.gx + 42, 0, display_get_gui_width() - width);
    y = clamp(owner.gy + 42, 0, display_get_gui_height() - height);
  
    //draw line to unit
    draw_line_width(owner.gx, owner.gy, x, y, 2);

Now, I'm trying to use lengthdir functions in relation to the camera's center. Is this the right approach?

GML:
    var cam_dist = point_distance(cam_x, cam_y, x, y);
    var cam_angle = point_direction(cam_x, cam_y, x, y) + camera_get_view_angle(view_camera);
  
    gx = (cam_w / 2) + (lengthdir_x(cam_dist, cam_angle) * dsx);
    gy = (cam_h / 2) + (lengthdir_y(cam_dist, cam_angle) * dsx);
Thanks for your help in advance!

EDIT: fixed typo in last line of code

Using GMS v. 2.2.5.481
 
Last edited:

Dycedargb

Member
Tom Francis had to solve this for Heat Signature. Maybe this article could help you out.
AH of course! My mistake was referencing the camera instead of the display in those last two lines.

It looks like his code is for an older version of GM, but I get it now.

Here is the working code for those interested:

GML:
    //get camera position data
    var cam_left = camera_get_view_x(view_camera);
    var cam_top = camera_get_view_y(view_camera);
    var cam_w = camera_get_view_width(view_camera);
    var cam_h = camera_get_view_height(view_camera);
    var cam_right = cam_left + cam_w;
    var cam_bottom = cam_top + cam_h;
    
    //center of camera  coordinates
    var cam_x = (cam_left + cam_right) / 2;
    var cam_y = (cam_top + cam_bottom) / 2;

    //display scale
    var dsx = display_get_gui_width() / cam_w;
    var dsy = display_get_gui_height() / cam_h;
    
    var cam_dist = point_distance(cam_x, cam_y, x, y);
    var cam_angle = point_direction(cam_x, cam_y, x, y) + camera_get_view_angle(view_camera);
    
    gx = (display_get_gui_width() / 2) + (lengthdir_x(cam_dist, cam_angle) * dsx);
    gy = (display_get_gui_height() / 2) + (lengthdir_y(cam_dist, cam_angle) * dsx);
Thanks to Pixelated_Pope and Tom Francis for helping me find the answer.
 
Top