Last active
June 7, 2016 11:18
-
-
Save augustlate/4588cb986dc456b5747ab1e4e7f63f06 to your computer and use it in GitHub Desktop.
Bounding Rectangle helper class for kha
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Copyright 2016 August Late. Licensed under zlib. | |
//Tiny utility class for working with bounding rectangles | |
import kha.math.Vector2; | |
import kha.math.FastMatrix4; | |
class Rect { | |
//Computes the smallest axis aligned rect that will contain the transformed rect. | |
public static function transform(rect : Rect,m : FastMatrix4) : Rect { | |
var r = new Rect(); | |
r.center.x = m._00 * rect.center.x + m._10 * rect.center.y + m._30; | |
r.center.y = m._01 * rect.center.x + m._11 * rect.center.y + m._31; | |
r.halfExtents.x = Math.abs(m._00)*rect.halfExtents.x + Math.abs(m._10)*rect.halfExtents.y; | |
r.halfExtents.y = Math.abs(m._01)*rect.halfExtents.x + Math.abs(m._11)*rect.halfExtents.y; | |
return r; | |
} | |
static public function MinMax(min_x : Float, min_y : Float, max_x : Float, max_y : Float) : Rect{ | |
return Rect.create(min_x,min_y,Math.abs(max_x-min_x),Math.abs(max_y-min_y)); | |
} | |
static public function create(x : Float,y : Float, width:Float, height:Float) : Rect{ | |
var rect = new Rect(); | |
rect.halfExtents = new Vector2(0.5*width,0.5*height); | |
rect.center = new Vector2(x+rect.halfExtents.x,y+rect.halfExtents.y); | |
return rect; | |
} | |
public function new(){ | |
center = new Vector2(); | |
halfExtents = new Vector2(); | |
} | |
public var min(get,never) : Vector2; | |
public var max(get,never) : Vector2; | |
public var x(get,never) : Float; | |
public var y(get,never) : Float; | |
public var width(get,never) : Float; | |
public var height(get,never) : Float; | |
@:embed public var center : Vector2; | |
@:embed public var halfExtents : Vector2; | |
function get_min() : Vector2 { | |
return center.sub(halfExtents); | |
} | |
function get_max() : Vector2 { | |
return center.add(halfExtents); | |
} | |
function get_x() : Float { return center.x-halfExtents.x; } | |
function get_y() : Float { return center.y-halfExtents.y; } | |
function get_width() : Float { return halfExtents.x*2; } | |
function get_height() : Float { return halfExtents.y*2; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Copyright 2016 August Late. Licensed as zlib. | |
//This code lets us generate a complete 2d transform in a single step. | |
function computeTransform(rotation : Float, origin : Vector2, position : Vector2, scale : Vector2){ | |
var cos = Math.cos(rotation); | |
var sin = Math.sin(rotation); | |
var scaled_origin = new FastVector2( | |
origin.x*scale.x, | |
origin.y*scale.y | |
); | |
var transformed_origin = new FastVector2( | |
scaled_origin.x * cos - scaled_origin.y * sin, | |
scaled_origin.x * sin + scaled_origin.y * cos | |
); | |
return new FastMatrix4( | |
scale.x*cos, scale.y*-sin,0, position.x-transformed_origin.x, | |
scale.x*sin, scale.y*cos, 0, position.y-transformed_origin.y, | |
0, 0, 1, 0, | |
0, 0, 0, 1 | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Copyright 2016 August Late. Licensed under zlib. | |
//This code makes using transforms to compute scissor rects extremely painless. | |
var rect = Rect.MinMax(0,0,1,1); | |
var scissor = Rect.transform(rect,viewMatrix.multmat(object.localToWorld)); | |
g.scissor(Std.int(scissor.x),Std.int(scissor.y),Std.int(scissor.width),Std.int(scissor.height)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment