Skip to content

Commit

Permalink
added mouse control to spaceship
Browse files Browse the repository at this point in the history
+ warpdrive is engaged after jump
  • Loading branch information
BarthPaleologue committed Aug 3, 2023
1 parent af806c7 commit 2b23167
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 29 deletions.
12 changes: 6 additions & 6 deletions src/ts/controller/inputs/mouse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class Mouse implements Input {
this.deadAreaRadius = deadAreaRadius;
this.canvas = canvas;

this.canvas.addEventListener("mousemove", (e) => {
window.addEventListener("mousemove", (e) => {
this.dx = (e.x - this.x) / this.canvas.width;
this.dy = (e.y - this.y) / this.canvas.height;

Expand All @@ -32,7 +32,10 @@ export class Mouse implements Input {
}

getRoll() {
return 0;
const d2 = this.dxToCenter ** 2 + this.dyToCenter ** 2;
const adaptedLength = Math.max(Math.log(d2 / this.deadAreaRadius ** 2), 0) / 3;
const greaterLength = Math.max(this.canvas.width, this.canvas.height);
return (this.dxToCenter * adaptedLength) / (greaterLength / 2);
}

getPitch() {
Expand All @@ -43,10 +46,7 @@ export class Mouse implements Input {
}

getYaw() {
const d2 = this.dxToCenter ** 2 + this.dyToCenter ** 2;
const adaptedLength = Math.max(Math.log(d2 / this.deadAreaRadius ** 2), 0) / 3;
const greaterLength = Math.max(this.canvas.width, this.canvas.height);
return (this.dxToCenter * adaptedLength) / (greaterLength / 2);
return 0;
}

getZAxis() {
Expand Down
5 changes: 4 additions & 1 deletion src/ts/controller/planetEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import HavokPhysics from "@babylonjs/havok";
import "@babylonjs/core/Engines/WebGPU/Extensions/";
import { SystemUI } from "../ui/systemUI";
import { BlackHole } from "../view/bodies/stellarObjects/blackHole";
import { ShipController } from "../spaceship/shipController";

export class PlanetEngine {
// UI
Expand Down Expand Up @@ -125,7 +126,9 @@ export class PlanetEngine {
this.init();
const firstBody = this.getStarSystem().getBodies()[0];
if (firstBody === undefined) throw new Error("No bodies in star system");
positionNearObject(this.getStarSystemScene().getActiveController(), firstBody, this.getStarSystem(), firstBody instanceof BlackHole ? 5 : 3);
const activeController = this.getStarSystemScene().getActiveController();
positionNearObject(activeController, firstBody, this.getStarSystem(), firstBody instanceof BlackHole ? 5 : 3);
if (activeController instanceof ShipController) activeController.enableWarpDrive();
this.toggleStarMap();
});

Expand Down
3 changes: 2 additions & 1 deletion src/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ await engine.setup();

const scene = engine.getStarSystemScene();

const mouse = new Mouse(engine.canvas, 1e5);
const mouse = new Mouse(engine.canvas, 100);
const keyboard = new Keyboard();
const gamepad = new Gamepad();

Expand All @@ -41,6 +41,7 @@ const spaceshipController = new ShipController(scene);
spaceshipController.getActiveCamera().maxZ = Settings.EARTH_RADIUS * 100000;
spaceshipController.addInput(keyboard);
spaceshipController.addInput(gamepad);
spaceshipController.addInput(mouse);

scene.setActiveController(spaceshipController);

Expand Down
5 changes: 5 additions & 0 deletions src/ts/spaceship/rcsThruster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export class RCSThruster extends AbstractThruster {
this.throttle = 1;
}

public setThrottle(throttle: number): void {
if(throttle < 0 || throttle > 1) throw new Error("Throttle must be between 0 and 1");
this.throttle = throttle;
}

public deactivate(): void {
this.throttle = 0;
}
Expand Down
54 changes: 33 additions & 21 deletions src/ts/spaceship/shipController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { MeshBuilder } from "@babylonjs/core/Meshes/meshBuilder";
export class ShipController extends AbstractController {
readonly transform: NewtonianTransform;

readonly rollAuthority = 0.5;
readonly rollAuthority = 0.1;
readonly pitchAuthority = 1;
readonly yawAuthority = 1;

Expand All @@ -31,7 +31,7 @@ export class ShipController extends AbstractController {
private readonly mainThrusters: MainThruster[] = [];
private readonly rcsThrusters: RCSThruster[] = [];

private readonly warpDrive = new WarpDrive();
private readonly warpDrive = new WarpDrive(true);

private closestDistanceToPlanet = Infinity;

Expand Down Expand Up @@ -100,12 +100,15 @@ export class ShipController extends AbstractController {
this.closestDistanceToPlanet = distance;
}

public enableWarpDrive() {
for(const thruster of this.mainThrusters) thruster.setThrottle(0);
for(const thruster of this.rcsThrusters) thruster.deactivate();
this.warpDrive.enable();
}

public toggleWarpDrive() {
if (!this.warpDrive.isEnabled()) {
this.warpDrive.enable();
for (const thruster of this.mainThrusters) thruster.setThrottle(0);
for (const thruster of this.rcsThrusters) thruster.deactivate();
} else this.warpDrive.desengage();
if (!this.warpDrive.isEnabled()) this.enableWarpDrive();
else this.warpDrive.desengage();
}

private getTotalAuthority(direction: Vector3) {
Expand Down Expand Up @@ -156,10 +159,6 @@ export class ShipController extends AbstractController {
if (keyboard.isPressed("3")) this.thirdPersonCamera.rotatePhi(-0.8 * deltaTime);
if (keyboard.isPressed("5")) this.thirdPersonCamera.rotateTheta(-0.8 * deltaTime);
if (keyboard.isPressed("2")) this.thirdPersonCamera.rotateTheta(0.8 * deltaTime);
} else if (input.type === InputType.MOUSE) {
const mouse = input as Mouse;
this.thirdPersonCamera.rotatePhi(mouse.getYaw() * deltaTime);
this.thirdPersonCamera.rotateTheta(mouse.getPitch() * deltaTime);
}
}

Expand All @@ -185,17 +184,25 @@ export class ShipController extends AbstractController {
else if (input.getYAxis() < 0 && rcsThruster.getAuthority01(LOCAL_DIRECTION.DOWN) > 0.5) rcsThruster.activate();
else if (input.getXAxis() > 0 && rcsThruster.getAuthority01(LOCAL_DIRECTION.RIGHT) > 0.5) rcsThruster.activate();
else if (input.getXAxis() < 0 && rcsThruster.getAuthority01(LOCAL_DIRECTION.LEFT) > 0.5) rcsThruster.activate();
// rcs rotation contribution
else if (input.getRoll() < 0 && rcsThruster.getAuthorityAroundAxis01(LOCAL_DIRECTION.BACKWARD) > 0.2) rcsThruster.activate();
else if (input.getRoll() > 0 && rcsThruster.getAuthorityAroundAxis01(LOCAL_DIRECTION.FORWARD) > 0.2) rcsThruster.activate();
else if (input.getPitch() > 0 && rcsThruster.getAuthorityAroundAxis01(LOCAL_DIRECTION.RIGHT) > 0.2) rcsThruster.activate();
else if (input.getPitch() < 0 && rcsThruster.getAuthorityAroundAxis01(LOCAL_DIRECTION.LEFT) > 0.2) rcsThruster.activate();
else if (input.getYaw() < 0 && rcsThruster.getAuthorityAroundAxis01(LOCAL_DIRECTION.DOWN) > 0.2) rcsThruster.activate();
else if (input.getYaw() > 0 && rcsThruster.getAuthorityAroundAxis01(LOCAL_DIRECTION.UP) > 0.2) rcsThruster.activate();
else rcsThruster.deactivate();
}
}

if(input.type === InputType.MOUSE) {
const mouse = input as Mouse;
const roll = mouse.getRoll();
const pitch = mouse.getPitch();

for (const rcsThruster of this.rcsThrusters) {
// rcs rotation contribution
if (roll < 0 && rcsThruster.getAuthorityAroundAxis01(LOCAL_DIRECTION.FORWARD) > 0.2) rcsThruster.setThrottle(Math.abs(roll));
else if (roll > 0 && rcsThruster.getAuthorityAroundAxis01(LOCAL_DIRECTION.BACKWARD) > 0.2) rcsThruster.setThrottle(Math.abs(roll));

if (pitch > 0 && rcsThruster.getAuthorityAroundAxis01(LOCAL_DIRECTION.RIGHT) > 0.2) rcsThruster.setThrottle(Math.abs(pitch));
else if (pitch < 0 && rcsThruster.getAuthorityAroundAxis01(LOCAL_DIRECTION.LEFT) > 0.2) rcsThruster.setThrottle(Math.abs(pitch));
}
}

this.transform.rotationAcceleration.x = this.getTotalRollAuthority() * deltaTime;
this.transform.rotationAcceleration.y = this.getTotalPitchAuthority() * deltaTime;
this.transform.rotationAcceleration.z = this.getTotalYawAuthority() * deltaTime;
Expand All @@ -218,9 +225,14 @@ export class ShipController extends AbstractController {
this.transform.acceleration.addInPlace(rightAcceleration);
this.transform.acceleration.addInPlace(leftAcceleration);
} else {
this.transform.rotationAcceleration.x += this.rollAuthority * input.getRoll() * deltaTime;
this.transform.rotationAcceleration.y += this.pitchAuthority * input.getPitch() * deltaTime;
this.transform.rotationAcceleration.z += this.yawAuthority * input.getYaw() * deltaTime;
if(input.type === InputType.MOUSE) {
const mouse = input as Mouse;
const roll = mouse.getRoll();
const pitch = mouse.getPitch();

this.transform.rotationAcceleration.x -= 2 * this.rollAuthority * roll * deltaTime;
this.transform.rotationAcceleration.y += this.pitchAuthority * pitch * deltaTime;
}

const warpSpeed = this.transform.getForwardDirection().scale(this.warpDrive.getWarpSpeed());
this.transform.speed.copyFrom(warpSpeed);
Expand Down
4 changes: 4 additions & 0 deletions src/ts/spaceship/warpDrive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ export class WarpDrive {
*/
private state = WARPDRIVE_STATE.DISABLED;

constructor(enabledByDefault = false) {
this.state = enabledByDefault ? WARPDRIVE_STATE.ENABLED : WARPDRIVE_STATE.DISABLED;
}

/**
* Enables the warp drive: the ship will start to accelerate towards the target speed.
*/
Expand Down

0 comments on commit 2b23167

Please sign in to comment.