Skip to content

Commit

Permalink
simplified example of shape order issue
Browse files Browse the repository at this point in the history
  • Loading branch information
MondayHopscotch committed Jan 12, 2024
1 parent 734ee76 commit 99beaab
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
2 changes: 1 addition & 1 deletion sample/Main.hx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Main extends BaseApp {

// Set up our Sample States
sample_states = [
PolygonState, StackingState, MultiShapeState, ShapesState, GroupsState, StaticState, LinecastState, Linecast2State, TileMapState, TileMapState2,
ListenerState, PolygonState, StackingState, MultiShapeState, ShapesState, GroupsState, StaticState, LinecastState, Linecast2State, TileMapState, TileMapState2,
BezierState, VerletState
];
index = 0;
Expand Down
81 changes: 81 additions & 0 deletions sample/state/ListenerState.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package state;

import echo.data.Options.ListenerOptions;
import echo.Material;
import echo.Body;
import echo.World;
import util.Random;

class ListenerState extends BaseState {

override public function enter(world:World) {
Main.instance.state_text.text = "Sample: Collision Listener";

// Create a material for all the shapes to share
var material:Material = {elasticity: 0.7};

var bodyA = new Body({
x: Random.range(60, world.width - 60),
y: Random.range(0, world.height / 2),
rotation: Random.range(0, 360),
material: material,
shapes: [
{
type: POLYGON,
radius: Random.range(16, 32),
width: Random.range(16, 48),
height: Random.range(16, 48),
sides: Random.range_int(3, 8),
offset_y: -10,
},
{
type: POLYGON,
radius: Random.range(16, 32),
width: Random.range(16, 48),
height: Random.range(16, 48),
sides: Random.range_int(3, 8),
offset_y: 10,
}
]
});
world.add(bodyA);

// 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
var bodyB = new Body({
mass: STATIC,
x: world.width / 5,
y: world.height - 40,
material: material,
rotation: 5,
shape: {
type: RECT,
width: world.width,
height: 20
}
});
world.add(bodyB);

var dbgOpts:ListenerOptions = {
enter: (a, b, data) -> {
trace('bodyA == listener `a`: ${bodyA == a}');
trace('bodyA shape == data `shape a`: ${bodyA.shape == data[0].sa}');
trace('bodyB == listener `b`: ${bodyB == b}');
trace('bodyB shape == data `shape b`: ${bodyB.shape == data[0].sb}');
},
};

// Create a listener for collisions between the Physics Bodies
world.listen(bodyA, bodyB, dbgOpts);
}

override function step(world:World, dt:Float) {
// Reset any off-screen Bodies
world.for_each((member) -> {
if (offscreen(member, world)) {
member.velocity.set(0, 0);
member.set_position(Random.range(0, world.width), 0);
}
});
}
}

0 comments on commit 99beaab

Please sign in to comment.