Skip to content

Commit

Permalink
test: update raycast test
Browse files Browse the repository at this point in the history
  • Loading branch information
eonarheim committed Nov 2, 2024
1 parent 6755221 commit 2a6e2e6
Showing 1 changed file with 63 additions and 26 deletions.
89 changes: 63 additions & 26 deletions sandbox/tests/raycast/main.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,59 @@
var game = new ex.Engine({
width: 600,
height: 400,
displayMode: ex.DisplayMode.FitScreenAndFill
displayMode: ex.DisplayMode.FitScreenAndFill,
physics: {
spatialPartition: ex.SpatialPartitionStrategy.DynamicTree
}
});
var random = new ex.Random(1337);

// collides with everything but players
var playerGroup = ex.CollisionGroupManager.create('playerGroup');
var blockGroup = ex.CollisionGroupManager.create('blockGroup');
var notPlayers = ~playerGroup.category;
var notPlayers2 = playerGroup.invert();
// var playerGroup = ex.CollisionGroupManager.create('playerGroup');
// var blockGroup = ex.CollisionGroupManager.create('blockGroup');
// var notPlayers = ~playerGroup.category;
// var notPlayers2 = playerGroup.invert();

var playerColliders = new ex.CollisionGroup('playerCollider', 0b0001, ~0b1100);
var bulletColliders = new ex.CollisionGroup('bulletColliders', 0b0010, ~0b1100);
var wallColliders = new ex.CollisionGroup('wallColliders', 0b0100, ~0b1011);
var enemyColliders = new ex.CollisionGroup('enemyColliders', 0b1000, ~0b0111);

var wall1 = new ex.Actor({
name: 'walls',
pos: ex.vec(0, 0),
anchor: ex.vec(0, 0),
width: game.screen.resolution.width,
height: 20,
color: ex.Color.Black,
collisionGroup: wallColliders
});
game.currentScene.add(wall1);

var wall2 = new ex.Actor({
name: 'walls',
pos: ex.vec(game.screen.resolution.width, 0),
anchor: ex.vec(1, 0),
width: 20,
height: game.screen.resolution.height,
color: ex.Color.Black,
collisionGroup: wallColliders
});
game.currentScene.add(wall2);

var player = new ex.Actor({
name: 'player',
pos: ex.vec(70, 320),
width: 40,
height: 40,
collisionGroup: playerGroup,
collisionGroup: playerColliders,
color: ex.Color.Red,
z: 10
});
var playerDir = ex.Vector.Right;
var playerSightDistance = 100;
var playerSightDistance = 2000;
var playerSpeed = 100;
var rotationAmount = Math.PI / 32;
var rotationAmount = Math.PI / 1024;
player.onPostUpdate = (engine) => {
player.vel = ex.Vector.Zero;
const keyboard = engine.input.keyboard;
Expand All @@ -39,39 +70,45 @@ player.onPostUpdate = (engine) => {
}

// raycast
var ray = new ex.Ray(player.pos, playerDir);
var hits = engine.currentScene.physics.rayCast(new ex.Ray(player.pos, playerDir), {
maxDistance: playerSightDistance,
// collisionMask: notPlayers,
collisionGroup: notPlayers2,
searchAllColliders: false
searchAllColliders: true,
collisionMask: 0b1100,
ignoreCollisionGroupAll: true,
maxDistance: 2000
});
ex.Debug.drawRay(ray, { distance: playerSightDistance, color: ex.Color.Red });

for (let hit of hits) {
ex.Debug.drawPoint(hit.point, {
size: 10,
color: ex.Color.Red
});
const hitActor = hit.collider.owner as ex.Actor;
hitActor.graphics.use(
new ex.Rectangle({
color: ex.Color.Violet,
width: 20,
height: 20
})
);
if (hitActor.name === 'circles') {
hitActor.graphics.use(
new ex.Circle({
color: ex.Color.Violet,
radius: 10
})
);
}
}
};
player.graphics.onPostDraw = (ctx) => {
ctx.drawLine(ex.Vector.Zero, ex.Vector.Right.scale(playerSightDistance), ex.Color.Red, 2);
};
// player.graphics.onPostDraw = (ctx) => {
// ctx.drawLine(ex.Vector.Zero, ex.Vector.Right.scale(playerSightDistance), ex.Color.Red, 2);
// };
game.currentScene.add(player);

// environment

for (let i = 0; i < 10; i++) {
var block = new ex.Actor({
name: 'circles',
pos: ex.vec(random.floating(0, 600), random.floating(0, 400)),
width: 20,
height: 20,
radius: 10,
color: ex.Color.Black,
collisionType: ex.CollisionType.Fixed,
collisionGroup: blockGroup
collisionGroup: enemyColliders
});
game.currentScene.add(block);
}
Expand Down

0 comments on commit 2a6e2e6

Please sign in to comment.