Skip to content

Commit

Permalink
sort out circle collision flip logic
Browse files Browse the repository at this point in the history
  • Loading branch information
MondayHopscotch committed Jan 16, 2024
1 parent bdf99d8 commit 04fec0d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 28 deletions.
2 changes: 1 addition & 1 deletion echo/shape/Circle.hx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class Circle extends Shape implements Poolable {
return false;
}

override inline function collides(s:Shape):Null<CollisionData> return s.collide_circle(this);
override inline function collides(s:Shape):Null<CollisionData> return s.collide_circle(this, true);

override inline function collide_rect(r:Rect, flip:Bool = false):Null<CollisionData> return r.rect_and_circle(this, !flip);

Expand Down
10 changes: 6 additions & 4 deletions echo/util/SAT.hx
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,8 @@ class SAT {
}

// Vector from A to B
var nx = flip ? c.x - r.x : r.x - c.x;
var ny = flip ? c.y - r.y : r.y - c.y;
var nx = flip ? r.x - c.x : c.x - r.x;
var ny = flip ? r.y - c.y : c.y - r.y;
// Closest point on A to center of B
var cx = nx;
var cy = ny;
Expand Down Expand Up @@ -506,7 +506,7 @@ class SAT {
test1 = min1 - max2;
test2 = min2 - max1;

// Preform another test
// Preform another test // TODO: What is this test doing, exactly?
if (test1 > 0 || test2 > 0) {
col.put();
return null;
Expand All @@ -529,7 +529,7 @@ class SAT {

col.overlap = Math.abs(col.overlap);

if (!flip) {
if (flip) {
col.normal.negate();
}

Expand Down Expand Up @@ -590,6 +590,8 @@ class SAT {
col.sa = flip ? polygon2 : polygon1;
col.sb = flip ? polygon1 : polygon2;

// collision normal is calculated as resolution for poly2, so we need to
// negate the normal if we are not flipping the collision check.
if (!flip) {
col.normal.negate();
}
Expand Down
34 changes: 14 additions & 20 deletions sample/state/OverlappingSpawnState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,25 @@ class OverlappingSpawnState extends BaseState {
rotation: 0,
material: material,
shape: {
type: POLYGON,
type: CIRCLE,
radius: 16,
width: 16,
height: 16,
sides: 5,
offset_y: 0,
}
});
world.add(body);

body = new Body({
x: 200,
y: 53,
rotation: 0,
material: material,
shape: {
type: POLYGON,
radius: 10,
width: 10,
height: 10,
sides: 5,
offset_y: 0,
}
});
world.add(body);
// body = new Body({
// x: 200,
// y: 53,
// rotation: 0,
// material: material,
// shape: {
// type: CIRCLE,
// radius: 12,
// offset_y: 0,
// }
// });
// world.add(body);

// Add a Physics body at the bottom of the screen for the other Physics Bodies to stack on top of
// This body has a mass of 0, so it acts as an immovable object
Expand All @@ -53,7 +47,7 @@ class OverlappingSpawnState extends BaseState {
x: world.width / 5,
y: world.height - 40,
material: material,
rotation: 5,
// rotation: 5,
shape: {
type: RECT,
width: world.width,
Expand Down
5 changes: 2 additions & 3 deletions sample/state/StaticState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ class StaticState extends BaseState {
y: (world.height * 0.5) * Math.sin(i) + world.height * 0.5,
material: static_material,
shape: {
// type: CIRCLE,
// radius: Random.range(2, 4),
type: RECT,
type: CIRCLE,
radius: Random.range(2, 4),
width: Random.range(2, 4),
height: Random.range(2, 4)
}
Expand Down

0 comments on commit 04fec0d

Please sign in to comment.