From f808001eac6c2cb029a8e10ef0e17d813484ac49 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Fri, 1 Oct 2021 18:30:24 -0500 Subject: [PATCH 01/15] preserve momentum in collisions --- flixel/FlxObject.hx | 50 ++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/flixel/FlxObject.hx b/flixel/FlxObject.hx index 9eb4118783..9da574b040 100644 --- a/flixel/FlxObject.hx +++ b/flixel/FlxObject.hx @@ -221,8 +221,10 @@ class FlxObject extends FlxBasic // Then adjust their positions and velocities accordingly (if there was any overlap) if (overlap != 0) { - var obj1v:Float = Object1.velocity.x; - var obj2v:Float = Object2.velocity.x; + var vel1 = Object1.velocity.x; + var vel2 = Object2.velocity.x; + var mass1 = Object1.mass; + var mass2 = Object2.mass; if (!obj1immovable && !obj2immovable) { @@ -230,23 +232,20 @@ class FlxObject extends FlxBasic Object1.x = Object1.x - overlap; Object2.x += overlap; - var obj1velocity:Float = Math.sqrt((obj2v * obj2v * Object2.mass) / Object1.mass) * ((obj2v > 0) ? 1 : -1); - var obj2velocity:Float = Math.sqrt((obj1v * obj1v * Object1.mass) / Object2.mass) * ((obj1v > 0) ? 1 : -1); - var average:Float = (obj1velocity + obj2velocity) * 0.5; - obj1velocity -= average; - obj2velocity -= average; - Object1.velocity.x = average + obj1velocity * Object1.elasticity; - Object2.velocity.x = average + obj2velocity * Object2.elasticity; + var newVel1 = (mass1 * vel1 + mass2 * vel2 + Object1.elasticity * mass2 * (vel2 - vel1)) / (mass1 + mass2); + var newVel2 = (mass2 * vel2 + mass1 * vel1 + Object2.elasticity * mass1 * (vel1 - vel2)) / (mass1 + mass2); + Object1.velocity.x = newVel1; + Object2.velocity.x = newVel2; } else if (!obj1immovable) { Object1.x = Object1.x - overlap; - Object1.velocity.x = obj2v - obj1v * Object1.elasticity; + Object1.velocity.x = vel2 - vel1 * Object1.elasticity; } else if (!obj2immovable) { Object2.x += overlap; - Object2.velocity.x = obj1v - obj2v * Object2.elasticity; + Object2.velocity.x = vel1 - vel2 * Object2.elasticity; } return true; } @@ -378,10 +377,12 @@ class FlxObject extends FlxBasic // Then adjust their positions and velocities accordingly (if there was any overlap) if (overlap != 0) { - var obj1delta:Float = Object1.y - Object1.last.y; - var obj2delta:Float = Object2.y - Object2.last.y; - var obj1v:Float = Object1.velocity.y; - var obj2v:Float = Object2.velocity.y; + var delta1:Float = Object1.y - Object1.last.y; + var delta2:Float = Object2.y - Object2.last.y; + var vel1:Float = Object1.velocity.y; + var vel2:Float = Object2.velocity.y; + var mass1:Float = Object1.mass; + var mass2:Float = Object2.mass; if (!obj1immovable && !obj2immovable) { @@ -389,20 +390,17 @@ class FlxObject extends FlxBasic Object1.y = Object1.y - overlap; Object2.y += overlap; - var obj1velocity:Float = Math.sqrt((obj2v * obj2v * Object2.mass) / Object1.mass) * ((obj2v > 0) ? 1 : -1); - var obj2velocity:Float = Math.sqrt((obj1v * obj1v * Object1.mass) / Object2.mass) * ((obj1v > 0) ? 1 : -1); - var average:Float = (obj1velocity + obj2velocity) * 0.5; - obj1velocity -= average; - obj2velocity -= average; - Object1.velocity.y = average + obj1velocity * Object1.elasticity; - Object2.velocity.y = average + obj2velocity * Object2.elasticity; + var newVel1 = (mass1 * vel1 + mass2 * vel2 + Object1.elasticity * mass2 * (vel2 - vel1)) / (mass1 + mass2); + var newVel2 = (mass2 * vel2 + mass1 * vel1 + Object2.elasticity * mass1 * (vel1 - vel2)) / (mass1 + mass2); + Object1.velocity.y = newVel1; + Object2.velocity.y = newVel2; } else if (!obj1immovable) { Object1.y = Object1.y - overlap; - Object1.velocity.y = obj2v - obj1v * Object1.elasticity; + Object1.velocity.y = vel2 - vel1 * Object1.elasticity; // This is special case code that handles cases like horizontal moving platforms you can ride - if (Object1.collisonXDrag && Object2.active && Object2.moves && (obj1delta > obj2delta)) + if (Object1.collisonXDrag && Object2.active && Object2.moves && (delta1 > delta2)) { Object1.x += Object2.x - Object2.last.x; } @@ -410,9 +408,9 @@ class FlxObject extends FlxBasic else if (!obj2immovable) { Object2.y += overlap; - Object2.velocity.y = obj1v - obj2v * Object2.elasticity; + Object2.velocity.y = vel1 - vel2 * Object2.elasticity; // This is special case code that handles cases like horizontal moving platforms you can ride - if (Object2.collisonXDrag && Object1.active && Object1.moves && (obj1delta < obj2delta)) + if (Object2.collisonXDrag && Object1.active && Object1.moves && (delta1 < delta2)) { Object2.x += Object1.x - Object1.last.x; } From ee82ab66c05b3528e554ab56724d4536dd33a815 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Fri, 1 Oct 2021 19:12:16 -0500 Subject: [PATCH 02/15] add `CollisionDragType`, and `collisionYDrag` --- flixel/FlxObject.hx | 67 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 54 insertions(+), 13 deletions(-) diff --git a/flixel/FlxObject.hx b/flixel/FlxObject.hx index 9da574b040..430078e6f4 100644 --- a/flixel/FlxObject.hx +++ b/flixel/FlxObject.hx @@ -121,6 +121,17 @@ class FlxObject extends FlxBasic return touchingX || touchingY; } + static function allowCollisionDrag(type:CollisionDragType, object1:FlxObject, object2:FlxObject):Bool + { + return object2.active && object2.moves && switch (type) + { + case NEVER: false; + case ALWAYS: true; + case IMMOVABLE: object2.immovable; + case HEAVIER: object2.mass > object1.mass; + } + } + /** * Internal function that computes overlap among two objects on the X axis. It also updates the `touching` variable. * `checkMaxOverlap` is used to determine whether we want to exclude (therefore check) overlaps which are @@ -221,6 +232,8 @@ class FlxObject extends FlxBasic // Then adjust their positions and velocities accordingly (if there was any overlap) if (overlap != 0) { + var delta1 = Object1.x - Object1.last.x; + var delta2 = Object2.x - Object2.last.x; var vel1 = Object1.velocity.x; var vel2 = Object2.velocity.x; var mass1 = Object1.mass; @@ -247,6 +260,13 @@ class FlxObject extends FlxBasic Object2.x += overlap; Object2.velocity.x = vel1 - vel2 * Object2.elasticity; } + + // use collisionDrag properties to determine whether one object + if (allowCollisionDrag(Object1.collisonYDrag, Object1, Object2) && delta1 > delta2) + Object1.y += Object2.y - Object2.last.y; + else if (allowCollisionDrag(Object2.collisonYDrag, Object2, Object1) && delta2 > delta1) + Object2.y += Object1.y - Object1.last.y; + return true; } @@ -377,8 +397,8 @@ class FlxObject extends FlxBasic // Then adjust their positions and velocities accordingly (if there was any overlap) if (overlap != 0) { - var delta1:Float = Object1.y - Object1.last.y; - var delta2:Float = Object2.y - Object2.last.y; + var delta1 = Object1.y - Object1.last.y; + var delta2 = Object2.y - Object2.last.y; var vel1:Float = Object1.velocity.y; var vel2:Float = Object2.velocity.y; var mass1:Float = Object1.mass; @@ -399,22 +419,19 @@ class FlxObject extends FlxBasic { Object1.y = Object1.y - overlap; Object1.velocity.y = vel2 - vel1 * Object1.elasticity; - // This is special case code that handles cases like horizontal moving platforms you can ride - if (Object1.collisonXDrag && Object2.active && Object2.moves && (delta1 > delta2)) - { - Object1.x += Object2.x - Object2.last.x; - } } else if (!obj2immovable) { Object2.y += overlap; Object2.velocity.y = vel1 - vel2 * Object2.elasticity; - // This is special case code that handles cases like horizontal moving platforms you can ride - if (Object2.collisonXDrag && Object1.active && Object1.moves && (delta1 < delta2)) - { - Object2.x += Object1.x - Object1.last.x; - } } + + // use collisionDrag properties to determine whether one object + if (allowCollisionDrag(Object1.collisonXDrag, Object1, Object2) && delta1 > delta2) + Object1.x += Object2.x - Object2.last.x; + else if (allowCollisionDrag(Object2.collisonXDrag, Object2, Object1) && delta2 > delta1) + Object2.x += Object1.x - Object1.last.x; + return true; } @@ -597,7 +614,13 @@ class FlxObject extends FlxBasic * Whether this sprite is dragged along with the horizontal movement of objects it collides with * (makes sense for horizontally-moving platforms in platformers for example). */ - public var collisonXDrag:Bool = true; + public var collisonXDrag = CollisionDragType.IMMOVABLE; + + /** + * Whether this sprite is dragged along with the vertical movement of objects it collides with + * (for sticking to vertically-moving platforms in platformers for example). + */ + public var collisonYDrag = CollisionDragType.NEVER; #if FLX_DEBUG /** @@ -1336,3 +1359,21 @@ class FlxObject extends FlxBasic return this.path = path; } } + +/** + * Determines when to apply collision drag to one object that collided with another. + */ +@:enum abstract CollisionDragType(Int) +{ + /** Never drags on colliding objects. */ + var NEVER = 0; + + /** Always drags on colliding objects. */ + var ALWAYS = 1; + + /** Drags when colliding with immovable objects. */ + var IMMOVABLE = 2; + + /** Drags when colliding with heavier objects. */ + var HEAVIER = 3; +} \ No newline at end of file From ba333964748b44c5cc4caea4773dfc610f7bc541 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Fri, 1 Oct 2021 19:39:07 -0500 Subject: [PATCH 03/15] fix typo, add `@since`, count immovable as heavier --- flixel/FlxObject.hx | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/flixel/FlxObject.hx b/flixel/FlxObject.hx index 430078e6f4..c4b7c6c88e 100644 --- a/flixel/FlxObject.hx +++ b/flixel/FlxObject.hx @@ -128,7 +128,7 @@ class FlxObject extends FlxBasic case NEVER: false; case ALWAYS: true; case IMMOVABLE: object2.immovable; - case HEAVIER: object2.mass > object1.mass; + case HEAVIER: object2.immovable || object2.mass > object1.mass; } } @@ -262,9 +262,9 @@ class FlxObject extends FlxBasic } // use collisionDrag properties to determine whether one object - if (allowCollisionDrag(Object1.collisonYDrag, Object1, Object2) && delta1 > delta2) + if (allowCollisionDrag(Object1.collisionYDrag, Object1, Object2) && delta1 > delta2) Object1.y += Object2.y - Object2.last.y; - else if (allowCollisionDrag(Object2.collisonYDrag, Object2, Object1) && delta2 > delta1) + else if (allowCollisionDrag(Object2.collisionYDrag, Object2, Object1) && delta2 > delta1) Object2.y += Object1.y - Object1.last.y; return true; @@ -427,9 +427,9 @@ class FlxObject extends FlxBasic } // use collisionDrag properties to determine whether one object - if (allowCollisionDrag(Object1.collisonXDrag, Object1, Object2) && delta1 > delta2) + if (allowCollisionDrag(Object1.collisionXDrag, Object1, Object2) && delta1 > delta2) Object1.x += Object2.x - Object2.last.x; - else if (allowCollisionDrag(Object2.collisonXDrag, Object2, Object1) && delta2 > delta1) + else if (allowCollisionDrag(Object2.collisionXDrag, Object2, Object1) && delta2 > delta1) Object2.x += Object1.x - Object1.last.x; return true; @@ -614,13 +614,14 @@ class FlxObject extends FlxBasic * Whether this sprite is dragged along with the horizontal movement of objects it collides with * (makes sense for horizontally-moving platforms in platformers for example). */ - public var collisonXDrag = CollisionDragType.IMMOVABLE; + public var collisionXDrag = CollisionDragType.IMMOVABLE; /** * Whether this sprite is dragged along with the vertical movement of objects it collides with * (for sticking to vertically-moving platforms in platformers for example). + * @since 4.11.0 */ - public var collisonYDrag = CollisionDragType.NEVER; + public var collisionYDrag = CollisionDragType.NEVER; #if FLX_DEBUG /** @@ -1374,6 +1375,6 @@ class FlxObject extends FlxBasic /** Drags when colliding with immovable objects. */ var IMMOVABLE = 2; - /** Drags when colliding with heavier objects. */ + /** Drags when colliding with heavier objects. Immovable objects have infinite mass. */ var HEAVIER = 3; } \ No newline at end of file From 11ee4a85cd7b3176ca7a685e29ec096bf2c6d605 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Fri, 1 Oct 2021 19:54:42 -0500 Subject: [PATCH 04/15] add FLX_LEGACY_COLLISION to use old collision --- flixel/FlxObject.hx | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/flixel/FlxObject.hx b/flixel/FlxObject.hx index c4b7c6c88e..dd9f5e022d 100644 --- a/flixel/FlxObject.hx +++ b/flixel/FlxObject.hx @@ -245,10 +245,20 @@ class FlxObject extends FlxBasic Object1.x = Object1.x - overlap; Object2.x += overlap; + #if FLX_LEGACY_COLLISION + var newVel1 = Math.sqrt((vel2 * vel2 * mass2) / mass1) * ((vel2 > 0) ? 1 : -1); + var newVel2 = Math.sqrt((vel1 * vel1 * mass1) / mass2) * ((vel1 > 0) ? 1 : -1); + var average = (newVel1 + newVel2) * 0.5; + newVel1 -= average; + newVel2 -= average; + Object1.velocity.x = average + newVel1 * Object1.elasticity; + Object2.velocity.x = average + newVel2 * Object2.elasticity; + #else var newVel1 = (mass1 * vel1 + mass2 * vel2 + Object1.elasticity * mass2 * (vel2 - vel1)) / (mass1 + mass2); var newVel2 = (mass2 * vel2 + mass1 * vel1 + Object2.elasticity * mass1 * (vel1 - vel2)) / (mass1 + mass2); Object1.velocity.x = newVel1; Object2.velocity.x = newVel2; + #end } else if (!obj1immovable) { @@ -410,10 +420,20 @@ class FlxObject extends FlxBasic Object1.y = Object1.y - overlap; Object2.y += overlap; + #if FLX_LEGACY_COLLISION + var newVel1 = Math.sqrt((vel2 * vel2 * mass2) / mass1) * ((vel2 > 0) ? 1 : -1); + var newVel2 = Math.sqrt((vel1 * vel1 * mass1) / mass2) * ((vel1 > 0) ? 1 : -1); + var average = (newVel1 + newVel2) * 0.5; + newVel1 -= average; + newVel2 -= average; + Object1.velocity.y = average + newVel1 * Object1.elasticity; + Object2.velocity.y = average + newVel2 * Object2.elasticity; + #else var newVel1 = (mass1 * vel1 + mass2 * vel2 + Object1.elasticity * mass2 * (vel2 - vel1)) / (mass1 + mass2); var newVel2 = (mass2 * vel2 + mass1 * vel1 + Object2.elasticity * mass1 * (vel1 - vel2)) / (mass1 + mass2); Object1.velocity.y = newVel1; Object2.velocity.y = newVel2; + #end } else if (!obj1immovable) { From a3d6df8d0bf195387f08fd644d2501928544d8bc Mon Sep 17 00:00:00 2001 From: George FunBook Date: Tue, 5 Oct 2021 12:20:48 -0500 Subject: [PATCH 05/15] cleanup --- flixel/FlxObject.hx | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/flixel/FlxObject.hx b/flixel/FlxObject.hx index dd9f5e022d..6231495d3e 100644 --- a/flixel/FlxObject.hx +++ b/flixel/FlxObject.hx @@ -238,14 +238,15 @@ class FlxObject extends FlxBasic var vel2 = Object2.velocity.x; var mass1 = Object1.mass; var mass2 = Object2.mass; + var massSum = mass1 + mass2; if (!obj1immovable && !obj2immovable) { + #if FLX_LEGACY_COLLISION overlap *= 0.5; Object1.x = Object1.x - overlap; Object2.x += overlap; - #if FLX_LEGACY_COLLISION var newVel1 = Math.sqrt((vel2 * vel2 * mass2) / mass1) * ((vel2 > 0) ? 1 : -1); var newVel2 = Math.sqrt((vel1 * vel1 * mass1) / mass2) * ((vel1 > 0) ? 1 : -1); var average = (newVel1 + newVel2) * 0.5; @@ -254,15 +255,21 @@ class FlxObject extends FlxBasic Object1.velocity.x = average + newVel1 * Object1.elasticity; Object2.velocity.x = average + newVel2 * Object2.elasticity; #else - var newVel1 = (mass1 * vel1 + mass2 * vel2 + Object1.elasticity * mass2 * (vel2 - vel1)) / (mass1 + mass2); - var newVel2 = (mass2 * vel2 + mass1 * vel1 + Object2.elasticity * mass1 * (vel1 - vel2)) / (mass1 + mass2); + Object1.x -= overlap / 2; + Object2.x += overlap / 2; + // this causes stacking issues + // Object1.x -= overlap * mass2 / massSum; + // Object2.x += overlap * mass1 / massSum; + + var newVel1 = (mass1 * vel1 + mass2 * vel2 + Object1.elasticity * mass2 * (vel2 - vel1)) / massSum; + var newVel2 = (mass2 * vel2 + mass1 * vel1 + Object2.elasticity * mass1 * (vel1 - vel2)) / massSum; Object1.velocity.x = newVel1; Object2.velocity.x = newVel2; #end } else if (!obj1immovable) { - Object1.x = Object1.x - overlap; + Object1.x -= overlap; Object1.velocity.x = vel2 - vel1 * Object1.elasticity; } else if (!obj2immovable) @@ -409,18 +416,19 @@ class FlxObject extends FlxBasic { var delta1 = Object1.y - Object1.last.y; var delta2 = Object2.y - Object2.last.y; - var vel1:Float = Object1.velocity.y; - var vel2:Float = Object2.velocity.y; - var mass1:Float = Object1.mass; - var mass2:Float = Object2.mass; + var vel1 = Object1.velocity.y; + var vel2 = Object2.velocity.y; + var mass1 = Object1.mass; + var mass2 = Object2.mass; + var massSum = mass1 + mass2; if (!obj1immovable && !obj2immovable) { + #if FLX_LEGACY_COLLISION overlap *= 0.5; Object1.y = Object1.y - overlap; Object2.y += overlap; - #if FLX_LEGACY_COLLISION var newVel1 = Math.sqrt((vel2 * vel2 * mass2) / mass1) * ((vel2 > 0) ? 1 : -1); var newVel2 = Math.sqrt((vel1 * vel1 * mass1) / mass2) * ((vel1 > 0) ? 1 : -1); var average = (newVel1 + newVel2) * 0.5; @@ -429,15 +437,21 @@ class FlxObject extends FlxBasic Object1.velocity.y = average + newVel1 * Object1.elasticity; Object2.velocity.y = average + newVel2 * Object2.elasticity; #else - var newVel1 = (mass1 * vel1 + mass2 * vel2 + Object1.elasticity * mass2 * (vel2 - vel1)) / (mass1 + mass2); - var newVel2 = (mass2 * vel2 + mass1 * vel1 + Object2.elasticity * mass1 * (vel1 - vel2)) / (mass1 + mass2); + Object1.y -= overlap / 2; + Object2.y += overlap / 2; + // this causes stacking issues + // Object1.y -= overlap * mass2 / massSum; + // Object2.y += overlap * mass1 / massSum; + + var newVel1 = (mass1 * vel1 + mass2 * vel2 + Object1.elasticity * mass2 * (vel2 - vel1)) / massSum; + var newVel2 = (mass2 * vel2 + mass1 * vel1 + Object2.elasticity * mass1 * (vel1 - vel2)) / massSum; Object1.velocity.y = newVel1; Object2.velocity.y = newVel2; #end } else if (!obj1immovable) { - Object1.y = Object1.y - overlap; + Object1.y -= overlap; Object1.velocity.y = vel2 - vel1 * Object1.elasticity; } else if (!obj2immovable) From 3c883312471e4ef61f6aab6b6aacf453a2b04432 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Mon, 17 Jan 2022 14:08:07 -0600 Subject: [PATCH 06/15] improve docs --- flixel/FlxObject.hx | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/flixel/FlxObject.hx b/flixel/FlxObject.hx index 6231495d3e..8cbee837b2 100644 --- a/flixel/FlxObject.hx +++ b/flixel/FlxObject.hx @@ -646,16 +646,19 @@ class FlxObject extends FlxBasic /** * Whether this sprite is dragged along with the horizontal movement of objects it collides with - * (makes sense for horizontally-moving platforms in platformers for example). + * (makes sense for horizontally-moving platforms in platformers for example). Use values + * IMMOVABLE, ALWAYS, HEAVIER or NEVER + * @since 4.11.0 */ - public var collisionXDrag = CollisionDragType.IMMOVABLE; - + public var collisionXDrag:CollisionDragType = IMMOVABLE; + /** * Whether this sprite is dragged along with the vertical movement of objects it collides with - * (for sticking to vertically-moving platforms in platformers for example). + * (for sticking to vertically-moving platforms in platformers for example). Use values + * IMMOVABLE, ALWAYS, HEAVIER or NEVER * @since 4.11.0 */ - public var collisionYDrag = CollisionDragType.NEVER; + public var collisionYDrag:CollisionDragType = NEVER; #if FLX_DEBUG /** From 2a7abfd7602a0a0725733a7f78760910a727b5df Mon Sep 17 00:00:00 2001 From: George FunBook Date: Mon, 17 Jan 2022 14:12:25 -0600 Subject: [PATCH 07/15] add back old collisonXDrag and deprecate --- flixel/FlxObject.hx | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/flixel/FlxObject.hx b/flixel/FlxObject.hx index 8cbee837b2..486899a297 100644 --- a/flixel/FlxObject.hx +++ b/flixel/FlxObject.hx @@ -644,6 +644,15 @@ class FlxObject extends FlxBasic */ public var allowCollisions(default, set):FlxDirectionFlags = ANY; + /** DEPRECATED + * Whether this sprite is dragged along with the horizontal movement of objects it collides with + * (makes sense for horizontally-moving platforms in platformers for example). + * + * Apart from having a weird typo, this has been deprecated for collisionXDrag, which allows more options. + */ + @:deprecated("Use `collisionXDrag`, instead. Note the corrected spelling: `collis(i)onXDrag") + public var collisonXDrag(get, set):Bool; + /** * Whether this sprite is dragged along with the horizontal movement of objects it collides with * (makes sense for horizontally-moving platforms in platformers for example). Use values @@ -1363,6 +1372,19 @@ class FlxObject extends FlxBasic return allowCollisions = Value; } + @:noCompletion + function get_collisonXDrag():Bool + { + return collisionXDrag == IMMOVABLE; + } + + @:noCompletion + function set_collisonXDrag(Value:Bool):Bool + { + collisionXDrag = Value ? IMMOVABLE : NEVER; + return Value; + } + #if FLX_DEBUG @:noCompletion function set_debugBoundingBoxColorSolid(color:FlxColor) From d7498dbfa1936fd0a9c92984d621f99d021ac4b1 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Thu, 21 Apr 2022 13:56:02 -0500 Subject: [PATCH 08/15] rename FLX_LEGACY_COLLISION to FLX_LEGACY_COLLISION_4 --- flixel/FlxObject.hx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flixel/FlxObject.hx b/flixel/FlxObject.hx index 486899a297..57e6f320e9 100644 --- a/flixel/FlxObject.hx +++ b/flixel/FlxObject.hx @@ -242,7 +242,7 @@ class FlxObject extends FlxBasic if (!obj1immovable && !obj2immovable) { - #if FLX_LEGACY_COLLISION + #if FLX_LEGACY_COLLISION_4 overlap *= 0.5; Object1.x = Object1.x - overlap; Object2.x += overlap; @@ -424,7 +424,7 @@ class FlxObject extends FlxBasic if (!obj1immovable && !obj2immovable) { - #if FLX_LEGACY_COLLISION + #if FLX_LEGACY_COLLISION_4 overlap *= 0.5; Object1.y = Object1.y - overlap; Object2.y += overlap; From d6aea7b9b7bebbce05ed53b9758e065264be6545 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Thu, 21 Apr 2022 13:56:41 -0500 Subject: [PATCH 09/15] remove unused code --- flixel/FlxObject.hx | 6 ------ 1 file changed, 6 deletions(-) diff --git a/flixel/FlxObject.hx b/flixel/FlxObject.hx index 57e6f320e9..d413cb2221 100644 --- a/flixel/FlxObject.hx +++ b/flixel/FlxObject.hx @@ -257,9 +257,6 @@ class FlxObject extends FlxBasic #else Object1.x -= overlap / 2; Object2.x += overlap / 2; - // this causes stacking issues - // Object1.x -= overlap * mass2 / massSum; - // Object2.x += overlap * mass1 / massSum; var newVel1 = (mass1 * vel1 + mass2 * vel2 + Object1.elasticity * mass2 * (vel2 - vel1)) / massSum; var newVel2 = (mass2 * vel2 + mass1 * vel1 + Object2.elasticity * mass1 * (vel1 - vel2)) / massSum; @@ -439,9 +436,6 @@ class FlxObject extends FlxBasic #else Object1.y -= overlap / 2; Object2.y += overlap / 2; - // this causes stacking issues - // Object1.y -= overlap * mass2 / massSum; - // Object2.y += overlap * mass1 / massSum; var newVel1 = (mass1 * vel1 + mass2 * vel2 + Object1.elasticity * mass2 * (vel2 - vel1)) / massSum; var newVel2 = (mass2 * vel2 + mass1 * vel1 + Object2.elasticity * mass1 * (vel1 - vel2)) / massSum; From 29961c7af95da9b30bd78a0269dd5af7ea39bf4b Mon Sep 17 00:00:00 2001 From: George FunBook Date: Thu, 21 Apr 2022 14:08:08 -0500 Subject: [PATCH 10/15] rename vars --- flixel/FlxObject.hx | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/flixel/FlxObject.hx b/flixel/FlxObject.hx index d413cb2221..f22436a2d3 100644 --- a/flixel/FlxObject.hx +++ b/flixel/FlxObject.hx @@ -209,9 +209,9 @@ class FlxObject extends FlxBasic public static function separateX(Object1:FlxObject, Object2:FlxObject):Bool { // can't separate two immovable objects - var obj1immovable:Bool = Object1.immovable; - var obj2immovable:Bool = Object2.immovable; - if (obj1immovable && obj2immovable) + var immovable1 = Object1.immovable; + var immovable2 = Object2.immovable; + if (immovable1 && immovable2) { return false; } @@ -239,8 +239,10 @@ class FlxObject extends FlxBasic var mass1 = Object1.mass; var mass2 = Object2.mass; var massSum = mass1 + mass2; + var elasticity1 = Object1.elasticity; + var elasticity2 = Object2.elasticity; - if (!obj1immovable && !obj2immovable) + if (!immovable1 && !immovable2) { #if FLX_LEGACY_COLLISION_4 overlap *= 0.5; @@ -252,27 +254,27 @@ class FlxObject extends FlxBasic var average = (newVel1 + newVel2) * 0.5; newVel1 -= average; newVel2 -= average; - Object1.velocity.x = average + newVel1 * Object1.elasticity; - Object2.velocity.x = average + newVel2 * Object2.elasticity; + Object1.velocity.x = average + newVel1 * elasticity1; + Object2.velocity.x = average + newVel2 * elasticity2; #else Object1.x -= overlap / 2; Object2.x += overlap / 2; - var newVel1 = (mass1 * vel1 + mass2 * vel2 + Object1.elasticity * mass2 * (vel2 - vel1)) / massSum; - var newVel2 = (mass2 * vel2 + mass1 * vel1 + Object2.elasticity * mass1 * (vel1 - vel2)) / massSum; + var newVel1 = (mass1 * vel1 + mass2 * vel2 + elasticity1 * mass2 * (vel2 - vel1)) / massSum; + var newVel2 = (mass2 * vel2 + mass1 * vel1 + elasticity2 * mass1 * (vel1 - vel2)) / massSum; Object1.velocity.x = newVel1; Object2.velocity.x = newVel2; #end } - else if (!obj1immovable) + else if (!immovable1) { Object1.x -= overlap; - Object1.velocity.x = vel2 - vel1 * Object1.elasticity; + Object1.velocity.x = vel2 - vel1 * elasticity1; } - else if (!obj2immovable) + else if (!immovable2) { Object2.x += overlap; - Object2.velocity.x = vel1 - vel2 * Object2.elasticity; + Object2.velocity.x = vel1 - vel2 * elasticity2; } // use collisionDrag properties to determine whether one object From 2bd24e7f257a7d51118716d13d3d598370fe0dc8 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Thu, 21 Apr 2022 14:17:18 -0500 Subject: [PATCH 11/15] rename more vars --- flixel/FlxObject.hx | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/flixel/FlxObject.hx b/flixel/FlxObject.hx index 3ccfc71e76..5b5e4f486d 100644 --- a/flixel/FlxObject.hx +++ b/flixel/FlxObject.hx @@ -390,9 +390,9 @@ class FlxObject extends FlxBasic public static function separateY(Object1:FlxObject, Object2:FlxObject):Bool { // can't separate two immovable objects - var obj1immovable:Bool = Object1.immovable; - var obj2immovable:Bool = Object2.immovable; - if (obj1immovable && obj2immovable) + var immovable1:Bool = Object1.immovable; + var immovable2:Bool = Object2.immovable; + if (immovable1 && immovable2) { return false; } @@ -420,8 +420,10 @@ class FlxObject extends FlxBasic var mass1 = Object1.mass; var mass2 = Object2.mass; var massSum = mass1 + mass2; + var elasticity1 = Object1.elasticity; + var elasticity2 = Object2.elasticity; - if (!obj1immovable && !obj2immovable) + if (!immovable1 && !immovable2) { #if FLX_LEGACY_COLLISION_4 overlap *= 0.5; @@ -433,27 +435,27 @@ class FlxObject extends FlxBasic var average = (newVel1 + newVel2) * 0.5; newVel1 -= average; newVel2 -= average; - Object1.velocity.y = average + newVel1 * Object1.elasticity; - Object2.velocity.y = average + newVel2 * Object2.elasticity; + Object1.velocity.y = average + newVel1 * elasticity1; + Object2.velocity.y = average + newVel2 * elasticity2; #else Object1.y -= overlap / 2; Object2.y += overlap / 2; - var newVel1 = (mass1 * vel1 + mass2 * vel2 + Object1.elasticity * mass2 * (vel2 - vel1)) / massSum; - var newVel2 = (mass2 * vel2 + mass1 * vel1 + Object2.elasticity * mass1 * (vel1 - vel2)) / massSum; + var newVel1 = (mass1 * vel1 + mass2 * vel2 + elasticity1 * mass2 * (vel2 - vel1)) / massSum; + var newVel2 = (mass2 * vel2 + mass1 * vel1 + elasticity2 * mass1 * (vel1 - vel2)) / massSum; Object1.velocity.y = newVel1; Object2.velocity.y = newVel2; #end } - else if (!obj1immovable) + else if (!immovable1) { Object1.y -= overlap; - Object1.velocity.y = vel2 - vel1 * Object1.elasticity; + Object1.velocity.y = vel2 - vel1 * elasticity1; } - else if (!obj2immovable) + else if (!immovable2) { Object2.y += overlap; - Object2.velocity.y = vel1 - vel2 * Object2.elasticity; + Object2.velocity.y = vel1 - vel2 * elasticity2; } // use collisionDrag properties to determine whether one object From 715ed2e1cd29dfc489864d8f902b4beb69c7c7f8 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Thu, 21 Apr 2022 15:30:01 -0500 Subject: [PATCH 12/15] add momentum var --- flixel/FlxObject.hx | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/flixel/FlxObject.hx b/flixel/FlxObject.hx index 5b5e4f486d..a7a8e7765a 100644 --- a/flixel/FlxObject.hx +++ b/flixel/FlxObject.hx @@ -260,8 +260,9 @@ class FlxObject extends FlxBasic Object1.x -= overlap / 2; Object2.x += overlap / 2; - var newVel1 = (mass1 * vel1 + mass2 * vel2 + elasticity1 * mass2 * (vel2 - vel1)) / massSum; - var newVel2 = (mass2 * vel2 + mass1 * vel1 + elasticity2 * mass1 * (vel1 - vel2)) / massSum; + var momentum = mass1 * vel1 + mass2 * vel2; + var newVel1 = (momentum + elasticity1 * mass2 * (vel2 - vel1)) / massSum; + var newVel2 = (momentum + elasticity2 * mass1 * (vel1 - vel2)) / massSum; Object1.velocity.x = newVel1; Object2.velocity.x = newVel2; #end @@ -441,8 +442,9 @@ class FlxObject extends FlxBasic Object1.y -= overlap / 2; Object2.y += overlap / 2; - var newVel1 = (mass1 * vel1 + mass2 * vel2 + elasticity1 * mass2 * (vel2 - vel1)) / massSum; - var newVel2 = (mass2 * vel2 + mass1 * vel1 + elasticity2 * mass1 * (vel1 - vel2)) / massSum; + var momentum = mass1 * vel1 + mass2 * vel2; + var newVel1 = (momentum + elasticity1 * mass2 * (vel2 - vel1)) / massSum; + var newVel2 = (momentum + elasticity2 * mass1 * (vel1 - vel2)) / massSum; Object1.velocity.y = newVel1; Object2.velocity.y = newVel2; #end From 9d1b11e2a92d9ececd8448d8bb53662a8105d1ad Mon Sep 17 00:00:00 2001 From: George FunBook Date: Thu, 21 Apr 2022 15:30:53 -0500 Subject: [PATCH 13/15] rename FLX_LEGACY_COLLISION_4 to FLX_4_LEGACY_COLLISION --- flixel/FlxObject.hx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flixel/FlxObject.hx b/flixel/FlxObject.hx index a7a8e7765a..f10a7deb48 100644 --- a/flixel/FlxObject.hx +++ b/flixel/FlxObject.hx @@ -244,7 +244,7 @@ class FlxObject extends FlxBasic if (!immovable1 && !immovable2) { - #if FLX_LEGACY_COLLISION_4 + #if FLX_4_LEGACY_COLLISION overlap *= 0.5; Object1.x = Object1.x - overlap; Object2.x += overlap; @@ -426,7 +426,7 @@ class FlxObject extends FlxBasic if (!immovable1 && !immovable2) { - #if FLX_LEGACY_COLLISION_4 + #if FLX_4_LEGACY_COLLISION overlap *= 0.5; Object1.y = Object1.y - overlap; Object2.y += overlap; From 9f502050f49ddcbdb8b9420eeaf012f0df3efe95 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Thu, 21 Apr 2022 16:17:03 -0500 Subject: [PATCH 14/15] rename more vars --- flixel/FlxObject.hx | 242 ++++++++++++++++++++++---------------------- 1 file changed, 121 insertions(+), 121 deletions(-) diff --git a/flixel/FlxObject.hx b/flixel/FlxObject.hx index f10a7deb48..4c84adf2e4 100644 --- a/flixel/FlxObject.hx +++ b/flixel/FlxObject.hx @@ -99,10 +99,10 @@ class FlxObject extends FlxBasic * @param Object2 Any other `FlxObject`. * @return Whether the objects in fact touched and were separated. */ - public static function separate(Object1:FlxObject, Object2:FlxObject):Bool + public static function separate(object1:FlxObject, object2:FlxObject):Bool { - var separatedX:Bool = separateX(Object1, Object2); - var separatedY:Bool = separateY(Object1, Object2); + var separatedX:Bool = separateX(object1, object2); + var separatedY:Bool = separateY(object1, object2); return separatedX || separatedY; } @@ -138,12 +138,12 @@ class FlxObject extends FlxBasic * greater than a certain maximum (linked to `SEPARATE_BIAS`). Default is `true`, handy for `separateX` code. */ @:noCompletion - static function computeOverlapX(Object1:FlxObject, Object2:FlxObject, checkMaxOverlap:Bool = true):Float + static function computeOverlapX(object1:FlxObject, object2:FlxObject, checkMaxOverlap:Bool = true):Float { var overlap:Float = 0; // First, get the two object deltas - var obj1delta:Float = Object1.x - Object1.last.x; - var obj2delta:Float = Object2.x - Object2.last.x; + var obj1delta:Float = object1.x - object1.last.x; + var obj2delta:Float = object2.x - object2.last.x; if (obj1delta != obj2delta) { @@ -151,10 +151,10 @@ class FlxObject extends FlxBasic var obj1deltaAbs:Float = (obj1delta > 0) ? obj1delta : -obj1delta; var obj2deltaAbs:Float = (obj2delta > 0) ? obj2delta : -obj2delta; - var obj1rect:FlxRect = _firstSeparateFlxRect.set(Object1.x - ((obj1delta > 0) ? obj1delta : 0), Object1.last.y, Object1.width + obj1deltaAbs, - Object1.height); - var obj2rect:FlxRect = _secondSeparateFlxRect.set(Object2.x - ((obj2delta > 0) ? obj2delta : 0), Object2.last.y, Object2.width + obj2deltaAbs, - Object2.height); + var obj1rect:FlxRect = _firstSeparateFlxRect.set(object1.x - ((obj1delta > 0) ? obj1delta : 0), object1.last.y, object1.width + obj1deltaAbs, + object1.height); + var obj2rect:FlxRect = _secondSeparateFlxRect.set(object2.x - ((obj2delta > 0) ? obj2delta : 0), object2.last.y, object2.width + obj2deltaAbs, + object2.height); if ((obj1rect.x + obj1rect.width > obj2rect.x) && (obj1rect.x < obj2rect.x + obj2rect.width) @@ -166,32 +166,32 @@ class FlxObject extends FlxBasic // If they did overlap (and can), figure out by how much and flip the corresponding flags if (obj1delta > obj2delta) { - overlap = Object1.x + Object1.width - Object2.x; + overlap = object1.x + object1.width - object2.x; if ((checkMaxOverlap && (overlap > maxOverlap)) - || ((Object1.allowCollisions & RIGHT) == 0) - || ((Object2.allowCollisions & LEFT) == 0)) + || ((object1.allowCollisions & RIGHT) == 0) + || ((object2.allowCollisions & LEFT) == 0)) { overlap = 0; } else { - Object1.touching |= RIGHT; - Object2.touching |= LEFT; + object1.touching |= RIGHT; + object2.touching |= LEFT; } } else if (obj1delta < obj2delta) { - overlap = Object1.x - Object2.width - Object2.x; + overlap = object1.x - object2.width - object2.x; if ((checkMaxOverlap && (-overlap > maxOverlap)) - || ((Object1.allowCollisions & LEFT) == 0) - || ((Object2.allowCollisions & RIGHT) == 0)) + || ((object1.allowCollisions & LEFT) == 0) + || ((object2.allowCollisions & RIGHT) == 0)) { overlap = 0; } else { - Object1.touching |= LEFT; - Object2.touching |= RIGHT; + object1.touching |= LEFT; + object2.touching |= RIGHT; } } } @@ -206,83 +206,83 @@ class FlxObject extends FlxBasic * @param Object2 Any other `FlxObject`. * @return Whether the objects in fact touched and were separated along the X axis. */ - public static function separateX(Object1:FlxObject, Object2:FlxObject):Bool + public static function separateX(object1:FlxObject, object2:FlxObject):Bool { // can't separate two immovable objects - var immovable1 = Object1.immovable; - var immovable2 = Object2.immovable; + var immovable1 = object1.immovable; + var immovable2 = object2.immovable; if (immovable1 && immovable2) { return false; } // If one of the objects is a tilemap, just pass it off. - if (Object1.flixelType == TILEMAP) + if (object1.flixelType == TILEMAP) { - var tilemap:FlxBaseTilemap = cast Object1; - return tilemap.overlapsWithCallback(Object2, separateX); + var tilemap:FlxBaseTilemap = cast object1; + return tilemap.overlapsWithCallback(object2, separateX); } - if (Object2.flixelType == TILEMAP) + if (object2.flixelType == TILEMAP) { - var tilemap:FlxBaseTilemap = cast Object2; - return tilemap.overlapsWithCallback(Object1, separateX, true); + var tilemap:FlxBaseTilemap = cast object2; + return tilemap.overlapsWithCallback(object1, separateX, true); } - var overlap:Float = computeOverlapX(Object1, Object2); + var overlap:Float = computeOverlapX(object1, object2); // Then adjust their positions and velocities accordingly (if there was any overlap) if (overlap != 0) { - var delta1 = Object1.x - Object1.last.x; - var delta2 = Object2.x - Object2.last.x; - var vel1 = Object1.velocity.x; - var vel2 = Object2.velocity.x; - var mass1 = Object1.mass; - var mass2 = Object2.mass; + var delta1 = object1.x - object1.last.x; + var delta2 = object2.x - object2.last.x; + var vel1 = object1.velocity.x; + var vel2 = object2.velocity.x; + var mass1 = object1.mass; + var mass2 = object2.mass; var massSum = mass1 + mass2; - var elasticity1 = Object1.elasticity; - var elasticity2 = Object2.elasticity; + var elasticity1 = object1.elasticity; + var elasticity2 = object2.elasticity; if (!immovable1 && !immovable2) { #if FLX_4_LEGACY_COLLISION overlap *= 0.5; - Object1.x = Object1.x - overlap; - Object2.x += overlap; + object1.x = object1.x - overlap; + object2.x += overlap; var newVel1 = Math.sqrt((vel2 * vel2 * mass2) / mass1) * ((vel2 > 0) ? 1 : -1); var newVel2 = Math.sqrt((vel1 * vel1 * mass1) / mass2) * ((vel1 > 0) ? 1 : -1); var average = (newVel1 + newVel2) * 0.5; newVel1 -= average; newVel2 -= average; - Object1.velocity.x = average + newVel1 * elasticity1; - Object2.velocity.x = average + newVel2 * elasticity2; + object1.velocity.x = average + newVel1 * elasticity1; + object2.velocity.x = average + newVel2 * elasticity2; #else - Object1.x -= overlap / 2; - Object2.x += overlap / 2; + object1.x -= overlap / 2; + object2.x += overlap / 2; var momentum = mass1 * vel1 + mass2 * vel2; var newVel1 = (momentum + elasticity1 * mass2 * (vel2 - vel1)) / massSum; var newVel2 = (momentum + elasticity2 * mass1 * (vel1 - vel2)) / massSum; - Object1.velocity.x = newVel1; - Object2.velocity.x = newVel2; + object1.velocity.x = newVel1; + object2.velocity.x = newVel2; #end } else if (!immovable1) { - Object1.x -= overlap; - Object1.velocity.x = vel2 - vel1 * elasticity1; + object1.x -= overlap; + object1.velocity.x = vel2 - vel1 * elasticity1; } else if (!immovable2) { - Object2.x += overlap; - Object2.velocity.x = vel1 - vel2 * elasticity2; + object2.x += overlap; + object2.velocity.x = vel1 - vel2 * elasticity2; } // use collisionDrag properties to determine whether one object - if (allowCollisionDrag(Object1.collisionYDrag, Object1, Object2) && delta1 > delta2) - Object1.y += Object2.y - Object2.last.y; - else if (allowCollisionDrag(Object2.collisionYDrag, Object2, Object1) && delta2 > delta1) - Object2.y += Object1.y - Object1.last.y; + if (allowCollisionDrag(object1.collisionYDrag, object1, object2) && delta1 > delta2) + object1.y += object2.y - object2.last.y; + else if (allowCollisionDrag(object2.collisionYDrag, object2, object1) && delta2 > delta1) + object2.y += object1.y - object1.last.y; return true; } @@ -297,21 +297,21 @@ class FlxObject extends FlxBasic * @param Object2 Any other `FlxObject`. * @return Whether the objects in fact touched along the X axis. */ - public static function updateTouchingFlagsX(Object1:FlxObject, Object2:FlxObject):Bool + public static function updateTouchingFlagsX(object1:FlxObject, object2:FlxObject):Bool { // If one of the objects is a tilemap, just pass it off. - if (Object1.flixelType == TILEMAP) + if (object1.flixelType == TILEMAP) { - var tilemap:FlxBaseTilemap = cast Object1; - return tilemap.overlapsWithCallback(Object2, updateTouchingFlagsX); + var tilemap:FlxBaseTilemap = cast object1; + return tilemap.overlapsWithCallback(object2, updateTouchingFlagsX); } - if (Object2.flixelType == TILEMAP) + if (object2.flixelType == TILEMAP) { - var tilemap:FlxBaseTilemap = cast Object2; - return tilemap.overlapsWithCallback(Object1, updateTouchingFlagsX, true); + var tilemap:FlxBaseTilemap = cast object2; + return tilemap.overlapsWithCallback(object1, updateTouchingFlagsX, true); } // Since we are not separating, always return any amount of overlap => false as last parameter - return computeOverlapX(Object1, Object2, false) != 0; + return computeOverlapX(object1, object2, false) != 0; } /** @@ -320,12 +320,12 @@ class FlxObject extends FlxBasic * greater than a certain maximum (linked to `SEPARATE_BIAS`). Default is `true`, handy for `separateY` code. */ @:noCompletion - static function computeOverlapY(Object1:FlxObject, Object2:FlxObject, checkMaxOverlap:Bool = true):Float + static function computeOverlapY(object1:FlxObject, object2:FlxObject, checkMaxOverlap:Bool = true):Float { var overlap:Float = 0; // First, get the two object deltas - var obj1delta:Float = Object1.y - Object1.last.y; - var obj2delta:Float = Object2.y - Object2.last.y; + var obj1delta:Float = object1.y - object1.last.y; + var obj2delta:Float = object2.y - object2.last.y; if (obj1delta != obj2delta) { @@ -333,10 +333,10 @@ class FlxObject extends FlxBasic var obj1deltaAbs:Float = (obj1delta > 0) ? obj1delta : -obj1delta; var obj2deltaAbs:Float = (obj2delta > 0) ? obj2delta : -obj2delta; - var obj1rect:FlxRect = _firstSeparateFlxRect.set(Object1.x, Object1.y - ((obj1delta > 0) ? obj1delta : 0), Object1.width, - Object1.height + obj1deltaAbs); - var obj2rect:FlxRect = _secondSeparateFlxRect.set(Object2.x, Object2.y - ((obj2delta > 0) ? obj2delta : 0), Object2.width, - Object2.height + obj2deltaAbs); + var obj1rect:FlxRect = _firstSeparateFlxRect.set(object1.x, object1.y - ((obj1delta > 0) ? obj1delta : 0), object1.width, + object1.height + obj1deltaAbs); + var obj2rect:FlxRect = _secondSeparateFlxRect.set(object2.x, object2.y - ((obj2delta > 0) ? obj2delta : 0), object2.width, + object2.height + obj2deltaAbs); if ((obj1rect.x + obj1rect.width > obj2rect.x) && (obj1rect.x < obj2rect.x + obj2rect.width) @@ -348,32 +348,32 @@ class FlxObject extends FlxBasic // If they did overlap (and can), figure out by how much and flip the corresponding flags if (obj1delta > obj2delta) { - overlap = Object1.y + Object1.height - Object2.y; + overlap = object1.y + object1.height - object2.y; if ((checkMaxOverlap && (overlap > maxOverlap)) - || ((Object1.allowCollisions & DOWN) == 0) - || ((Object2.allowCollisions & UP) == 0)) + || ((object1.allowCollisions & DOWN) == 0) + || ((object2.allowCollisions & UP) == 0)) { overlap = 0; } else { - Object1.touching |= DOWN; - Object2.touching |= UP; + object1.touching |= DOWN; + object2.touching |= UP; } } else if (obj1delta < obj2delta) { - overlap = Object1.y - Object2.height - Object2.y; + overlap = object1.y - object2.height - object2.y; if ((checkMaxOverlap && (-overlap > maxOverlap)) - || ((Object1.allowCollisions & UP) == 0) - || ((Object2.allowCollisions & DOWN) == 0)) + || ((object1.allowCollisions & UP) == 0) + || ((object2.allowCollisions & DOWN) == 0)) { overlap = 0; } else { - Object1.touching |= UP; - Object2.touching |= DOWN; + object1.touching |= UP; + object2.touching |= DOWN; } } } @@ -388,83 +388,83 @@ class FlxObject extends FlxBasic * @param Object2 Any other `FlxObject`. * @return Whether the objects in fact touched and were separated along the Y axis. */ - public static function separateY(Object1:FlxObject, Object2:FlxObject):Bool + public static function separateY(object1:FlxObject, object2:FlxObject):Bool { // can't separate two immovable objects - var immovable1:Bool = Object1.immovable; - var immovable2:Bool = Object2.immovable; + var immovable1:Bool = object1.immovable; + var immovable2:Bool = object2.immovable; if (immovable1 && immovable2) { return false; } // If one of the objects is a tilemap, just pass it off. - if (Object1.flixelType == TILEMAP) + if (object1.flixelType == TILEMAP) { - var tilemap:FlxBaseTilemap = cast Object1; - return tilemap.overlapsWithCallback(Object2, separateY); + var tilemap:FlxBaseTilemap = cast object1; + return tilemap.overlapsWithCallback(object2, separateY); } - if (Object2.flixelType == TILEMAP) + if (object2.flixelType == TILEMAP) { - var tilemap:FlxBaseTilemap = cast Object2; - return tilemap.overlapsWithCallback(Object1, separateY, true); + var tilemap:FlxBaseTilemap = cast object2; + return tilemap.overlapsWithCallback(object1, separateY, true); } - var overlap:Float = computeOverlapY(Object1, Object2); + var overlap:Float = computeOverlapY(object1, object2); // Then adjust their positions and velocities accordingly (if there was any overlap) if (overlap != 0) { - var delta1 = Object1.y - Object1.last.y; - var delta2 = Object2.y - Object2.last.y; - var vel1 = Object1.velocity.y; - var vel2 = Object2.velocity.y; - var mass1 = Object1.mass; - var mass2 = Object2.mass; + var delta1 = object1.y - object1.last.y; + var delta2 = object2.y - object2.last.y; + var vel1 = object1.velocity.y; + var vel2 = object2.velocity.y; + var mass1 = object1.mass; + var mass2 = object2.mass; var massSum = mass1 + mass2; - var elasticity1 = Object1.elasticity; - var elasticity2 = Object2.elasticity; + var elasticity1 = object1.elasticity; + var elasticity2 = object2.elasticity; if (!immovable1 && !immovable2) { #if FLX_4_LEGACY_COLLISION overlap *= 0.5; - Object1.y = Object1.y - overlap; - Object2.y += overlap; + object1.y = object1.y - overlap; + object2.y += overlap; var newVel1 = Math.sqrt((vel2 * vel2 * mass2) / mass1) * ((vel2 > 0) ? 1 : -1); var newVel2 = Math.sqrt((vel1 * vel1 * mass1) / mass2) * ((vel1 > 0) ? 1 : -1); var average = (newVel1 + newVel2) * 0.5; newVel1 -= average; newVel2 -= average; - Object1.velocity.y = average + newVel1 * elasticity1; - Object2.velocity.y = average + newVel2 * elasticity2; + object1.velocity.y = average + newVel1 * elasticity1; + object2.velocity.y = average + newVel2 * elasticity2; #else - Object1.y -= overlap / 2; - Object2.y += overlap / 2; + object1.y -= overlap / 2; + object2.y += overlap / 2; var momentum = mass1 * vel1 + mass2 * vel2; var newVel1 = (momentum + elasticity1 * mass2 * (vel2 - vel1)) / massSum; var newVel2 = (momentum + elasticity2 * mass1 * (vel1 - vel2)) / massSum; - Object1.velocity.y = newVel1; - Object2.velocity.y = newVel2; + object1.velocity.y = newVel1; + object2.velocity.y = newVel2; #end } else if (!immovable1) { - Object1.y -= overlap; - Object1.velocity.y = vel2 - vel1 * elasticity1; + object1.y -= overlap; + object1.velocity.y = vel2 - vel1 * elasticity1; } else if (!immovable2) { - Object2.y += overlap; - Object2.velocity.y = vel1 - vel2 * elasticity2; + object2.y += overlap; + object2.velocity.y = vel1 - vel2 * elasticity2; } // use collisionDrag properties to determine whether one object - if (allowCollisionDrag(Object1.collisionXDrag, Object1, Object2) && delta1 > delta2) - Object1.x += Object2.x - Object2.last.x; - else if (allowCollisionDrag(Object2.collisionXDrag, Object2, Object1) && delta2 > delta1) - Object2.x += Object1.x - Object1.last.x; + if (allowCollisionDrag(object1.collisionXDrag, object1, object2) && delta1 > delta2) + object1.x += object2.x - object2.last.x; + else if (allowCollisionDrag(object2.collisionXDrag, object2, object1) && delta2 > delta1) + object2.x += object1.x - object1.last.x; return true; } @@ -479,21 +479,21 @@ class FlxObject extends FlxBasic * @param Object2 Any other `FlxObject`. * @return Whether the objects in fact touched along the Y axis. */ - public static function updateTouchingFlagsY(Object1:FlxObject, Object2:FlxObject):Bool + public static function updateTouchingFlagsY(object1:FlxObject, object2:FlxObject):Bool { // If one of the objects is a tilemap, just pass it off. - if (Object1.flixelType == TILEMAP) + if (object1.flixelType == TILEMAP) { - var tilemap:FlxBaseTilemap = cast Object1; - return tilemap.overlapsWithCallback(Object2, updateTouchingFlagsY); + var tilemap:FlxBaseTilemap = cast object1; + return tilemap.overlapsWithCallback(object2, updateTouchingFlagsY); } - if (Object2.flixelType == TILEMAP) + if (object2.flixelType == TILEMAP) { - var tilemap:FlxBaseTilemap = cast Object2; - return tilemap.overlapsWithCallback(Object1, updateTouchingFlagsY, true); + var tilemap:FlxBaseTilemap = cast object2; + return tilemap.overlapsWithCallback(object1, updateTouchingFlagsY, true); } // Since we are not separating, always return any amount of overlap => false as last parameter - return computeOverlapY(Object1, Object2, false) != 0; + return computeOverlapY(object1, object2, false) != 0; } /** From 1562d3fd4f6b6346e91a1b8d7ada4cea5c48346d Mon Sep 17 00:00:00 2001 From: George FunBook Date: Thu, 21 Apr 2022 16:17:15 -0500 Subject: [PATCH 15/15] add to FlxDefines --- flixel/system/macros/FlxDefines.hx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/flixel/system/macros/FlxDefines.hx b/flixel/system/macros/FlxDefines.hx index 00662f29bd..a984a6ce4d 100644 --- a/flixel/system/macros/FlxDefines.hx +++ b/flixel/system/macros/FlxDefines.hx @@ -22,6 +22,8 @@ private enum UserDefines FLX_UNIT_TEST; /* additional rendering define */ FLX_RENDER_TRIANGLE; + /* Uses flixel 4.0 legacy collision */ + FLX_4_LEGACY_COLLISION; } /**