Created
April 18, 2016 17:04
-
-
Save Beeblerox/304fb0bce5996079e3915813b1a8af67 to your computer and use it in GitHub Desktop.
FlxSprite with correct bounding box calculations
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
package; | |
import flixel.FlxCamera; | |
import flixel.FlxG; | |
import flixel.FlxSprite; | |
import flixel.math.FlxRect; | |
/** | |
* ... | |
* @author Zaphod | |
*/ | |
class FlxBoundingBoxSprite extends FlxSprite | |
{ | |
override private function getBoundingBox(camera:FlxCamera):FlxRect | |
{ | |
getScreenPosition(_point, camera).subtractPoint(offset); | |
if (isSimpleRender(camera)) | |
{ | |
return _rect.set(_point.x, _point.y, frameWidth, frameHeight); | |
} | |
_point.add(origin.x, origin.y); | |
var px:Float = _point.x; | |
var py:Float = _point.y; | |
_matrix.identity(); | |
_matrix.translate(-origin.x, -origin.y); | |
_matrix.scale(scale.x, scale.y); | |
updateTrig(); | |
if (angle != 0) | |
{ | |
_matrix.rotateWithTrig(_cosAngle, _sinAngle); | |
} | |
_point.set(0, 0); | |
_point.transform(_matrix); | |
var x1:Float = _point.x; | |
var y1:Float = _point.y; | |
_point.set(frameWidth, 0); | |
_point.transform(_matrix); | |
var x2:Float = _point.x; | |
var y2:Float = _point.y; | |
_point.set(frameWidth, frameHeight); | |
_point.transform(_matrix); | |
var x3:Float = _point.x; | |
var y3:Float = _point.y; | |
_point.set(0, frameHeight); | |
_point.transform(_matrix); | |
var x4:Float = _point.x; | |
var y4:Float = _point.y; | |
var minX:Float = Math.min(x1, Math.min(x2, Math.min(x3, x4))); | |
var minY:Float = Math.min(y1, Math.min(y2, Math.min(y3, y4))); | |
var maxX:Float = Math.max(x1, Math.max(x2, Math.max(x3, x4))); | |
var maxY:Float = Math.max(y1, Math.max(y2, Math.max(y3, y4))); | |
return _rect.set(px + minX, py + minY, maxX - minX, maxY - minY); | |
} | |
public function calculateBounds(?rect:FlxRect, ?camera:FlxCamera):FlxRect | |
{ | |
if (camera == null) | |
{ | |
camera = FlxG.camera; | |
} | |
return getBoundingBox(camera); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment