Skip to content

Commit

Permalink
Whitespace - Format
Browse files Browse the repository at this point in the history
  • Loading branch information
PhoenixIllusion committed Jun 13, 2024
1 parent 02b41c4 commit 41a3cb7
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 104 deletions.
8 changes: 4 additions & 4 deletions basic-examples/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class App {
const initJolt = (await import('jolt-physics/wasm')).default;
setJoltModule(() => initJolt({
locateFile: () => joltWasmUrl,
}));
}));
const joltPlugin = await JoltJSPlugin.loadPlugin(true);
scene.enablePhysics(new Vector3(0, -9.8, 0), joltPlugin)

Expand All @@ -93,7 +93,7 @@ export class App {
} else {
this.config.getCamera();
}


// This creates a light, aiming 0,1,0 - to the sky (non-mesh)
const light = new DirectionalLight('light', new Vector3(-1, -3, 0), scene);
Expand All @@ -106,11 +106,11 @@ export class App {
const callback = maybeCallback instanceof Promise ? await maybeCallback : maybeCallback;

joltPlugin.registerPerPhysicsStepCallback(delta => {
if(callback) [
if (callback) [
callback(performance.now(), delta)
]
})

// run the main render loop
engine.runRenderLoop(() => {
const scene = this.scene;
Expand Down
31 changes: 16 additions & 15 deletions basic-examples/src/scene/character_virtual_ribbon_world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,39 +40,40 @@ export default (scene: Scene): SceneCallback => {
floor.ground.material = material;

const path = createPath3DWithCatmullRomPath(
[new Vector3(0, 0, -20), new Vector3(0, 0, -10), new Vector3(0, 0, 0), new Vector3(0, 0, 10), new Vector3(0, 0, 20), new Vector3(0,0, 30), new Vector3(0,0, 40)],
[new Vector3(0, 0, -20), new Vector3(0, 0, -10), new Vector3(0, 0, 0), new Vector3(0, 0, 10), new Vector3(0, 0, 20), new Vector3(0, 0, 30), new Vector3(0, 0, 40)],
[new Vector3(0, 1, 0), new Vector3(1, 0, 0), new Vector3(0, -1, 0), new Vector3(-1, 0, 0), new Vector3(0, 1, 0), new Vector3(1, 0, 0), new Vector3(0, -1, 0)], 50, false);

const ribbonGravity: GravityInterface = {
getGravity: (com: ()=>Vector3): Vector3 => {
const point = path.getClosestPositionTo(com());
return path.getNormalAt(point, true).scale(9.8)
}};
getGravity: (com: () => Vector3): Vector3 => {
const point = path.getClosestPositionTo(com());
return path.getNormalAt(point, true).scale(9.8)
}
};

const points = path.getPoints();
const binormals = path.getBinormals();
const normals = path.getNormals();
const pathArray: [Vector3[],Vector3[]]= [[],[]];
points.forEach( (p,i) => {
const pathArray: [Vector3[], Vector3[]] = [[], []];
points.forEach((p, i) => {
pathArray[1].push(p.add(binormals[i].scale(3)))
pathArray[0].push(p.subtract(binormals[i].scale(3)))
if(i % 8 == 4) {
if (i % 8 == 4) {
const pos = p.add(new Vector3(0, 10, 0)).add(normals[i].negate());
const boxes1 = createBox(pos.add(binormals[i].scale(2)), Quaternion.Identity(), new Vector3(0.25,0.25,0.25), { mass: 1, friction: 1});
boxes1.physics.setGravityOverride(ribbonGravity);
const boxes2 = createBox(pos.subtract(binormals[i].scale(2)), Quaternion.Identity(), new Vector3(0.25,0.25,0.25), { mass: 1, friction: 1});
boxes2.physics.setGravityOverride(ribbonGravity);
const boxes1 = createBox(pos.add(binormals[i].scale(2)), Quaternion.Identity(), new Vector3(0.25, 0.25, 0.25), { mass: 1, friction: 1 });
boxes1.physics.setGravityOverride(ribbonGravity);
const boxes2 = createBox(pos.subtract(binormals[i].scale(2)), Quaternion.Identity(), new Vector3(0.25, 0.25, 0.25), { mass: 1, friction: 1 });
boxes2.physics.setGravityOverride(ribbonGravity);
}
});

const ribbon = MeshBuilder.CreateRibbon("ribbon", { pathArray, sideOrientation: Mesh.DOUBLESIDE }, scene);
const ribbon = MeshBuilder.CreateRibbon("ribbon", { pathArray, sideOrientation: Mesh.DOUBLESIDE }, scene);
ribbon.position.y += 10;
ribbon.physicsImpostor = new PhysicsImpostor(ribbon, PhysicsImpostor.MeshImpostor, { mass: 0 });

ribbon.material = material;
material.sideOrientation = 2
//ribbon.material!.wireframe = true;
new HemisphericLight('hemi', new Vector3(0,0,-1));
new HemisphericLight('hemi', new Vector3(0, 0, -1));

const createCharacter = () => {
const staticShape = new Mesh('static-shape', scene);
Expand Down Expand Up @@ -157,6 +158,6 @@ export default (scene: Scene): SceneCallback => {
return (_time: number, _delta: number) => {
inputHandler.updateInput(input.direction.negate(), input.jump);
camera.upVector.copyFrom(inputHandler.up);
camera.position.copyFrom(char.mesh.position.add(inputHandler.up.scale(10)).addInPlaceFromFloats(0,0,-20));
camera.position.copyFrom(char.mesh.position.add(inputHandler.up.scale(10)).addInPlaceFromFloats(0, 0, -20));
}
}
22 changes: 11 additions & 11 deletions basic-examples/src/scene/character_virtual_sphere_world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const config: SceneConfig = {
}

export default (scene: Scene): SceneCallback => {
const floor = createSphere(new Vector3(0,-4,0), 14, { mass: 0, friction: 1});
const floor = createSphere(new Vector3(0, -4, 0), 14, { mass: 0, friction: 1 });
const tiledTexture = new Texture('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAAAAABX3VL4AAAACXBIWXMAAC4jAAAuIwF4pT92AAAAB3RJTUUH5wsCAyocY2BWPgAAABl0RVh0Q29tbWVudABDcmVhdGVkIHdpdGggR0lNUFeBDhcAAAAOSURBVAjXY2D4z/CfAQAGAAH/P9ph1wAAAABJRU5ErkJggg==');
tiledTexture.onLoadObservable.add(() => {
tiledTexture.wrapU = 1;
Expand All @@ -35,7 +35,7 @@ export default (scene: Scene): SceneCallback => {
floor.sphere.material = material;


const upIndicator = MeshBuilder.CreateSphere('sphere', { diameter: 0.25});
const upIndicator = MeshBuilder.CreateSphere('sphere', { diameter: 0.25 });
upIndicator.material = getMaterial('#00ff00');
upIndicator.position.y += 0.7;
const createCharacter = () => {
Expand All @@ -61,17 +61,17 @@ export default (scene: Scene): SceneCallback => {
inputHandler.jumpSpeed = 6;
char.phyics.controller.inputHandler = inputHandler;

const sphereGravity = new GravityPoint(new Vector3(0,-4,0), 9.81);
const sphereGravity = new GravityPoint(new Vector3(0, -4, 0), 9.81);

char.phyics.setGravityOverride(sphereGravity)

for(let j=0;j<4;j++)
for(let i=0; i< 360; i+= 15) {
const rad = DegreesToRadians(i);
const [x,z] = [Math.cos(rad), Math.sin(rad)]
const item = createBox(new Vector3(x*15, j*3-6, z*15), Quaternion.Identity(), new Vector3(0.25, 0.25, 0.25), { mass: 1, friction: 1}, '#ff44ff');
item.physics.setGravityOverride(sphereGravity)
}
for (let j = 0; j < 4; j++)
for (let i = 0; i < 360; i += 15) {
const rad = DegreesToRadians(i);
const [x, z] = [Math.cos(rad), Math.sin(rad)]
const item = createBox(new Vector3(x * 15, j * 3 - 6, z * 15), Quaternion.Identity(), new Vector3(0.25, 0.25, 0.25), { mass: 1, friction: 1 }, '#ff44ff');
item.physics.setGravityOverride(sphereGravity)
}


const input = {
Expand Down Expand Up @@ -125,7 +125,7 @@ export default (scene: Scene): SceneCallback => {

return (_time: number, _delta: number) => {
inputHandler.updateInput(input.direction, input.jump);
upIndicator.position.copyFrom( char.mesh.position.add(inputHandler.up.scale(2)));
upIndicator.position.copyFrom(char.mesh.position.add(inputHandler.up.scale(2)));

}
}
2 changes: 1 addition & 1 deletion basic-examples/src/scene/gravity-factor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export default (_scene: Scene): SceneCallback => {
// Create physics body
const position = new Vector3(-10.0 + 2.0 * i, 15, 0);
const box = createBox(position, Quaternion.Identity(), new Vector3(0.5, 0.5, 0.5), { mass: 10, friction: 0.1 * i, restitution: 0.2 }, '#990099');
box.physics.setGravityFactor(i/10);
box.physics.setGravityFactor(i / 10);
}
}
16 changes: 8 additions & 8 deletions basic-examples/src/scene/gravity-override.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { GravityPoint, GravityVector, GravityInterface } from '@phoenixillusion/
export default (scene: Scene): SceneCallback => {
createFloor({ friction: 0.8, mass: 0, restitution: 0 });

const pointGravity = new GravityPoint(new Vector3(0,15,0), 9.81);
const pointGravity = new GravityPoint(new Vector3(0, 15, 0), 9.81);
const linearGravity = new GravityVector(new Vector3(-9.81, 0, 0));

const boxes: PhysicsImpostor[] = [];
Expand All @@ -22,35 +22,35 @@ export default (scene: Scene): SceneCallback => {
createBox(position, Quaternion.Identity(), new Vector3(0.5, 0.5, 0.5), { mass: 10, friction: 0.1 * i, restitution: 0.2 }, '#ff0000');
}

const gravityLeft = MeshBuilder.CreateBox('gravity-left', { size: 1}, scene);
const gravityLeft = MeshBuilder.CreateBox('gravity-left', { size: 1 }, scene);
gravityLeft.material = getMaterial('#ff0000');
gravityLeft.position.x -= 15;
gravityLeft.isPickable = true;

const gravityRight = MeshBuilder.CreateBox('gravity-right', { size: 1}, scene);
const gravityRight = MeshBuilder.CreateBox('gravity-right', { size: 1 }, scene);
gravityRight.material = getMaterial('#00ff00');
gravityRight.position.x += 15;
gravityRight.isPickable = true;

const gravityCenter = MeshBuilder.CreateBox('gravity-center', { size: 1}, scene);
const gravityCenter = MeshBuilder.CreateBox('gravity-center', { size: 1 }, scene);
gravityCenter.material = getMaterial('#00ffff');
gravityCenter.position.y += 15;
gravityCenter.isPickable = true;

scene.onPointerDown = (_evt, _pickInfo, _type) => {
let gravity: GravityInterface | undefined = undefined;
if(_pickInfo.pickedMesh == gravityLeft) {
if (_pickInfo.pickedMesh == gravityLeft) {
gravity = linearGravity;
linearGravity.gravity.set(-9.81, 0, 0);
}
if(_pickInfo.pickedMesh == gravityRight) {
if (_pickInfo.pickedMesh == gravityRight) {
gravity = linearGravity;
linearGravity.gravity.set(9.81, 0, 0);
}
if(_pickInfo.pickedMesh == gravityCenter) {
if (_pickInfo.pickedMesh == gravityCenter) {
gravity = pointGravity;
}
if(gravity) {
if (gravity) {
boxes.forEach(box => {
box.setGravityOverride(gravity!);
})
Expand Down
48 changes: 24 additions & 24 deletions basic-examples/src/scene/vehicle_kart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { StandardMaterial } from '@babylonjs/core/Materials/standardMaterial';
import { Mesh } from '@babylonjs/core/Meshes/mesh';
import { Scene } from '@babylonjs/core/scene';
import { PhysicsImpostorParameters } from '@babylonjs/core/Physics/v1/physicsImpostor';
import { setupTachometer, setupVehicleInput } from '../util/vehicle-utils';
import { setupTachometer, setupVehicleInput } from '../util/vehicle-utils';
import { AbstractMesh } from '@babylonjs/core/Meshes/abstractMesh';
import { Color3 } from '@babylonjs/core/Maths/math.color';
import { ParticleSystem } from '@babylonjs/core/Particles/particleSystem';
Expand All @@ -30,9 +30,9 @@ function getAreaFromRect(rect: SVGRectElement) {
const height = rect.height.baseVal.value;

return {
center: new Vector3(x + width/2, -0.25, y + height/2),
center: new Vector3(x + width / 2, -0.25, y + height / 2),
extents: new Vector3(width, 1, height)
}
}
}

async function loadKartTrack() {
Expand All @@ -44,24 +44,24 @@ async function loadKartTrack() {
const svgWidth = svg.viewBox!.baseVal.width;
const svgHeight = svg.viewBox!.baseVal.height;
const scale = 1.5;
const offset = new Vector3(-(svgWidth*scale)/2 , 0, -(svgHeight*scale)/2 )
const floor = createFloor({friction: 1, mass: 0}, '#222222', (svgWidth*scale));
const offset = new Vector3(-(svgWidth * scale) / 2, 0, -(svgHeight * scale) / 2)
const floor = createFloor({ friction: 1, mass: 0 }, '#222222', (svgWidth * scale));
const floorMat = floor.ground.material as StandardMaterial;
floorMat.emissiveTexture = new Texture('kart-track-ground.svg');
paths.forEach(path => {
const len = path.getTotalLength();
const start = path.getPointAtLength(0);
const end = path.getPointAtLength(len);
const dx = Math.abs(start.x - end.x)*scale;
const dy = Math.abs(start.y - end.y)*scale;
const midX = ((start.x + end.x)/2)*scale;
const midY = ((start.y + end.y)/2)*scale;
const dx = Math.abs(start.x - end.x) * scale;
const dy = Math.abs(start.y - end.y) * scale;
const midX = ((start.x + end.x) / 2) * scale;
const midY = ((start.y + end.y) / 2) * scale;
const height = path.getAttribute('stroke') == 'blue' ? 1.25 : .8;
const color = path.getAttribute('stroke') == 'blue' ? '#990000': '#000099';
const extents = new Vector3(dx/2 + 0.75, height, dy/2 + 0.75);
const color = path.getAttribute('stroke') == 'blue' ? '#990000' : '#000099';
const extents = new Vector3(dx / 2 + 0.75, height, dy / 2 + 0.75);
const physicsExtents = extents.scale(2);
physicsExtents.y *= 10;
createBox(new Vector3(midX, -0.75, midY).add(offset), Quaternion.Identity(), extents, { mass: 0, extents: physicsExtents}, color);
createBox(new Vector3(midX, -0.75, midY).add(offset), Quaternion.Identity(), extents, { mass: 0, extents: physicsExtents }, color);
})
const start = getPointFromEllipse(svg.getElementById('start') as SVGEllipseElement).scale(scale).add(offset);
const collectables = Array.from(svg.querySelectorAll('ellipse')).filter(ele => ele.id != 'start').map(ele => getPointFromEllipse(ele).scale(scale).add(offset))
Expand All @@ -75,7 +75,7 @@ async function loadKartTrack() {

class Pickup {
pickedUp = false;

constructor(private mesh: AbstractMesh) {
}

Expand All @@ -102,45 +102,45 @@ export default async (scene: Scene): Promise<SceneCallback> => {

const centerOfMass = new Vector3(0, -.435, 0);
const physicSetting: PhysicsImpostorParameters = { mass: 125, restitution: 0, friction: 0, centerOfMass: centerOfMass };

const elements = await loadKartTrack();

const car = createBox(elements.start, Quaternion.RotationAxis(new Vector3(0,1,0), Math.PI/2), new Vector3(0.45, .1, 1), physicSetting, '#FF0000');
const car = createBox(elements.start, Quaternion.RotationAxis(new Vector3(0, 1, 0), Math.PI / 2), new Vector3(0.45, .1, 1), physicSetting, '#FF0000');
car.box.material!.wireframe = true;


elements.collectables.forEach(v => {
const sphere = createSphere(v, 0.5, { sensor: true, mass: 0}, '#ffff00');
const sphere = createSphere(v, 0.5, { sensor: true, mass: 0 }, '#ffff00');
sphere.sphere.scaling.scaleInPlace(0.5);
const material = sphere.sphere.material as StandardMaterial;
//material.disableLighting = true;
material.emissiveColor = Color3.FromHexString('#aaaa00');
const pickup = new Pickup(sphere.sphere);
car.physics.registerOnJoltPhysicsCollide('on-contact-add',sphere.physics, () => pickup.trigger())
car.physics.registerOnJoltPhysicsCollide('on-contact-add', sphere.physics, () => pickup.trigger())
})

let speedBoostActive = false;
elements.speedRegions.forEach(region => {
const speedArea = createBox(region.center, Quaternion.Identity(), region.extents.scale(0.5), { sensor: true, mass: 0}, "#ffff00");
const speedArea = createBox(region.center, Quaternion.Identity(), region.extents.scale(0.5), { sensor: true, mass: 0 }, "#ffff00");
speedArea.box.visibility = 0.2;
speedArea.box.scaling.y /= 100;
const onSpeedBoost = () => {
speedBoostActive = true;
}
car.physics.registerOnJoltPhysicsCollide('on-contact-add',speedArea.physics, onSpeedBoost)
car.physics.registerOnJoltPhysicsCollide('on-contact-persist',speedArea.physics, onSpeedBoost)
car.physics.registerOnJoltPhysicsCollide('on-contact-add', speedArea.physics, onSpeedBoost)
car.physics.registerOnJoltPhysicsCollide('on-contact-persist', speedArea.physics, onSpeedBoost)
})

const CoM = MeshBuilder.CreateSphere('center-of-mass', { segments: 34, diameter: 0.2});
const CoM = MeshBuilder.CreateSphere('center-of-mass', { segments: 34, diameter: 0.2 });
CoM.parent = car.box;
CoM.position.copyFrom(centerOfMass);
const wheeledConfig: Vehicle.WheeledVehicleSettings = createBasicCar({ height: .2, length: 2, width: .9 }, { radius: .2, width: .2 }, true);

const lonScale = 2;
const latScale = 5;
wheeledConfig.wheels.forEach(wheel => {
wheel.longitudinalFriction = [[0,0], [0.06, 1.2], [0.2, 1]].map(([x,y]) => ([x,y*lonScale]))
wheel.lateralFriction = [[0,0],[3, 1.2],[20, 1]].map(([x,y]) => ([x,y*latScale*latScale]))
wheel.longitudinalFriction = [[0, 0], [0.06, 1.2], [0.2, 1]].map(([x, y]) => ([x, y * lonScale]))
wheel.lateralFriction = [[0, 0], [3, 1.2], [20, 1]].map(([x, y]) => ([x, y * latScale * latScale]))
wheel.position.y += 0.35;
});
wheeledConfig.engine = { maxTorque: 900, maxRPM: 2000 };
Expand Down Expand Up @@ -184,7 +184,7 @@ export default async (scene: Scene): Promise<SceneCallback> => {
vehicleInput.input.handBrake = input.handbrake;

const newTorque = input.boost ? 2.5 * stdTorque : stdTorque;
if(speedBoostActive) {
if (speedBoostActive) {
boostParticles.start()
} else {
boostParticles.stop();
Expand Down
Loading

0 comments on commit 41a3cb7

Please sign in to comment.