Skip to content

Commit

Permalink
Partial merge of #1760
Browse files Browse the repository at this point in the history
closes #1760
  • Loading branch information
Gama11 committed Aug 21, 2016
1 parent c551ed6 commit c52b534
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 44 deletions.
27 changes: 12 additions & 15 deletions flixel/input/FlxPointer.hx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class FlxPointer

private var _globalScreenX:Int = 0;
private var _globalScreenY:Int = 0;
private static var _cachedPoint:FlxPoint = new FlxPoint();

public function new() {}

Expand All @@ -36,10 +37,9 @@ class FlxPointer
{
point = FlxPoint.get();
}
var screenPosition:FlxPoint = getScreenPosition(Camera);
point.x = screenPosition.x + Camera.scroll.x;
point.y = screenPosition.y + Camera.scroll.y;
screenPosition.put();
getScreenPosition(Camera, _cachedPoint);
point.x = _cachedPoint.x + Camera.scroll.x;
point.y = _cachedPoint.y + Camera.scroll.y;
return point;
}

Expand Down Expand Up @@ -106,10 +106,9 @@ class FlxPointer
}
else
{
var point:FlxPoint = getPosition();
getPosition(_cachedPoint);
var object:FlxObject = cast ObjectOrGroup;
result = object.overlapsPoint(point, true, Camera);
point.put();
result = object.overlapsPoint(_cachedPoint, true, Camera);
}

return result;
Expand Down Expand Up @@ -140,14 +139,12 @@ class FlxPointer
*/
private function updatePositions():Void
{
var screenPosition:FlxPoint = getScreenPosition();
screenX = Std.int(screenPosition.x);
screenY = Std.int(screenPosition.y);
screenPosition.put();
getScreenPosition(FlxG.camera, _cachedPoint);
screenX = Std.int(_cachedPoint.x);
screenY = Std.int(_cachedPoint.y);

var worldPosition = getWorldPosition();
x = Std.int(worldPosition.x);
y = Std.int(worldPosition.y);
worldPosition.put();
getWorldPosition(FlxG.camera, _cachedPoint);
x = Std.int(_cachedPoint.x);
y = Std.int(_cachedPoint.y);
}
}
6 changes: 2 additions & 4 deletions flixel/input/keyboard/FlxKeyboard.hx
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,11 @@ class FlxKeyboard extends FlxKeyManager<FlxKey, FlxKeyList>
{
var i:Int = 0;
var l:Int = Record.length;
var o:CodeValuePair;
var o2:FlxKeyInput;

while (i < l)
{
o = Record[i++];
o2 = getKey(o.code);
var o = Record[i++];
var o2 = getKey(o.code);
o2.current = o.value;
}
}
Expand Down
11 changes: 7 additions & 4 deletions flixel/math/FlxRect.hx
Original file line number Diff line number Diff line change
Expand Up @@ -367,26 +367,29 @@ class FlxRect implements IFlxPooled
* @param rect Rectangle to check intersection againist.
* @return The area of intersection of two rectangles.
*/
public function intersection(rect:FlxRect):FlxRect
public function intersection(rect:FlxRect, ?result:FlxRect):FlxRect
{
if (result == null)
result = FlxRect.get();

var x0:Float = x < rect.x ? rect.x : x;
var x1:Float = right > rect.right ? rect.right : right;
if (x1 <= x0)
{
rect.putWeak();
return FlxRect.get(0, 0, 0, 0);
return result;
}

var y0:Float = y < rect.y ? rect.y : y;
var y1:Float = bottom > rect.bottom ? rect.bottom : bottom;
if (y1 <= y0)
{
rect.putWeak();
return FlxRect.get(0, 0, 0, 0);
return result;
}

rect.putWeak();
return FlxRect.get(x0, y0, x1 - x0, y1 - y0);
return result.set(x0, y0, x1 - x0, y1 - y0);
}

/**
Expand Down
52 changes: 31 additions & 21 deletions flixel/util/FlxCollision.hx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import flixel.FlxSprite;
import flixel.group.FlxGroup;
import flixel.math.FlxAngle;
import flixel.math.FlxMath;
import flixel.math.FlxRect;
import flixel.math.FlxVector;
import flixel.math.FlxMatrix;
import flixel.tile.FlxTileblock;

/**
Expand All @@ -21,15 +24,17 @@ import flixel.tile.FlxTileblock;
class FlxCollision
{
// Optimization: Local static vars to reduce allocations
private static var pointA:Point = new Point();
private static var pointB:Point = new Point();
private static var centerA:Point = new Point();
private static var centerB:Point = new Point();
private static var matrixA:Matrix = new Matrix();
private static var matrixB:Matrix = new Matrix();
private static var testMatrix:Matrix = new Matrix();
private static var boundsA:Rectangle = new Rectangle();
private static var boundsB:Rectangle = new Rectangle();
private static var pointA:FlxVector = new FlxVector();
private static var pointB:FlxVector = new FlxVector();
private static var centerA:FlxVector = new FlxVector();
private static var centerB:FlxVector = new FlxVector();
private static var matrixA:FlxMatrix = new FlxMatrix();
private static var matrixB:FlxMatrix = new FlxMatrix();
private static var testMatrix:FlxMatrix = new FlxMatrix();
private static var boundsA:FlxRect = new FlxRect();
private static var boundsB:FlxRect = new FlxRect();
private static var intersect:FlxRect = new FlxRect();
private static var flashRect:Rectangle = new Rectangle();

/**
* A Pixel Perfect Collision check between two FlxSprites. It will do a bounds check first, and if that passes it will run a
Expand Down Expand Up @@ -57,18 +62,20 @@ class FlxCollision
if (considerRotation)
{
// find the center of both sprites
Contact.origin.copyToFlash(centerA);
Target.origin.copyToFlash(centerB);
Contact.origin.copyTo(centerA);
Target.origin.copyTo(centerB);

// now make a bounding box that allows for the sprite to be rotated in 360 degrees
boundsA.x = (pointA.x + centerA.x - centerA.length);
boundsA.y = (pointA.y + centerA.y - centerA.length);
boundsA.width = centerA.length * 2;
var lengthA = centerA.length;
boundsA.x = (pointA.x + centerA.x - lengthA);
boundsA.y = (pointA.y + centerA.y - lengthA);
boundsA.width = lengthA * 2;
boundsA.height = boundsA.width;

boundsB.x = (pointB.x + centerB.x - centerB.length);
boundsB.y = (pointB.y + centerB.y - centerB.length);
boundsB.width = centerB.length * 2;
var lengthB = centerB.length;
boundsB.x = (pointB.x + centerB.x - lengthB);
boundsB.y = (pointB.y + centerB.y - lengthB);
boundsB.width = lengthB * 2;
boundsB.height = boundsB.width;
}
else
Expand All @@ -84,9 +91,9 @@ class FlxCollision
boundsB.height = Target.frameHeight;
}

var intersect:Rectangle = boundsA.intersection(boundsB);
boundsA.intersection(boundsB, intersect.set());

if (intersect.isEmpty() || intersect.width < 1 || intersect.height < 1)
if (intersect.isEmpty || intersect.width < 1 || intersect.height < 1)
{
return false;
}
Expand Down Expand Up @@ -152,8 +159,11 @@ class FlxCollision
boundsB.width = overlapWidth;
boundsB.height = overlapHeight;

var pixelsA = testA.getPixels(boundsA);
var pixelsB = testB.getPixels(boundsB);
boundsA.copyToFlash(flashRect);
var pixelsA = testA.getPixels(flashRect);

boundsB.copyToFlash(flashRect);
var pixelsB = testB.getPixels(flashRect);

var hit = false;

Expand Down

0 comments on commit c52b534

Please sign in to comment.