Skip to content

Commit

Permalink
Merge pull request #25 from BarthPaleologue/WIP
Browse files Browse the repository at this point in the history
1.4.2 - Spacestation ground work + orbital motion fixes
  • Loading branch information
BarthPaleologue authored Feb 12, 2024
2 parents 5ece24f + be4d2a9 commit 504ae66
Show file tree
Hide file tree
Showing 31 changed files with 400 additions and 202 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^4.15.1"
},
"version": "1.4.1",
"version": "1.4.2",
"description": "CosmosJourneyer",
"name": "cosmos-journeyer",
"scripts": {
Expand Down
Binary file modified src/asset/spacestation/shipcarrier.glb
Binary file not shown.
18 changes: 16 additions & 2 deletions src/ts/alphaTestis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import { StarModel } from "./stellarObjects/star/starModel";
import { RingsUniforms } from "./postProcesses/rings/ringsUniform";
import { getMoonSeed } from "./planets/common";
import { SystemSeed } from "./utils/systemSeed";
import { SpaceStation } from "./spacestation/spaceStation";
import { PhysicsViewer } from "@babylonjs/core/Debug/physicsViewer";

Check warning on line 38 in src/ts/alphaTestis.ts

View workflow job for this annotation

GitHub Actions / build (16)

'PhysicsViewer' is defined but never used

Check warning on line 38 in src/ts/alphaTestis.ts

View workflow job for this annotation

GitHub Actions / build (18)

'PhysicsViewer' is defined but never used

const engine = await CosmosJourneyer.CreateAsync();

Expand Down Expand Up @@ -80,8 +82,13 @@ const planet = StarSystemHelper.makeTelluricPlanet(starSystem, planetModel);
planet.model.ringsUniforms = new RingsUniforms(planet.model.rng);
planet.postProcesses.push(PostProcessType.RING);

//const spacestation = new SpaceStation(starSystemView.scene, planet);
//starSystemView.getStarSystem().addSpaceStation(spacestation);
const spacestation = new SpaceStation(starSystemView.scene, planet);
starSystemView.getStarSystem().addSpaceStation(spacestation);

//physicsViewer.showBody(spacestation.aggregate.body);
/*for(const landingpad of spacestation.landingPads) {
physicsViewer.showBody(landingpad.aggregate.body);
}*/

const moonModel = new TelluricPlanetModel(getMoonSeed(planetModel, 0), planetModel);
moonModel.physicalProperties.mass = 2;
Expand Down Expand Up @@ -160,6 +167,13 @@ if (aresAtmosphere) {
document.addEventListener("keydown", (e) => {
if (engine.isPaused()) return;

if(e.key === "o") {
const landingPad = spacestation.handleDockingRequest();
if(landingPad !== null && starSystemView.scene.getActiveController() === spaceshipController) {
spaceshipController.spaceship.engageLandingOnPad(landingPad);
}
}

if (e.key === "x") {
let nbVertices = 0;
let nbInstances = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/ts/architecture/celestialBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ export interface CelestialBodyModel extends OrbitalObjectModel {
* The radius of the celestial body
*/
readonly radius: number;

getNbSpaceStations(): number;
}
5 changes: 2 additions & 3 deletions src/ts/architecture/orbitalObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import { Transformable } from "./transformable";
import { BoundingSphere } from "./boundingSphere";
import { OrbitProperties } from "../orbit/orbitProperties";
import { rotateVector3AroundInPlace } from "../utils/algebra";
import { Quaternion, Vector3 } from "@babylonjs/core/Maths/math";
import { getRotationQuaternion, setRotationQuaternion, translate } from "../uberCore/transforms/basicTransform";
import { OrbitalObjectPhysicalProperties } from "./physicalProperties";
Expand Down Expand Up @@ -82,8 +81,8 @@ export class OrbitalObject {

// rotate the object around the barycenter of the orbit, around the normal to the orbital plane
const dtheta = (2 * Math.PI * deltaTime) / orbit.period;
rotateVector3AroundInPlace(newPosition, barycenter, orbit.normalToPlane, dtheta);

const rotationQuaternion = Quaternion.RotationAxis(orbit.normalToPlane, dtheta);
newPosition.applyRotationQuaternionInPlace(rotationQuaternion);
newPosition.normalize().scaleInPlace(orbit.radius);

// enforce orbital plane
Expand Down
1 change: 1 addition & 0 deletions src/ts/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import { createButterfly } from "./proceduralAssets/butterfly/butterfly";
import { createGrassBlade } from "./proceduralAssets/grass/grassBlade";
import { ButterflyMaterial } from "./proceduralAssets/butterfly/butterflyMaterial";
import { GrassMaterial } from "./proceduralAssets/grass/grassMaterial";
import { Axis } from "@babylonjs/core/Maths/math.axis";

Check warning on line 76 in src/ts/assets.ts

View workflow job for this annotation

GitHub Actions / build (16)

'Axis' is defined but never used

Check warning on line 76 in src/ts/assets.ts

View workflow job for this annotation

GitHub Actions / build (18)

'Axis' is defined but never used

export class Assets {
static IS_READY = false;
Expand Down
29 changes: 21 additions & 8 deletions src/ts/landingPad/landingPad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,28 @@ import { PhysicsShapeConvexHull } from "@babylonjs/core/Physics/v2/physicsShape"
import { Mesh } from "@babylonjs/core/Meshes/mesh";

Check warning on line 6 in src/ts/landingPad/landingPad.ts

View workflow job for this annotation

GitHub Actions / build (16)

'Mesh' is defined but never used

Check warning on line 6 in src/ts/landingPad/landingPad.ts

View workflow job for this annotation

GitHub Actions / build (18)

'Mesh' is defined but never used
import { CollisionMask } from "../settings";

Check warning on line 7 in src/ts/landingPad/landingPad.ts

View workflow job for this annotation

GitHub Actions / build (16)

'CollisionMask' is defined but never used

Check warning on line 7 in src/ts/landingPad/landingPad.ts

View workflow job for this annotation

GitHub Actions / build (18)

'CollisionMask' is defined but never used
import { Scene } from "@babylonjs/core/scene";
import { Vector3 } from "@babylonjs/core/Maths/math";
import { Quaternion, Vector3 } from "@babylonjs/core/Maths/math";
import { Transformable } from "../architecture/transformable";
import { AbstractMesh } from "@babylonjs/core/Meshes/abstractMesh";
import { PhysicsMotionType } from "@babylonjs/core";
import { Axis } from "@babylonjs/core/Maths/math.axis";

export class LandingPad implements Transformable {
readonly instanceRoot: TransformNode;
readonly aggregate: PhysicsAggregate;

constructor(scene: Scene) {
this.instanceRoot = Assets.CreateLandingPadInstance();
//readonly aggregate: PhysicsAggregate;

this.aggregate = new PhysicsAggregate(
constructor(scene: Scene, existingMesh: AbstractMesh | null = null) {
if (existingMesh === null) {
this.instanceRoot = Assets.CreateLandingPadInstance();
} else {
this.instanceRoot = existingMesh;
}

// init rotation quaternion
this.instanceRoot.rotate(Axis.X, 0);

/*this.aggregate = new PhysicsAggregate(
this.instanceRoot,
PhysicsShapeType.CONTAINER,
{
Expand All @@ -26,22 +37,24 @@ export class LandingPad implements Transformable {
scene
);
this.aggregate.body.setMotionType(PhysicsMotionType.STATIC);
this.aggregate.body.setMassProperties({ inertia: Vector3.Zero(), mass: 0 });
for (const child of this.instanceRoot.getChildMeshes()) {
const childShape = new PhysicsShapeConvexHull(child as Mesh, scene);
childShape.filterMembershipMask = CollisionMask.LANDING_PADS;
this.aggregate.shape.addChildFromParent(this.instanceRoot, childShape, child);
}
this.aggregate.body.disablePreStep = false;
this.aggregate.body.disablePreStep = false;*/
}

getTransform(): TransformNode {
return this.aggregate.transformNode;
return this.instanceRoot;
}

dispose() {
this.aggregate.dispose();
//this.aggregate.dispose();
this.instanceRoot.dispose();
}
}
43 changes: 35 additions & 8 deletions src/ts/landingSimulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { setMaxLinVel } from "./utils/havok";
import { UberScene } from "./uberCore/uberScene";
import { Vector3 } from "@babylonjs/core/Maths/math.vector";
import { Assets } from "./assets";
import { roll, translate } from "./uberCore/transforms/basicTransform";
import { roll, setRotationQuaternion, translate } from "./uberCore/transforms/basicTransform";
import { DirectionalLight } from "@babylonjs/core/Lights/directionalLight";
import { Color4 } from "@babylonjs/core/Maths/math.color";
import "@babylonjs/core/Physics/physicsEngineComponent";
Expand All @@ -21,6 +21,10 @@ import { ShadowGenerator } from "@babylonjs/core/Lights/Shadows/shadowGenerator"
import { MeshBuilder } from "@babylonjs/core/Meshes/meshBuilder";
import { HemisphericLight } from "@babylonjs/core/Lights/hemisphericLight";
import { PointLight } from "@babylonjs/core/Lights/pointLight";
import { SpaceStation } from "./spacestation/spaceStation";
import { Quaternion } from "@babylonjs/core/Maths/math";
import { Axis } from "@babylonjs/core/Maths/math.axis";
import { OrbitalObject } from "./architecture/orbitalObject";

const canvas = document.getElementById("renderer") as HTMLCanvasElement;
canvas.width = window.innerWidth;
Expand Down Expand Up @@ -53,19 +57,33 @@ shadowGenerator.addShadowCaster(spaceship.instanceRoot, true);
spaceship.getTransform().position = new Vector3(0, 0, -10);
roll(spaceship.getTransform(), Math.random() * 6.28);

const landingPad = new LandingPad(scene);
/*const landingPad = new LandingPad(scene);
landingPad.getTransform().position = new Vector3(0, -20, 0);
landingPad.instanceRoot.getChildMeshes().forEach((mesh) => {
mesh.receiveShadows = true;
});
});*/

const physicsViewer = new PhysicsViewer();
/*physicsViewer.showBody(spaceship.aggregate.body);
physicsViewer.showBody(landingPad.aggregate.body);*/

const spacestation = new SpaceStation(scene);
setRotationQuaternion(spacestation.getTransform(), Quaternion.RotationAxis(Axis.X, Math.PI / 2));
translate(spacestation.getTransform(), new Vector3(0, -100, 0));

//physicsViewer.showBody(spacestation.aggregate.body);
/*spacestation.landingPads.forEach(stationLandingPad => {
physicsViewer.showBody(stationLandingPad.aggregate.body);
});*/

/*spacestation.ringAggregates.forEach(ring => {
physicsViewer.showBody(ring.body);
});*/

/*const ground = MeshBuilder.CreateGround("ground", { width: 50, height: 50 });
ground.position.y = -40;
ground.receiveShadows = true;*/

/*const physicsViewer = new PhysicsViewer();
physicsViewer.showBody(spaceship.aggregate.body);
physicsViewer.showBody(landingPad.aggregate.body);*/

const defaultControls = new DefaultControls(scene);
defaultControls.speed *= 15;
Expand All @@ -74,12 +92,18 @@ scene.setActiveController(defaultControls);

translate(defaultControls.getTransform(), new Vector3(50, 0, 0));

defaultControls.getTransform().lookAt(Vector3.Lerp(spaceship.getTransform().position, landingPad.getTransform().position, 0.5));
//defaultControls.getTransform().lookAt(Vector3.Lerp(spaceship.getTransform().position, landingPad.getTransform().position, 0.5));

scene.onBeforeRenderObservable.add(() => {
const deltaTime = scene.deltaTime / 1000;
scene.getActiveController().update(deltaTime);
spaceship.update(deltaTime);

//OrbitalObject.UpdateRotation(spacestation, deltaTime);

spacestation.ringInstances.forEach(mesh => {
mesh.rotate(Axis.Y, 0.01 * deltaTime);
});
});

scene.executeWhenReady(() => {
Expand All @@ -88,9 +112,12 @@ scene.executeWhenReady(() => {
});
});

const landingPad = spacestation.handleDockingRequest();
if(landingPad === null) throw new Error("Docking request denied");

document.addEventListener("keydown", (event) => {
if (event.key === "o") {
spaceship.engageLanding(landingPad);
spaceship.engageLandingOnPad(landingPad);
}
});

Expand Down
4 changes: 4 additions & 0 deletions src/ts/mandelbulb/mandelbulbModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,8 @@ export class MandelbulbModel implements PlanetModel {
getApparentRadius(): number {
return this.radius;
}

getNbSpaceStations(): number {
return 0;
}
}
4 changes: 3 additions & 1 deletion src/ts/model/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export enum GENERATION_STEPS {

PRESSURE = 1100,
WATER_AMOUNT = 1200,
TERRAIN = 1500
TERRAIN = 1500,

SPACE_STATION = 2000
}

export enum BODY_TYPE {
Expand Down
6 changes: 6 additions & 0 deletions src/ts/planets/gasPlanet/gasPlanetModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,10 @@ export class GasPlanetModel implements PlanetModel {
getApparentRadius(): number {
return this.radius;
}

public getNbSpaceStations(): number {
if(uniformRandBool(0.2, this.rng, GENERATION_STEPS.SPACE_STATION)) return 1;
if(uniformRandBool(0.1, this.rng, GENERATION_STEPS.SPACE_STATION + 10)) return 2;
return 0;
}
}
6 changes: 6 additions & 0 deletions src/ts/planets/telluricPlanet/telluricPlanetModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,10 @@ export class TelluricPlanetModel implements PlanetModel {
getApparentRadius(): number {
return this.radius + this.physicalProperties.oceanLevel;
}

public getNbSpaceStations(): number {
if(uniformRandBool(0.2, this.rng, GENERATION_STEPS.SPACE_STATION)) return 1;
if(uniformRandBool(0.1, this.rng, GENERATION_STEPS.SPACE_STATION + 10)) return 2;
return 0;
}
}
7 changes: 4 additions & 3 deletions src/ts/planets/telluricPlanet/terrain/chunks/planetChunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ export class PlanetChunk implements Transformable, BoundingSphere {

this.mesh.parent = parentAggregate.transformNode;

this.mesh.occlusionQueryAlgorithmType = AbstractMesh.OCCLUSION_ALGORITHM_TYPE_CONSERVATIVE;
this.mesh.occlusionType = AbstractMesh.OCCLUSION_TYPE_OPTIMISTIC;
//this.mesh.occlusionQueryAlgorithmType = AbstractMesh.OCCLUSION_ALGORITHM_TYPE_CONSERVATIVE;
//this.mesh.occlusionType = AbstractMesh.OCCLUSION_TYPE_OPTIMISTIC;

this.parent = parentAggregate.transformNode;
this.parentAggregate = parentAggregate;
Expand Down Expand Up @@ -138,7 +138,8 @@ export class PlanetChunk implements Transformable, BoundingSphere {
this.aggregate = new PhysicsAggregate(this.mesh, PhysicsShapeType.MESH, { mass: 0 }, this.mesh.getScene());
this.aggregate.body.setMotionType(PhysicsMotionType.STATIC);
this.aggregate.body.disablePreStep = false;
this.aggregate.shape.filterMembershipMask = CollisionMask.GROUND;
this.aggregate.shape.filterMembershipMask = CollisionMask.ENVIRONMENT;
this.aggregate.shape.filterCollideMask = CollisionMask.DYNAMIC_OBJECTS;
const constraint = new LockConstraint(Vector3.Zero(), this.getTransform().position.negate(), new Vector3(0, 1, 0), new Vector3(0, 1, 0), this.mesh.getScene());
this.parentAggregate.body.addConstraint(this.aggregate.body, constraint);
}
Expand Down
5 changes: 2 additions & 3 deletions src/ts/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ export const Settings = {
};

export const CollisionMask = {
GROUND: 0b00000001,
SPACESHIP: 0b00000010,
LANDING_PADS: 0b00000100
ENVIRONMENT: 0b00000001,
DYNAMIC_OBJECTS: 0b00000010
};

const seedableRNG = seededSquirrelNoise(Settings.UNIVERSE_SEED);
Expand Down
2 changes: 1 addition & 1 deletion src/ts/spacelegs/characterControls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export class CharacterControls implements Controls {
setUpVector(character, up);
}

(this.scene.getPhysicsEngine() as PhysicsEngineV2).raycastToRef(start, end, this.raycastResult, { collideWith: CollisionMask.GROUND });
(this.scene.getPhysicsEngine() as PhysicsEngineV2).raycastToRef(start, end, this.raycastResult, { collideWith: CollisionMask.ENVIRONMENT });
if (this.raycastResult.hasHit) {
const up = character.up;
const distance = Vector3.Dot(character.getAbsolutePosition().subtract(this.raycastResult.hitPointWorld), up);
Expand Down
4 changes: 0 additions & 4 deletions src/ts/spaceship/shipControls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,6 @@ export class ShipControls implements Controls {
const deltaThrottle = keyboard.getZAxis() * deltaTime;
this.spaceship.getWarpDrive().increaseTargetThrottle(deltaThrottle);
}

const warpSpeed = getForwardDirection(this.getTransform()).scale(this.spaceship.getWarpDrive().getWarpSpeed());
//this.aggregate.body.setLinearVelocity(warpSpeed);
translate(this.getTransform(), warpSpeed.scale(deltaTime));
}
}

Expand Down
Loading

0 comments on commit 504ae66

Please sign in to comment.