Skip to content

Instantly share code, notes, and snippets.

@radiatoryang
Last active May 4, 2021 04:06
Show Gist options
  • Save radiatoryang/4f9d895cd2783b3789f8b9a0c68666e7 to your computer and use it in GitHub Desktop.
Save radiatoryang/4f9d895cd2783b3789f8b9a0c68666e7 to your computer and use it in GitHub Desktop.
Terrible hack for HaxeFlixel that adds a separation (collision) function for circle-shaped hitboxes... it's a hack, so it just assumes a circular collider of radius = width/2 ... plug this into a custom FlxG.overlap collision call in your main PlayState... also I don't know how to code so sorry lol
public static function separateCircle(circle1:FlxSprite, circle2:FlxSprite):Bool
{
var totalRadius:Float = circle1.width / 2 + circle2.width / 2;
var c1 = circle1.getMidpoint(FlxPoint.weak());
var c2 = circle2.getMidpoint(FlxPoint.weak());
var distanceSquared:Float = (c1.x - c2.x) * (c1.x - c2.x) + (c1.y - c2.y) * (c1.y - c2.y);
if (distanceSquared < totalRadius * totalRadius)
{
var overlap:Float = totalRadius - Math.sqrt(distanceSquared);
var normal = FlxVector.get(c1.x - c2.x, c1.y - c2.y).normalize().scale(overlap);
circle1.setPosition(circle1.x + normal.x, circle1.y + normal.y);
c1.putWeak();
c2.putWeak();
normal.put();
return true;
}
c1.putWeak();
c2.putWeak();
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment