GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

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

redeyesmaster
Member
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))
      draw_set_color(c_red)
      draw_circle(px+lengthdir_x(trav,dir),py+lengthdir_y(trav,dir),3,false)
      draw_set_color(c_white)

      return trav
      
}else{
  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.
https://i.gyazo.com/879caa9518b896f9870 … 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
https://i.gyazo.com/5a0c93e2230f593c28b … 8fdf2f.mp4

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

Offline

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

redeyesmaster
Member
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.

Offline

Board footer

Powered by FluxBB