Skip to content
This repository has been archived by the owner on Jun 10, 2022. It is now read-only.

Commit

Permalink
Add physics stack demos (#584)
Browse files Browse the repository at this point in the history
  • Loading branch information
nik0la31 authored May 9, 2020
1 parent 3f3b131 commit 10bf4e6
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/functional-tests/src/tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import LibraryFailTest from './library-fail-test';
import LightTest from './light-test';
import LookAtTest from './look-at-test';
import PhysicsBounceTest from './physics-bounce-test';
import PhysicsStackTest from './physics-stack-test';
import PhysicsFrictionTest from './physics-friction-test';
import PhysicsSimTest from './physics-sim-test';
import PhysicsCollisionTest from './physics-collision'
Expand Down Expand Up @@ -81,6 +82,12 @@ export const Factories = {
'light': (...args) => new LightTest(...args),
'look-at': (...args) => new LookAtTest(...args),
'physics-bounce': (...args) => new PhysicsBounceTest(...args),
'physics-stack-2 boxes': (...args) => new PhysicsStackTest(2, 0.5, false, ...args),
'physics-stack-2 boxes mix': (...args) => new PhysicsStackTest(2, 0.5, true, ...args),
'physics-stack-2 large boxes mix': (...args) => new PhysicsStackTest(2, 1.0, true, ...args),
'physics-stack-4 boxes': (...args) => new PhysicsStackTest(4, 0.5, false, ...args),
'physics-stack-4 boxes mix': (...args) => new PhysicsStackTest(4, 0.5, true, ...args),
'physics-stack-4 large boxes mix': (...args) => new PhysicsStackTest(4, 1.0, true, ...args),
'physics-firction': (...args) => new PhysicsFrictionTest(...args),
'physics-sim': (...args) => new PhysicsSimTest(...args),
'physics-free-fall': (...args) => new PhysichFreeFallTest(...args),
Expand Down
123 changes: 123 additions & 0 deletions packages/functional-tests/src/tests/physics-stack-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*!
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/

import * as MRE from '@microsoft/mixed-reality-extension-sdk';

import { App } from '../app';
import { Test } from '../test';
import { Vector3 } from '@microsoft/mixed-reality-extension-sdk';
import { int } from '../../../common/src/math/types';

export default class PhysicsStackTest extends Test {

public expectedResultDescription = "Stack of rigid body boxes.";
private assets: MRE.AssetContainer;
private interval: NodeJS.Timeout;

private materials: MRE.Material[];

private numBoxes: number;
private numOwners: number;
private boxSize: number;

constructor(numBoxes: number, boxSize: number, isMixedOwnership: boolean,
protected app: App, protected baseUrl: string, protected user: MRE.User) {
super(app, baseUrl, user);

this.assets = new MRE.AssetContainer(this.app.context);

this.materials = [
this.assets.createMaterial('red', { color: MRE.Color3.Red()}),
this.assets.createMaterial('blue', { color: MRE.Color3.Blue()}),
this.assets.createMaterial('green', { color: MRE.Color3.Green()}),
];

this.numBoxes = numBoxes;
this.boxSize = boxSize;
this.numOwners = isMixedOwnership ? 2 : 1;
}

public async run(root: MRE.Actor): Promise<boolean> {

for(let i = 0; i<this.app.context.users.length; i++) {
const index = Math.min(i, this.materials.length-1);
this.createLabel(root, this.materials[index], this.app.context.users[i].id);

if (i === 0) {
this.createCube(root, this.boxSize, new Vector3(1.2, this.boxSize * 0.5, -1),
this.app.context.users[i].id, this.materials[i]);
} else if (i === 1) {
this.createCube(root, this.boxSize, new Vector3(-1.2, this.boxSize * 0.5, -1),
this.app.context.users[i].id, this.materials[i]);
}
}

const numUsers = Math.min(this.numOwners, this.app.context.users.length);
this.createStack(root, this.boxSize, this.numBoxes, numUsers, this.app.context.users, this.materials);

await this.stoppedAsync();
return true;
}

public cleanup() {
clearInterval(this.interval);
this.assets.unload();
}

private createLabel(root: MRE.Actor, material: MRE.Material, userId: MRE.Guid) {
MRE.Actor.Create(this.app.context, {
actor: {
name: 'label',
parentId: root.id,
exclusiveToUser: userId,
transform: { local: { position: { y: 3.5 } } },
text: {
contents: material.name,
height: 0.5,
anchor: MRE.TextAnchorLocation.TopCenter,
color: material.color
}
}
});
}

private createStack(root: MRE.Actor, size: number, count: int,
numUsers: int, users: MRE.User[], materials: MRE.Material[]) {

const position = new Vector3(0, size * 0.5, -1);

for(let i = 0; i<count;i++) {
const userIndex = i % numUsers;
this.createCube(root, size, position, users[userIndex].id, materials[userIndex]);

position.y += size;
}
}

private createCube(root: MRE.Actor, size: number, position: Vector3, userId: MRE.Guid, material: MRE.Material) {
MRE.Actor.Create(this.app.context, {
actor: {
owner: userId,
parentId: root.id,
name: "box",
grabbable: true,
appearance: {
meshId: this.assets.createBoxMesh('box', size, size, size).id,
materialId: material.id
},
transform: {
local: { position: position }
},
rigidBody: {
mass: 1,
},
collider: {
geometry: { shape: MRE.ColliderType.Auto },
bounciness: 0.0, dynamicFriction: 0.5, staticFriction: 0.5
}
}
});
}
}

0 comments on commit 10bf4e6

Please sign in to comment.