Discuss and collaborate on GML scripts

You are not logged in.

#1 2020-08-18 08:10:24

Registered: 2020-08-09
Posts: 43

Rectangle Point Direction [unfinished]

This is a bit of code I do not plan on finishing but the part that is finished works extremely well.

Expand/// rect_point_dir(x1,y1,x2,y2,px,py,dir)

var x1,y1,x2,y2,px,py,dir;
x1 = argument0
y1 = argument1
x2 = argument2
y2 = argument3
px = argument4
py = argument5
dir = argument6

var rect_width  = x2-x1
var rect_height = y2-y1

if point_in_rectangle(px,py,x1,y1,x2,y2)
      var vx = cos(degtorad(dir))
      var vy = sin(degtorad(dir))
      var trav = rect_width*rect_height
      if (vx != 0) {
        t = (x1-px)/vx;
          if (t > 0) trav = min(t,trav);
          draw_text(0,16,"left = "+string(t))
        t = (x2-px)/vx; 
          if (t > 0) trav = min(t,trav);
          draw_text(0,32,"right = "+string(t))
      if (vy != 0) {
        t = (py-y1)/vy; 
          if (t > 0) trav = min(t,trav);
          draw_text(0,48,"top = "+string(t))
        t = (py-y2)/vy; 
          if (t > 0) trav = min(t,trav);
          draw_text(0,64,"bottom = "+string(t))
      draw_text(0,0,"travel = "+string(trav))

      return trav
  return -1

Feel free to edit and change this how ever you would like. The code which needs to be added before a proper submission would be to allow the px and py to be outside of the rectangle, currently it only supports when it is inside. Here is a short video demonstrating it. … 5cb92b.mp4

There are still draw functions to help with debugging in this script if you would like to check out how it works further.

[Edit]: I found a bug with an offset square and the y system, since it's using sin() you want to use (py-y1)/vy. Thebug has been fixed in the code above. Here is a short video showing off offset px, py … 8fdf2f.mp4

Last edited by redeyesmaster (2020-08-18 08:39:49)


#2 2020-08-18 08:14:13

Registered: 2020-08-09
Posts: 43

Re: Rectangle Point Direction [unfinished]

A new function name, and proper descriptions would also be needed for a proper submission to the math's section of scripts. The inputs should be self explanatory to most, and the output is the distance between the px, py, and the border of the box. Theoretically one would be able to convert this to work with shape although that's a LOT more work to be able to make it dynamic.


Board footer

Powered by FluxBB