Game Development Reference
In-Depth Information
For our shooter game, we'll use voxel collision and a little bit of manual intersection.
Unfortunately, all the physics libraries are large, so we don't have space to cover
their APIs here. However, Cannon.js and Physi.js have examples specifically for
use with Three.js available from their project pages.
Voxel collision
If we try to walk around our world now, we'll just fall through the floor. Let's create
a function to check for collision between the player and the voxel world:
function checkPlayerCollision(player) {
player.collideFloor(floor.position.y);
var cell = mapCellFromPosition(player.position);
switch (cell.char) {
case ' ':
case 'S':
break;
case 'X':
moveOutside(cell, player);
break;
}
}
Our collideFloor method keeps the player above the floor's y position. Then, the
mapCellFromPosition method looks up the map cell from the player's position to
determine whether the player is in a wall or open space. If the player is colliding
with a wall, the moveOutside() method moves the player outside of it by shifting
the player toward the closest cell. The cell-from-position lookup is just the reverse of
what we used to place each voxel originally:
var XOFFSET = (map.length+1) * 0.5 * HORIZONTAL_UNIT,
ZOFFSET = (map[0].length+1) * 0.5 * HORIZONTAL_UNIT,
col = Math.floor((position.x+XOFFSET) / HORIZONTAL_UNIT) - 1,
row = Math.floor((position.z+ZOFFSET) / HORIZONTAL_UNIT) - 1,
char = map[mapRow].charAt(mapCol);
Bots
Now that we've got the player working, it's time to add enemy bots. Enemies can
be Player is, just as the user is, so apart from initializing them, the main thing we
need to add is autonomous behavior. We don't have the space here to go in depth
on artificial intelligence strategies, so we'll just set each bot's moveDirection flags
randomly every once in awhile:
bot.rotation.y = Math.random() * Math.PI * 2;
bot.moveDirection.FORWARD = Math.random() < 0.8;
 
Search WWH ::




Custom Search