Compare commits
3 Commits
1b97c989db
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| be36b310b4 | |||
| 8f43cf3a01 | |||
| 2efbfcc564 |
@@ -329,7 +329,7 @@ export function getBodyGeometry(seed: number) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function blockLookup(chunkMap, x, y, z) {
|
export function blockLookup(chunkMap, x, y, z) {
|
||||||
const chunki = Math.floor((x + CHUNKSIZE / 2) / CHUNKSIZE);
|
const chunki = Math.floor((x + CHUNKSIZE / 2) / CHUNKSIZE);
|
||||||
const chunkj = Math.floor((y + CHUNKSIZE / 2) / CHUNKSIZE);
|
const chunkj = Math.floor((y + CHUNKSIZE / 2) / CHUNKSIZE);
|
||||||
const chunkk = Math.floor((z + CHUNKSIZE / 2) / CHUNKSIZE);
|
const chunkk = Math.floor((z + CHUNKSIZE / 2) / CHUNKSIZE);
|
||||||
|
|||||||
@@ -122,8 +122,7 @@ export async function initWorldGl(gl: WebGLRenderingContext) {
|
|||||||
|
|
||||||
const drawObject = (objectParams) => {
|
const drawObject = (objectParams) => {
|
||||||
const {
|
const {
|
||||||
position,
|
tf,
|
||||||
orientation,
|
|
||||||
glBuffer,
|
glBuffer,
|
||||||
numVertices,
|
numVertices,
|
||||||
lightDirection,
|
lightDirection,
|
||||||
@@ -131,8 +130,7 @@ export async function initWorldGl(gl: WebGLRenderingContext) {
|
|||||||
specularColor,
|
specularColor,
|
||||||
} = objectParams;
|
} = objectParams;
|
||||||
|
|
||||||
gl.uniformMatrix4fv(modelLoc, false, new Float32Array(se3.product(
|
gl.uniformMatrix4fv(modelLoc, false, new Float32Array(tf));
|
||||||
se3.translation(...position), orientation)));
|
|
||||||
gl.uniform3fv(lightDirectionLoc, lightDirection);
|
gl.uniform3fv(lightDirectionLoc, lightDirection);
|
||||||
gl.uniform3fv(glowColorLoc, glowColor);
|
gl.uniform3fv(glowColorLoc, glowColor);
|
||||||
gl.uniform3fv(specularColorLoc, specularColor);
|
gl.uniform3fv(specularColorLoc, specularColor);
|
||||||
@@ -219,14 +217,12 @@ export function getOrbitDrawContext(gl: WebGLRenderingContext) {
|
|||||||
|
|
||||||
const drawObject = (objectParams) => {
|
const drawObject = (objectParams) => {
|
||||||
const {
|
const {
|
||||||
position,
|
tf,
|
||||||
orientation,
|
|
||||||
value,
|
value,
|
||||||
glBuffer,
|
glBuffer,
|
||||||
} = objectParams;
|
} = objectParams;
|
||||||
|
|
||||||
gl.uniformMatrix4fv(modelLoc, false, new Float32Array(se3.product(
|
gl.uniformMatrix4fv(modelLoc, false, new Float32Array(tf));
|
||||||
se3.translation(...position), orientation)));
|
|
||||||
|
|
||||||
gl.bindBuffer(gl.ARRAY_BUFFER, glBuffer);
|
gl.bindBuffer(gl.ARRAY_BUFFER, glBuffer);
|
||||||
|
|
||||||
@@ -244,6 +240,16 @@ export function getOrbitDrawContext(gl: WebGLRenderingContext) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getShipTf(context) {
|
||||||
|
if (context.landed) {
|
||||||
|
const body = context.landedBody;
|
||||||
|
const bodyTf = se3.product(se3.translation(...body.position), body.orientation);
|
||||||
|
return se3.product(bodyTf, context.shipTf);
|
||||||
|
}
|
||||||
|
|
||||||
|
return context.player.tf;
|
||||||
|
}
|
||||||
|
|
||||||
function getObjects(context, body, parentPosition = undefined) {
|
function getObjects(context, body, parentPosition = undefined) {
|
||||||
const objects = [];
|
const objects = [];
|
||||||
const {gl, glContext, player} = context;
|
const {gl, glContext, player} = context;
|
||||||
@@ -255,8 +261,7 @@ function getObjects(context, body, parentPosition = undefined) {
|
|||||||
|
|
||||||
objects.push({
|
objects.push({
|
||||||
geometry: body.glBuffer,
|
geometry: body.glBuffer,
|
||||||
orientation,
|
tf: se3.product(se3.translation(...position), orientation),
|
||||||
position,
|
|
||||||
glContext,
|
glContext,
|
||||||
glowColor,
|
glowColor,
|
||||||
specularColor,
|
specularColor,
|
||||||
@@ -265,23 +270,10 @@ function getObjects(context, body, parentPosition = undefined) {
|
|||||||
const orbitObject = makeOrbitObject(gl, context.orbitGlContext, body.orbit, parentPosition);
|
const orbitObject = makeOrbitObject(gl, context.orbitGlContext, body.orbit, parentPosition);
|
||||||
//objects.push(orbitObject);
|
//objects.push(orbitObject);
|
||||||
} else {
|
} else {
|
||||||
const tfs = [player.tf];
|
const shipTf = se3.product(getShipTf(context), se3.rotxyz(-Math.PI / 2, Math.PI / 2, Math.PI / 2));
|
||||||
if (context.landed) {
|
|
||||||
const body = context.landedBody;
|
|
||||||
const bodyTf = se3.product(se3.translation(...body.position), body.orientation);
|
|
||||||
tfs.splice(0, 0, bodyTf);
|
|
||||||
}
|
|
||||||
const tf = tfs.reduce(se3.product);
|
|
||||||
const shipOrientation = [
|
|
||||||
se3.rotationOnly(tf),
|
|
||||||
//se3.rotationOnly(context.camera.tf),
|
|
||||||
se3.rotxyz(-Math.PI / 2, Math.PI / 2, Math.PI / 2),
|
|
||||||
].reduce(se3.product);
|
|
||||||
const shipPos = se3.apply(tf, [0, 0, 0, 1]);
|
|
||||||
objects.push({
|
objects.push({
|
||||||
geometry: makeBufferFromFaces(gl, context.spaceship),
|
geometry: makeBufferFromFaces(gl, context.spaceship),
|
||||||
orientation: shipOrientation,
|
tf: shipTf,
|
||||||
position: shipPos,
|
|
||||||
glContext,
|
glContext,
|
||||||
specularColor: [0.8, 0.8, 0.8],
|
specularColor: [0.8, 0.8, 0.8],
|
||||||
});
|
});
|
||||||
@@ -320,10 +312,12 @@ export function draw(context) {
|
|||||||
|
|
||||||
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
|
const thirdPerson = context.landed ? se3.identity() : se3.translation(0, 1, 4);
|
||||||
|
|
||||||
const playerView = [
|
const playerView = [
|
||||||
player.tf, // player position & orientation
|
player.tf, // player position & orientation
|
||||||
camera.tf, // camera orientation relative to player
|
camera.tf, // camera orientation relative to player
|
||||||
se3.translation(0, 1, 4), // step back from the player
|
thirdPerson,
|
||||||
];
|
];
|
||||||
if (context.landed) {
|
if (context.landed) {
|
||||||
const body = context.landedBody;
|
const body = context.landedBody;
|
||||||
@@ -334,7 +328,7 @@ export function draw(context) {
|
|||||||
const viewMatrix = se3.inverse(playerView.reduce(se3.product));
|
const viewMatrix = se3.inverse(playerView.reduce(se3.product));
|
||||||
let lastGlContext;
|
let lastGlContext;
|
||||||
|
|
||||||
for (const {position, orientation, geometry, glContext, glowColor, specularColor} of objects) {
|
for (const {tf, geometry, glContext, glowColor, specularColor} of objects) {
|
||||||
if (glContext !== lastGlContext) {
|
if (glContext !== lastGlContext) {
|
||||||
glContext.setupScene({
|
glContext.setupScene({
|
||||||
projectionMatrix: projMatrix,
|
projectionMatrix: projMatrix,
|
||||||
@@ -344,11 +338,11 @@ export function draw(context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
lastGlContext = glContext;
|
lastGlContext = glContext;
|
||||||
|
const position = se3.apply(tf, [0, 0, 0, 1]);
|
||||||
const lightDirection = sunDirection(position);
|
const lightDirection = sunDirection(position);
|
||||||
|
|
||||||
glContext.drawObject({
|
glContext.drawObject({
|
||||||
position,
|
tf,
|
||||||
orientation,
|
|
||||||
glBuffer: geometry.glBuffer,
|
glBuffer: geometry.glBuffer,
|
||||||
numVertices: geometry.numVertices,
|
numVertices: geometry.numVertices,
|
||||||
lightDirection,
|
lightDirection,
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import * as linalg from './linalg';
|
|||||||
import { loadObjModel } from './obj';
|
import { loadObjModel } from './obj';
|
||||||
import * as se3 from 'wmc-common/se3';
|
import * as se3 from 'wmc-common/se3';
|
||||||
import { computeOrbit, findSoi, getCartesianState, updateBodyPhysics, isInSoi } from './orbit';
|
import { computeOrbit, findSoi, getCartesianState, updateBodyPhysics, isInSoi } from './orbit';
|
||||||
import { getBodyGeometry, castRay, BlockType } from './chunk';
|
import { getBodyGeometry, castRay, blockLookup, BlockType } from './chunk';
|
||||||
import { draw, getOrbitDrawContext, initWorldGl } from './draw';
|
import { draw, getOrbitDrawContext, initWorldGl } from './draw';
|
||||||
import * as quat from './quat';
|
import * as quat from './quat';
|
||||||
|
|
||||||
@@ -159,19 +159,34 @@ function initUiListeners(canvas: HTMLCanvasElement, context) {
|
|||||||
case 'KeyL':
|
case 'KeyL':
|
||||||
context.landing = !context.landing;
|
context.landing = !context.landing;
|
||||||
return false;
|
return false;
|
||||||
case 'Space':
|
case 'KeyP':
|
||||||
context.pause = !context.pause;
|
context.pause = !context.pause;
|
||||||
return false;
|
return false;
|
||||||
|
case 'Space':
|
||||||
|
if (!context.flying) {
|
||||||
|
jump(context);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
context.keys.delete(e.code);
|
context.keys.delete(e.code);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const moveListener = e => {
|
const moveListener = e => {
|
||||||
// context.camera.orientation[0] -= e.movementY / 500;
|
const cam = context.camera;
|
||||||
// context.camera.orientation[1] -= e.movementX / 500;
|
if (context.landed) {
|
||||||
context.camera.tf =[
|
cam.orientation[1] -= e.movementX / 500;
|
||||||
context.camera.tf,
|
cam.orientation[0] -= e.movementY / 500;
|
||||||
|
cam.tf = [
|
||||||
|
se3.roty(cam.orientation[1]),
|
||||||
|
se3.rotx(cam.orientation[0]),
|
||||||
|
].reduce(se3.product);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
cam.tf =[
|
||||||
|
cam.tf,
|
||||||
se3.roty(-e.movementX / 500),
|
se3.roty(-e.movementX / 500),
|
||||||
se3.rotx(-e.movementY / 500),
|
se3.rotx(-e.movementY / 500),
|
||||||
].reduce(se3.product);
|
].reduce(se3.product);
|
||||||
@@ -204,29 +219,84 @@ function initUiListeners(canvas: HTMLCanvasElement, context) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function jump(context) {
|
||||||
|
if (context.jumpAmount > 0) {
|
||||||
|
const amount = context.jumpForce;
|
||||||
|
context.jumpAmount -= 1;
|
||||||
|
|
||||||
|
const gravity = landedGravity(context);
|
||||||
|
const up = linalg.normalize(linalg.scale(gravity, -1));
|
||||||
|
context.player.velocity = linalg.scale(up, amount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function moveShip(context, forward, right) {
|
||||||
|
if (context.keys.has('ShiftLeft')) {
|
||||||
|
forward *= 10;
|
||||||
|
right *= 10;
|
||||||
|
}
|
||||||
|
const tf = se3.product(
|
||||||
|
se3.orientationOnly(context.player.tf),
|
||||||
|
context.camera.tf,
|
||||||
|
);
|
||||||
|
const dir = [right, 0, -forward, 10];
|
||||||
|
if (context.flying) {
|
||||||
|
context.player.tf = [
|
||||||
|
context.player.tf,
|
||||||
|
se3.translation(...dir),
|
||||||
|
].reduce(se3.product);
|
||||||
|
} else {
|
||||||
|
const vel = context.player.velocity;
|
||||||
|
const dv = linalg.scale(se3.apply(tf, dir), 1/dir[3]);
|
||||||
|
context.player.velocity = linalg.add(vel, dv);
|
||||||
|
delete context.orbit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function moveOnGround(context, forward, right) {
|
||||||
|
if (context.keys.has('ShiftLeft')) {
|
||||||
|
forward *= 2;
|
||||||
|
right *= 2;
|
||||||
|
}
|
||||||
|
const tf = se3.product(
|
||||||
|
se3.orientationOnly(context.player.tf),
|
||||||
|
context.camera.tf,
|
||||||
|
);
|
||||||
|
const dir = se3.apply(tf, [right, 0, -forward, 0]);
|
||||||
|
if (context.flying) {
|
||||||
|
context.player.tf = [
|
||||||
|
se3.translation(...dir),
|
||||||
|
context.player.tf,
|
||||||
|
].reduce(se3.product);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const dv = linalg.scale(dir, 10);
|
||||||
|
|
||||||
|
const maxSpeed = 8;
|
||||||
|
const airMovement = 0.08;
|
||||||
|
|
||||||
|
if (context.isOnGround) {
|
||||||
|
const up = upDirection(context);
|
||||||
|
const vertical = linalg.dot(dv, up);
|
||||||
|
const horizontal = linalg.diff(dv, linalg.scale(up, vertical));
|
||||||
|
context.player.velocity = horizontal;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// steering in the air
|
||||||
|
context.player.velocity = linalg.add(context.player.velocity, linalg.scale(dv, airMovement));
|
||||||
|
|
||||||
|
const curVel = linalg.norm(context.player.velocity);
|
||||||
|
|
||||||
|
if (curVel > maxSpeed) {
|
||||||
|
context.player.velocity = linalg.scale(context.player.velocity, maxSpeed / curVel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
function handleInput(context) {
|
function handleInput(context) {
|
||||||
const move = (forward: number, right: number) => {
|
const move = (f, r) => (context.landed ? moveOnGround : moveShip)(context, f, r);
|
||||||
if (context.keys.has('ShiftLeft')) {
|
|
||||||
forward *= 10;
|
|
||||||
right *= 10;
|
|
||||||
}
|
|
||||||
const tf = se3.product(
|
|
||||||
se3.orientationOnly(context.player.tf),
|
|
||||||
context.camera.tf,
|
|
||||||
);
|
|
||||||
const dir = [right, 0, -forward, 10];
|
|
||||||
if (context.flying) {
|
|
||||||
context.player.tf = [
|
|
||||||
context.player.tf,
|
|
||||||
se3.translation(...dir),
|
|
||||||
].reduce(se3.product);
|
|
||||||
} else {
|
|
||||||
const vel = context.player.velocity;
|
|
||||||
const dv = linalg.scale(se3.apply(tf, dir), 1/dir[3]);
|
|
||||||
context.player.velocity = linalg.add(vel, dv);
|
|
||||||
delete context.orbit;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const roll = (amount: number) => {
|
const roll = (amount: number) => {
|
||||||
if (context.keys.has('ShiftLeft')) {
|
if (context.keys.has('ShiftLeft')) {
|
||||||
@@ -302,7 +372,48 @@ function distanceToGround(body: Body, position: number[], direction: number[]) :
|
|||||||
return {distance, normal};
|
return {distance, normal};
|
||||||
}
|
}
|
||||||
|
|
||||||
function autoLand(context) {
|
function finishLanding(context, body) {
|
||||||
|
const p = context.player;
|
||||||
|
context.landed = true;
|
||||||
|
context.landedBody = body;
|
||||||
|
|
||||||
|
p.tf = [
|
||||||
|
se3.inverse(body.orientation),
|
||||||
|
se3.inverse(se3.translation(...body.position)),
|
||||||
|
p.tf,
|
||||||
|
].reduce(se3.product);
|
||||||
|
|
||||||
|
context.shipTf = p.tf;
|
||||||
|
p.tf = se3.product(p.tf, se3.translation(1, 1, 0));
|
||||||
|
p.velocity = [0, 0, 0];
|
||||||
|
p.position = se3.apply(p.tf, [0, 0, 0, 1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
function alignUp(playerTf, surfaceNormal) {
|
||||||
|
const zaxis = se3.apply(playerTf, [0, 0, 1, 0]);
|
||||||
|
const ny = surfaceNormal;
|
||||||
|
const nx = linalg.normalize(linalg.cross(ny, zaxis));
|
||||||
|
const nz = linalg.normalize(linalg.cross(nx, ny));
|
||||||
|
|
||||||
|
return se3.fromBases(nx, ny, nz);
|
||||||
|
}
|
||||||
|
|
||||||
|
function calculateLandingDv(verticalSpeed, altitude, dt) {
|
||||||
|
// pretty crude logic, could be improved
|
||||||
|
const maxThrust = 2;
|
||||||
|
const final = verticalSpeed**2 / (2*(altitude - 1.0));
|
||||||
|
|
||||||
|
if (verticalSpeed > 0 || final < maxThrust * 0.8) {
|
||||||
|
// can still accelerate forward
|
||||||
|
return -maxThrust * dt;
|
||||||
|
} else if (final < maxThrust) {
|
||||||
|
// coast
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return final;
|
||||||
|
}
|
||||||
|
|
||||||
|
function autoLand(context, dt) {
|
||||||
const p = context.player;
|
const p = context.player;
|
||||||
// 0. check prereqs:
|
// 0. check prereqs:
|
||||||
// - none
|
// - none
|
||||||
@@ -317,44 +428,32 @@ function autoLand(context) {
|
|||||||
const toBodyCenter = linalg.diff(body.position, p.position);
|
const toBodyCenter = linalg.diff(body.position, p.position);
|
||||||
const {distance: altitude, normal: surfaceNormal} = distanceToGround(body, p.position, linalg.normalize(toBodyCenter));
|
const {distance: altitude, normal: surfaceNormal} = distanceToGround(body, p.position, linalg.normalize(toBodyCenter));
|
||||||
|
|
||||||
// figure out tangential speed
|
const surfaceVelocity = linalg.add(body.velocity, linalg.cross(body.spin, linalg.scale(toBodyCenter, -1)));
|
||||||
const spinSpeed = linalg.add(body.velocity, linalg.cross(body.spin, linalg.scale(toBodyCenter, -1)));
|
const relativeVelocity = linalg.diff(p.velocity, surfaceVelocity);
|
||||||
const dv = linalg.diff(spinSpeed, p.velocity);
|
const upward = linalg.dot(relativeVelocity, surfaceNormal);
|
||||||
// remove vertical component
|
const vertical = linalg.scale(surfaceNormal, upward);
|
||||||
const vertical = linalg.scale(surfaceNormal, -linalg.dot(dv, surfaceNormal));
|
const horizontal = linalg.diff(relativeVelocity, vertical);
|
||||||
const smallDv = linalg.scale(linalg.add(dv, vertical), 0.01);
|
const smallDv = linalg.scale(horizontal, -0.01);
|
||||||
// const smallDv = linalg.scale(dv, 0.05);
|
|
||||||
p.velocity = linalg.add(p.velocity, smallDv);
|
p.velocity = linalg.add(p.velocity, smallDv);
|
||||||
|
|
||||||
// up is up
|
// up is up
|
||||||
const shipZ = se3.apply(p.tf, [0, 0, 1, 0]);
|
const currentOrientation = slerp(p.tf, alignUp(p.tf, surfaceNormal), kShipRotateSpeed);
|
||||||
const ny = surfaceNormal;
|
|
||||||
const nx = linalg.normalize(linalg.cross(ny, shipZ));
|
|
||||||
const nz = linalg.normalize(linalg.cross(nx, ny));
|
|
||||||
|
|
||||||
const targetOrientation = se3.fromBases(nx, ny, nz);
|
|
||||||
const currentOrientation = slerp(p.tf, targetOrientation, kShipRotateSpeed);
|
|
||||||
p.tf = se3.setOrientation(p.tf, currentOrientation);
|
p.tf = se3.setOrientation(p.tf, currentOrientation);
|
||||||
|
|
||||||
if (altitude < 1.5) {
|
if (altitude < 1.5) {
|
||||||
const upward = linalg.dot(linalg.diff(p.velocity, body.velocity), surfaceNormal);
|
|
||||||
if (upward < -2) {
|
if (upward < -2) {
|
||||||
|
// going too fast, bounce
|
||||||
p.velocity = linalg.add(p.velocity, linalg.scale(surfaceNormal, -2*upward));
|
p.velocity = linalg.add(p.velocity, linalg.scale(surfaceNormal, -2*upward));
|
||||||
context.landing = false;
|
context.landing = false;
|
||||||
} else if (upward < 0) {
|
} else if (upward < 0) {
|
||||||
context.landed = true;
|
finishLanding(context, body);
|
||||||
context.landedBody = body;
|
return;
|
||||||
|
|
||||||
p.tf = [
|
|
||||||
se3.inverse(body.orientation),
|
|
||||||
se3.inverse(se3.translation(...body.position)),
|
|
||||||
p.tf,
|
|
||||||
].reduce(se3.product);
|
|
||||||
p.velocity = [0, 0, 0];
|
|
||||||
p.position = se3.apply(p.tf, [0, 0, 0, 1]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// p.velocity = linalg.add(p.velocity, linalg.scale(surfaceNormal, dvup));
|
|
||||||
|
// accelerate towards the ground and then slow down to land
|
||||||
|
const dvup = calculateLandingDv(upward, altitude, dt);
|
||||||
|
p.velocity = linalg.add(p.velocity, linalg.scale(surfaceNormal, dvup));
|
||||||
}
|
}
|
||||||
|
|
||||||
function effectiveGravity(position: number[], rootBody: Body) : number[] {
|
function effectiveGravity(position: number[], rootBody: Body) : number[] {
|
||||||
@@ -381,6 +480,110 @@ function effectiveGravity(position: number[], rootBody: Body) : number[] {
|
|||||||
return acceleration;
|
return acceleration;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function upDirection(context) {
|
||||||
|
const gravity = landedGravity(context);
|
||||||
|
return linalg.normalize(linalg.scale(gravity, -1));
|
||||||
|
}
|
||||||
|
|
||||||
|
function landedGravity(context) {
|
||||||
|
// just clamp "down" to one of our six directions
|
||||||
|
const pos = context.player.position;
|
||||||
|
const altitude2 = pos[0]**2 + pos[1]**2 + pos[2]**2;
|
||||||
|
const g = context.landedBody.mass / altitude2;
|
||||||
|
if (pos[0] > 0 && Math.abs(pos[1]) < pos[0] && Math.abs(pos[2]) < pos[0]) {
|
||||||
|
return [-g, 0, 0];
|
||||||
|
}
|
||||||
|
if (pos[0] < 0 && Math.abs(pos[1]) < -pos[0] && Math.abs(pos[2]) < -pos[0]) {
|
||||||
|
return [+g, 0, 0];
|
||||||
|
}
|
||||||
|
if (pos[1] > 0 && Math.abs(pos[0]) < pos[1] && Math.abs(pos[2]) < pos[1]) {
|
||||||
|
return [0, -g, 0];
|
||||||
|
}
|
||||||
|
if (pos[1] < 0 && Math.abs(pos[0]) < -pos[1] && Math.abs(pos[2]) < -pos[1]) {
|
||||||
|
return [0, +g, 0];
|
||||||
|
}
|
||||||
|
if (pos[2] > 0 && Math.abs(pos[1]) < pos[2] && Math.abs(pos[1]) < pos[2]) {
|
||||||
|
return [0, 0, -g];
|
||||||
|
}
|
||||||
|
if (pos[2] < 0 && Math.abs(pos[1]) < -pos[2] && Math.abs(pos[1]) < -pos[2]) {
|
||||||
|
return [0, 0, +g];
|
||||||
|
}
|
||||||
|
// blarg
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkCollision(curPos, newPos, chunkMap, orientation) {
|
||||||
|
const upDir = se3.apply(orientation, [0, 1, 0, 0]);
|
||||||
|
// I guess Gaspard is about 1.7 m tall?
|
||||||
|
// he also has a 60x60 cm axis-aligned square section '^_^
|
||||||
|
// box is centered around the camera
|
||||||
|
const gaspardBB = [
|
||||||
|
[-0.3, 0.2, -0.3],
|
||||||
|
[-0.3, 0.2, 0.3],
|
||||||
|
[0.3, 0.2, -0.3],
|
||||||
|
[0.3, 0.2, 0.3],
|
||||||
|
|
||||||
|
[-0.3, -1.5, -0.3],
|
||||||
|
[-0.3, -1.5, 0.3],
|
||||||
|
[0.3, -1.5, -0.3],
|
||||||
|
[0.3, -1.5, 0.3],
|
||||||
|
].map(v => se3.apply(orientation, v.concat([0])));
|
||||||
|
|
||||||
|
let dp = linalg.diff(newPos, curPos);
|
||||||
|
let isOnGround = false;
|
||||||
|
|
||||||
|
for (let i = 0; i < 3; i++) {
|
||||||
|
const newGaspard = v => v.map((x, j) => i === j ? x + newPos[j] : x + curPos[j]);
|
||||||
|
for (const point of gaspardBB.map(newGaspard)) {
|
||||||
|
const block = blockLookup(chunkMap, ...point);
|
||||||
|
if (block.type !== BlockType.AIR) {
|
||||||
|
const dir = [...Array(3).keys()].map(j => j === i ? 1 : 0);
|
||||||
|
if (Math.abs(linalg.dot(dir, upDir)) > 0.5) {
|
||||||
|
isOnGround = true;
|
||||||
|
}
|
||||||
|
dp[i] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (let i = 0; i < 3; i++) {
|
||||||
|
const newGaspard = v => linalg.add(linalg.add(curPos, v), dp);
|
||||||
|
for (const point of gaspardBB.map(newGaspard)) {
|
||||||
|
const block = blockLookup(chunkMap, ...point);
|
||||||
|
if (block.type !== BlockType.AIR) {
|
||||||
|
dp[i] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
newPos: linalg.add(curPos, dp),
|
||||||
|
isOnGround,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateLandedPhysics(context, dt) {
|
||||||
|
const gravity = landedGravity(context);
|
||||||
|
context.player.velocity = linalg.add(context.player.velocity, linalg.scale(gravity, dt));
|
||||||
|
|
||||||
|
const oldPos = context.player.position;
|
||||||
|
const taregtPos = linalg.add(context.player.position, linalg.scale(context.player.velocity, dt));
|
||||||
|
|
||||||
|
// from wmc
|
||||||
|
const {isOnGround, newPos} = checkCollision(oldPos, taregtPos, context.landedBody.geometry.chunkMap, se3.orientationOnly(context.player.tf));
|
||||||
|
context.player.position = newPos;
|
||||||
|
context.player.velocity = newPos.map((p, i) => (p - oldPos[i]) / dt);
|
||||||
|
if (isOnGround) {
|
||||||
|
context.jumpAmount = 2;
|
||||||
|
context.player.velocity = context.player.velocity.map(v => v * 0.7);
|
||||||
|
}
|
||||||
|
context.isOnGround = isOnGround;
|
||||||
|
context.player.tf = se3.setPosition(context.player.tf, newPos);
|
||||||
|
|
||||||
|
// up is up
|
||||||
|
const p = context.player;
|
||||||
|
const currentOrientation = slerp(p.tf, alignUp(p.tf, upDirection(context)), 0.1);
|
||||||
|
p.tf = se3.setOrientation(p.tf, currentOrientation);
|
||||||
|
}
|
||||||
|
|
||||||
function updatePhysics(time: number, context) {
|
function updatePhysics(time: number, context) {
|
||||||
const {player} = context;
|
const {player} = context;
|
||||||
const dt = time - (context.lastTime || 0);
|
const dt = time - (context.lastTime || 0);
|
||||||
@@ -395,13 +598,13 @@ function updatePhysics(time: number, context) {
|
|||||||
const kShipRotateSpeed = 0.1;
|
const kShipRotateSpeed = 0.1;
|
||||||
|
|
||||||
if (context.landed) {
|
if (context.landed) {
|
||||||
return; // TODO: implement
|
return updateLandedPhysics(context, dt);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (context.landing) {
|
if (context.landing) {
|
||||||
autoLand(context);
|
autoLand(context, dt);
|
||||||
} else {
|
} else {
|
||||||
// allow acdjusting camera
|
// allow adjusting camera
|
||||||
const dr = slerp(se3.identity(), context.camera.tf, kShipRotateSpeed);
|
const dr = slerp(se3.identity(), context.camera.tf, kShipRotateSpeed);
|
||||||
player.tf = se3.product(player.tf, dr);
|
player.tf = se3.product(player.tf, dr);
|
||||||
context.camera.tf = se3.product(context.camera.tf, se3.inverse(dr));
|
context.camera.tf = se3.product(context.camera.tf, se3.inverse(dr));
|
||||||
@@ -479,7 +682,7 @@ async function main() {
|
|||||||
// TODO
|
// TODO
|
||||||
// [ ] loading bar
|
// [ ] loading bar
|
||||||
// [x] spaceship
|
// [x] spaceship
|
||||||
// [ ] landing
|
// [x] landing
|
||||||
// [ ] huge planets
|
// [ ] huge planets
|
||||||
// [x] lighting
|
// [x] lighting
|
||||||
// [x] better lighting
|
// [x] better lighting
|
||||||
|
|||||||
@@ -277,8 +277,7 @@ export function makeOrbitObject(gl: WebGLRenderingContext, glContext: any, orbit
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
geometry,
|
geometry,
|
||||||
orientation,
|
tf: se3.product(se3.translation(...position), orientation),
|
||||||
position,
|
|
||||||
glContext,
|
glContext,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user