Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collision data shape ordering (second attempt) #45

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
sort out circle collision flip logic
  • Loading branch information
MondayHopscotch committed Jan 16, 2024
commit 04fec0d7182360e7f82d218a38273032af2c0727
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?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't articulate exactly what this test is doing, so a short descriptor might help future folks if they need to come in here for any reason.

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