Skip to content

Commit

Permalink
feat: add jump to player
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenrui committed Aug 29, 2022
1 parent 15251f2 commit 19ac83e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 17 deletions.
5 changes: 5 additions & 0 deletions src/game/Physics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export class Physics {
this.world.defaultContactMaterial.contactEquationStiffness = 1e7;
this.world.defaultContactMaterial.contactEquationRelaxation = 4;

const playerDefaultContactMaterial = new CANNON.ContactMaterial(this.materials.default, this.materials.player, {
friction: 0.0,
restitution: 0.3,
});
this.world.addContactMaterial(playerDefaultContactMaterial);
const playerGroundContactMaterial = new CANNON.ContactMaterial(this.materials.ground, this.materials.player, {
friction: 0.0,
restitution: 0.3,
Expand Down
4 changes: 4 additions & 0 deletions src/game/assets/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ export const BLOCKS_ASSETS: IBlockAssetGroup = {
},
leaves_oak: {
type: 'leaves',
body: {
type: CANNON.Body.STATIC,
mass: 1,
},
colorFilter: true,
transparent: true,
assets: {
Expand Down
5 changes: 3 additions & 2 deletions src/game/core/entities/Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import { BLOCKS_ASSETS } from '@game/assets/blocks';
import { IBiomes } from '@lib/types/biomes';
import { IBlockTypes } from '@lib/types/blocks';
import { BIOMES } from '@lib/constants/biomes';
import { Physics } from '@game/Physics';

const TOP_LAYER = 3;

export class Block {
static geometry = new THREE.BoxGeometry(1, 1, 1);
static shape = new CANNON.Box(new CANNON.Vec3(0.5, 0.5, 0.5));
static mass = Physics.density * this.shape.volume();
static mass = 1;
static materials: { [type: string]: THREE.MeshBasicMaterial | THREE.MeshBasicMaterial[] } = {};
static utilMaterials: { [type: string]: THREE.MeshBasicMaterial } = {};

Expand Down Expand Up @@ -55,6 +54,8 @@ export class Block {
shape: Block.shape,
});

this.body.collisionResponse = false;
this.body.fixedRotation = true;
this.body.angularDamping = 1;
}

Expand Down
7 changes: 4 additions & 3 deletions src/game/core/entities/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Physics } from '@game/Physics';
const DEFAULT_STATE = {
color: 'blue' as unknown as THREE.Color,
direction: new THREE.Vector3(),
mass: 100,
mass: 1,
speed: 500,
velocity: new THREE.Vector3(),
position: {
Expand Down Expand Up @@ -57,15 +57,16 @@ export class Player extends PlayerActions {

const shape = new CANNON.Box(new CANNON.Vec3(0.5, 0.5, 0.5));
this.body = new CANNON.Body({
mass: Physics.density * shape.volume(),
mass: this.state.mass,
position: new CANNON.Vec3(0, this.state.position.default.y, 0),
shape,
});
this.body.fixedRotation = true;
this.body.angularDamping = 1;
this.physics?.world.addBody(this.body);

if (!this.experience.debug)
this.scene.add(this.mesh);
// this.scene.add(this.mesh);

this.$setControls();
}
Expand Down
2 changes: 1 addition & 1 deletion src/game/core/entities/Terrain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ export class Terrain {

if (body) {
block.setBody(x, y, z);
this.objects.blocks[`${x}_${y}_${z}`] = block;
}

this.group.add(block.mesh);
this.objects.blocks[`${x}_${y}_${z}`] = block;

return block;
}
Expand Down
39 changes: 28 additions & 11 deletions src/game/core/entities/player/PlayerActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,12 @@ export class PlayerActions {
playerCamera!: PlayerCamera;
selector!: PlayerSelector;
raycaster = {
front: new THREE.Raycaster(),
back: new THREE.Raycaster(),
left: new THREE.Raycaster(),
right: new THREE.Raycaster(),
bottom: new THREE.Raycaster(),
};

$setControls() {
this.raycaster.front.far = 1;
this.raycaster.back.far = 1;
this.raycaster.left.far = 1;
this.raycaster.right.far = 1;

this.raycaster.bottom.far = 1;

document.addEventListener('pointerdown', (evt) => {
if (!this.playerCamera.controls.isLocked || !this.experience.world) return;
const { x, y, z } = this.selector.position;
Expand Down Expand Up @@ -63,6 +57,9 @@ export class PlayerActions {
this.state.moving.left = true;
} else if (evt.code === 'KeyD') {
this.state.moving.right = true;
} else if (evt.code === 'Space') {
if (!this.state.jumping) velocity.y += 5;
this.state.jumping = true;
}
});

Expand All @@ -89,12 +86,18 @@ export class PlayerActions {
// Set rotation of body
this.playerCamera.controls.getObject().getWorldQuaternion(quaternion);
this.body.quaternion.set(0, quaternion.y, 0, quaternion.w);

this.raycaster.bottom.ray.origin = this.mesh.position;
this.raycaster.bottom.ray.direction.set(0, -1, 0);
const intersections = this.raycaster.bottom.intersectObjects( terrain.group.children, false);

// Set direction
this.state.direction.z = Number(this.state.moving.forward) - Number(this.state.moving.backward);
this.state.direction.x = Number(this.state.moving.left) - Number(this.state.moving.right);
this.state.direction.normalize(); // this ensures consistent movements in all directions

velocity.y -= 9.8 * this.body.mass * delta;

// Set velocity based on direction
if (this.state.moving.forward || this.state.moving.backward) {
this.state.velocity.z = -this.state.direction.z * this.state.speed * delta;
Expand All @@ -110,9 +113,23 @@ export class PlayerActions {
velocity.x = 0;
}

this.body.quaternion.vmult(velocity, this.body.velocity);
this.body.position.y = 1;
if (this.state.jumping) {
this.mesh.position.y += velocity.y * delta;
}

const onObject = !!intersections[0] && this.mesh.position.y - intersections[0].object.position.y <= 1;

if (onObject) {
velocity.y = 0;
this.mesh.position.y = intersections[0].object.position.y + 1;
this.state.jumping = false;
} else {
this.mesh.position.y += velocity.y * delta;
}

this.body.quaternion.vmult(velocity, this.body.velocity);
this.body.position.y = this.mesh.position.y;

this.mesh.position.copy(this.body.position as unknown as THREE.Vector3);
this.mesh.quaternion.copy(this.body.quaternion as unknown as THREE.Quaternion);
}
Expand Down

0 comments on commit 19ac83e

Please sign in to comment.
  NODES
COMMUNITY 1
Note 1
Project 3
USERS 1