Skip to content

Commit

Permalink
utilized new convenience functions in examples and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyGura committed Apr 2, 2023
1 parent f8bbefd commit 0fe4b23
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 111 deletions.
28 changes: 10 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,28 +114,20 @@ world.addEntity(renderer);
renderer.activate();

// create floor (static rigid body)
const floor = new Gg3dEntity(
world.visualScene.factory.createBox({ x: 7, y: 7, z: 1 }),
world.physicsWorld.factory.createRigidBody({
shape: { shape: 'BOX', dimensions: { x: 7, y: 7, z: 1 } },
body: { dynamic: false },
}),
);
world.addEntity(floor);

// spawn cubes with mass 5kg twice a second
world.addPrimitiveRigidBody({
shape: { shape: 'BOX', dimensions: { x: 7, y: 7, z: 1 } },
body: { dynamic: false },
});

// spawn cubes with mass 1kg twice a second
interval(500).subscribe(() => {
// generate cube
let item: Gg3dEntity = new Gg3dEntity(
world.visualScene.factory.createBox({ x: 1, y: 1, z: 1 }),
world.physicsWorld.factory.createRigidBody({
shape: { shape: 'BOX', dimensions: { x: 1, y: 1, z: 1 } },
body: { mass: 1 },
}),
);
let item: Gg3dEntity = world.addPrimitiveRigidBody({
shape: { shape: 'BOX', dimensions: { x: 1, y: 1, z: 1 } },
body: { mass: 1 },
});
// set position to cube
item.position = { x: Math.random() * 5 - 2.5, y: Math.random() * 5 - 2.5, z: 10 };
world.addEntity(item);
// delete cube from world after 30 seconds
setTimeout(() => { world.removeEntity(item, true); }, 30000);
});
Expand Down
45 changes: 17 additions & 28 deletions examples/angular-pixi-matterjs/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import {Gg2dEntity, Gg2dWorld, GgViewport, GgViewportManager, Point2} from '@gg-web-engine/core';
import {interval} from 'rxjs';
import {Gg2dVisualScene, GgRenderer} from '@gg-web-engine/pixi';
import {Gg2dPhysicsWorld} from '@gg-web-engine/matter';
import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { Gg2dEntity, Gg2dWorld, GgViewport, GgViewportManager, Point2 } from '@gg-web-engine/core';
import { interval } from 'rxjs';
import { Gg2dVisualScene, GgRenderer } from '@gg-web-engine/pixi';
import { Gg2dPhysicsWorld } from '@gg-web-engine/matter';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit {
title = 'angular-three-ammo';
Expand All @@ -26,36 +26,25 @@ export class AppComponent implements OnInit {
world.addEntity(renderer);
renderer.activate();

const floor = new Gg2dEntity(
world.visualScene.factory.createSquare({x: 800, y: 100}),
world.physicsWorld.factory.createRigidBody({
shape: {shape: 'SQUARE', dimensions: {x: 800, y: 100}},
body: {dynamic: false}
}),
);
const floor = world.addPrimitiveRigidBody({
shape: { shape: 'SQUARE', dimensions: { x: 800, y: 100 } },
body: { dynamic: false },
});
GgViewport.instance.subscribeOnViewportSize().subscribe((newSize: Point2) => {
floor.position = {x: newSize.x / 2, y: newSize.y - 75};
floor.position = { x: newSize.x / 2, y: newSize.y - 75 };
});
world.addEntity(floor);

interval(500).subscribe(() => {
let item: Gg2dEntity;
if (Math.random() >= 0.5) {
item = new Gg2dEntity(
world.visualScene.factory.createSquare({x: 25, y: 25}),
world.physicsWorld.factory.createRigidBody({
shape: {shape: 'SQUARE', dimensions: {x: 25, y: 25}},
body: {mass: 1}
}),
);
item = world.addPrimitiveRigidBody({
shape: { shape: 'SQUARE', dimensions: { x: 25, y: 25 } },
body: { mass: 1 },
});
} else {
item = new Gg2dEntity(
world.visualScene.factory.createCircle(13),
world.physicsWorld.factory.createRigidBody({shape: {shape: 'CIRCLE', radius: 13}, body: {mass: 1}}),
);
item = world.addPrimitiveRigidBody({ shape: { shape: 'CIRCLE', radius: 13 }, body: { mass: 1 } });
}
item.position = {x: GgViewport.instance.getCurrentViewportSize().x / 2 + Math.random() * 100 - 50, y: 75};
world.addEntity(item);
item.position = { x: GgViewport.instance.getCurrentViewportSize().x / 2 + Math.random() * 100 - 50, y: 75 };
});

world.start();
Expand Down
94 changes: 37 additions & 57 deletions examples/angular-three-ammo/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import {
createInlineTickController,
Camera3dAnimator,
Gg3dEntity,
Gg3dWorld,
GgViewportManager,
Gg3dTriggerEntity,
Gg3dWorld,
GgPositionable3dEntity,
Qtrn
GgViewportManager,
} from '@gg-web-engine/core';
import {interval} from 'rxjs';
import {Gg3dVisualScene, GgRenderer} from '@gg-web-engine/three';
import {Gg3dPhysicsWorld} from '@gg-web-engine/ammo';
import { interval } from 'rxjs';
import { Gg3dVisualScene, GgRenderer } from '@gg-web-engine/three';
import { Gg3dPhysicsWorld } from '@gg-web-engine/ammo';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit {
title = 'angular-three-ammo';
Expand All @@ -32,30 +31,27 @@ export class AppComponent implements OnInit {
const canvas = await GgViewportManager.instance.createCanvas(1);
const renderer: GgRenderer = new GgRenderer(canvas);
world.addEntity(renderer);
createInlineTickController(world).subscribe(([elapsed, _]) => {
renderer.camera.position = {
x: 15 * Math.sin(elapsed / 2000),
y: 15 * Math.cos(elapsed / 2000),
const cameraController: Camera3dAnimator = new Camera3dAnimator(renderer.camera, (timeElapsed, _) => ({
position: {
x: 15 * Math.sin(timeElapsed / 2000),
y: 15 * Math.cos(timeElapsed / 2000),
z: 9,
};
renderer.camera.rotation = Qtrn.lookAt(renderer.camera.position, {x: 0, y: 0, z: 0}, {x: 0, y: 0, z: 1});
});
},
target: { x: 0, y: 0, z: 0 },
}));
world.addEntity(cameraController);
renderer.activate();

const floor = new Gg3dEntity(
world.visualScene.factory.createBox({x: 7, y: 7, z: 1}),
world.physicsWorld.factory.createRigidBody({
shape: {shape: 'BOX', dimensions: {x: 7, y: 7, z: 1}},
body: {dynamic: false}
}),
);
world.addEntity(floor);
world.addPrimitiveRigidBody({
shape: { shape: 'BOX', dimensions: { x: 7, y: 7, z: 1 } },
body: { dynamic: false },
});

const destroyTrigger = new Gg3dTriggerEntity(world.physicsWorld.factory.createTrigger({
shape: 'BOX',
dimensions: {x: 1000, y: 1000, z: 1}
dimensions: { x: 1000, y: 1000, z: 1 },
}));
destroyTrigger.position = {x: 0, y: 0, z: -15};
destroyTrigger.position = { x: 0, y: 0, z: -15 };
destroyTrigger.onEntityEntered.subscribe((entity: GgPositionable3dEntity) => {
world.removeEntity(entity, true);
});
Expand All @@ -64,42 +60,26 @@ export class AppComponent implements OnInit {
interval(500).subscribe(() => {
let item: Gg3dEntity;
if (Math.random() < 0.2) {
item = new Gg3dEntity(
world.visualScene.factory.createBox({x: 1, y: 1, z: 1}),
world.physicsWorld.factory.createRigidBody({
shape: {shape: 'BOX', dimensions: {x: 1, y: 1, z: 1}},
body: {mass: 1}
}),
);
item = world.addPrimitiveRigidBody({
shape: { shape: 'BOX', dimensions: { x: 1, y: 1, z: 1 } },
body: { mass: 1 },
});
} else if (Math.random() < 0.4) {
item = new Gg3dEntity(
world.visualScene.factory.createCapsule(0.5, 1),
world.physicsWorld.factory.createRigidBody({
shape: {shape: 'CAPSULE', radius: 0.5, centersDistance: 1},
body: {mass: 1}
}),
);
item = world.addPrimitiveRigidBody({
shape: { shape: 'CAPSULE', radius: 0.5, centersDistance: 1 },
body: { mass: 1 },
});
} else if (Math.random() < 0.6) {
item = new Gg3dEntity(
world.visualScene.factory.createCylinder(0.5, 1),
world.physicsWorld.factory.createRigidBody({
shape: {shape: 'CYLINDER', radius: 0.5, height: 1},
body: {mass: 1}
}),
);
item = world.addPrimitiveRigidBody({
shape: { shape: 'CYLINDER', radius: 0.5, height: 1 },
body: { mass: 1 },
});
} else if (Math.random() < 0.8) {
item = new Gg3dEntity(
world.visualScene.factory.createCone(0.5, 1),
world.physicsWorld.factory.createRigidBody({shape: {shape: 'CONE', radius: 0.5, height: 1}, body: {mass: 1}}),
);
item = world.addPrimitiveRigidBody({ shape: { shape: 'CONE', radius: 0.5, height: 1 }, body: { mass: 1 } });
} else {
item = new Gg3dEntity(
world.visualScene.factory.createSphere(0.5),
world.physicsWorld.factory.createRigidBody({shape: {shape: 'SPHERE', radius: 0.5}, body: {mass: 1}}),
);
item = world.addPrimitiveRigidBody({ shape: { shape: 'SPHERE', radius: 0.5 }, body: { mass: 1 } });
}
item.position = {x: Math.random() * 5 - 2.5, y: Math.random() * 5 - 2.5, z: 10};
world.addEntity(item);
item.position = { x: Math.random() * 5 - 2.5, y: Math.random() * 5 - 2.5, z: 10 };
});

world.start();
Expand Down
24 changes: 16 additions & 8 deletions examples/model-loader/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { DirectionalLight, HemisphereLight } from 'three';
import { createInlineTickController, Gg3dWorld, GgViewportManager, Mtrx4, Qtrn } from '@gg-web-engine/core';
import {
Camera3dAnimator,
createInlineTickController,
Gg3dWorld,
GgViewportManager,
Mtrx4,
Qtrn,
} from '@gg-web-engine/core';
import { interval } from 'rxjs';
import { Gg3dObject, Gg3dVisualScene, GgRenderer } from '@gg-web-engine/three';
import { Gg3dPhysicsWorld } from '@gg-web-engine/ammo';
Expand All @@ -25,14 +32,15 @@ export class AppComponent implements OnInit {
const canvas = await GgViewportManager.instance.createCanvas(1);
const renderer: GgRenderer = new GgRenderer(canvas);
world.addEntity(renderer);
createInlineTickController(world).subscribe(([elapsed, _]) => {
renderer.camera.position = {
x: 10 * Math.sin(elapsed / 2000),
y: 10 * Math.cos(elapsed / 2000),
const cameraController: Camera3dAnimator = new Camera3dAnimator(renderer.camera, (timeElapsed, _) => ({
position: {
x: 10 * Math.sin(timeElapsed / 2000),
y: 10 * Math.cos(timeElapsed / 2000),
z: 6,
};
renderer.camera.rotation = Qtrn.lookAt(renderer.camera.position, { x: 0, y: 0, z: 0 }, { x: 0, y: 0, z: 1 });
});
},
target: { x: 0, y: 0, z: 0 },
}));
world.addEntity(cameraController);
renderer.activate();

const hemiLight = new HemisphereLight(0xffffff, 0xffffff, 0.6);
Expand Down

0 comments on commit 0fe4b23

Please sign in to comment.