Skip to content

Instantly share code, notes, and snippets.

@kinoko87
Created November 3, 2022 07:08
Show Gist options
  • Save kinoko87/fcf468c114621f0ef182e12ec3212c9d to your computer and use it in GitHub Desktop.
Save kinoko87/fcf468c114621f0ef182e12ec3212c9d to your computer and use it in GitHub Desktop.
Ray class in haxe
package;
import flixel.math.FlxPoint;
import flixel.math.FlxRect;
import flixel.math.FlxVector;
import haxe.ds.Vector;
class Ray
{
public var start:FlxPoint;
public var end:FlxPoint;
public var x(get, set):Float;
public var y(get, set):Float;
public var dx(get, set):Float;
public var dy(get, set):Float;
private static var ray_cache:Array<Ray> = [];
public function new(x:Float, y:Float, dx:Float, dy:Float)
{
start = new FlxPoint(x, y);
end = new FlxPoint(dx, dy);
trace(start.toString());
trace(end.toString());
trace(x, y, dx, dy);
}
public function intersectsRay(r:Ray)
{
var x1 = r.start.x;
var y1 = r.start.y;
var x2 = r.end.x;
var y2 = r.end.y;
var x3 = start.x;
var y3 = start.y;
var x4 = start.x + end.x;
var y4 = start.y + end.y;
var denom = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
if (denom == 0)
return null;
var t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / denom;
var u = -((x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3)) / denom;
if (t >= 0 && t <= 1 && u >= 0 && u <= 1)
{
return new FlxPoint(x1 + t * (x2 - x1), y1 + t * (y2 - y1));
}
return null;
}
// boolean because im fucking lazy
// ill return a point one day
public function intersectsRect(r:FlxRect)
{
var left = r.left;
var right = r.right;
var top = r.top;
var bottom = r.bottom;
var ray = Ray.get(left, top, right, top);
if (intersectsRay(ray) != null)
return true;
ray.set(left, top, left, bottom);
if (intersectsRay(ray) != null)
return true;
ray.set(left, bottom, right, bottom);
if (intersectsRay(ray) != null)
return true;
ray.set(right, top, right, bottom);
if (intersectsRay(ray) != null)
return true;
return false;
}
public static inline function fromRay(r:Ray)
{
return new Ray(r.start.x, r.start.y, r.end.x, r.end.y);
}
public inline static function fromPoints(start:FlxPoint, end:FlxPoint)
{
return new Ray(start.x, start.y, end.x, end.y);
}
public static function get(x:Float, y:Float, dx:Float, dy:Float)
{
for (v in ray_cache)
{
if (v.x == x && v.y == y && v.dx == dx && v.dy == dy)
return v;
}
var v = new Ray(x, y, dx, dy);
v.put();
return v;
}
public function set(x:Float, y:Float, dx:Float, dy:Float)
{
start.set(x, y);
end.set(dx, dy);
return this;
}
public function put()
{
ray_cache.push(this);
}
public function equals(r:Ray)
{
return x == r.x && y == r.y && dx == r.dx && y == r.dy;
}
public inline function clone():Ray
return new Ray(x, y, dx, dy);
function get_x()
return start.x;
function set_x(v:Float)
return start.x = v;
function get_y()
return start.y;
function set_y(v:Float)
return start.y = v;
function get_dx()
return end.x;
function set_dx(v:Float)
return end.x = v;
function get_dy()
return end.y;
function set_dy(v:Float)
return end.y = v;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment