Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | import { createRigidBody } from './elements';
import world from './world';
import {
TYPES,
DEFAULT_RIGIDBODY_STATE
} from '../constants';
export const addPlayer = (data) => {
const { uuid, width, height, position, quaternion, mass, friction } = data;
const capsule = new Ammo.btCapsuleShape(width, height);
const body = createRigidBody(capsule, { uuid, position, quaternion, mass, friction });
// disabliing rotation for collisions
body.setAngularFactor(0);
world.addRigidBody(body);
world.addElement({ uuid, body, type: TYPES.PLAYER, state: DEFAULT_RIGIDBODY_STATE });
};
export const handlePlayerUpdate = ({ body, uuid, state = DEFAULT_RIGIDBODY_STATE }, dt) => {
const { movement, direction, cameraDirection, quaternion, position } = state;
const MAX_SPEED = 1;
const walkVelocity = .1;
const motionState = body.getMotionState();
if (motionState) {
const transform = new Ammo.btTransform();
motionState.getWorldTransform(transform);
const linearVelocity = body.getLinearVelocity();
const speed = linearVelocity.length();
const forwardDir = transform.getBasis().getRow(2);
forwardDir.normalize();
const walkDirection = new Ammo.btVector3(0.0, 0.0, 0.0);
const walkSpeed = walkVelocity * dt;
if (movement.forward) {
walkDirection.setX( walkDirection.x() + forwardDir.x());
//walkDirection.setY( walkDirection.y() + forwardDir.y());
walkDirection.setZ( walkDirection.z() + forwardDir.z());
}
if (movement.backwards) {
walkDirection.setX( walkDirection.x() - forwardDir.x());
//walkDirection.setY( walkDirection.y() - forwardDir.y());
walkDirection.setZ( walkDirection.z() - forwardDir.z());
}
if (!movement.forward && !movement.backwards) {
linearVelocity.setX(linearVelocity.x() * 0.2);
linearVelocity.setZ(linearVelocity.z() * 0.2);
} else if (speed < MAX_SPEED) {
linearVelocity.setX(linearVelocity.x() + cameraDirection.x * walkSpeed);
linearVelocity.setZ(linearVelocity.z() + cameraDirection.z * walkSpeed);
}
body.setLinearVelocity(linearVelocity);
body.getMotionState().setWorldTransform(transform);
body.setCenterOfMassTransform(transform);
let origin = transform.getOrigin();
let rotation = transform.getRotation();
dispatcher.sendBodyUpdate(uuid, origin, rotation, dt);
}
}; |