Skip to content

Commit

Permalink
#6 usePhysics createPhysicsSolver
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaspietravallo committed Jul 15, 2022
1 parent 689b767 commit bf93339
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 17 deletions.
4 changes: 2 additions & 2 deletions coverage/coverage-summary.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{"total": {"lines":{"total":915,"covered":836,"skipped":0,"pct":91.37},"statements":{"total":981,"covered":896,"skipped":0,"pct":91.34},"functions":{"total":216,"covered":200,"skipped":0,"pct":92.59},"branches":{"total":564,"covered":452,"skipped":0,"pct":80.14}}
,"/Users/tomaspietravallo/Documents/GitHub/sparkar-volts/volts.ts": {"lines":{"total":915,"covered":836,"skipped":0,"pct":91.37},"functions":{"total":216,"covered":200,"skipped":0,"pct":92.59},"statements":{"total":981,"covered":896,"skipped":0,"pct":91.34},"branches":{"total":564,"covered":452,"skipped":0,"pct":80.14}}
{"total": {"lines":{"total":922,"covered":843,"skipped":0,"pct":91.43},"statements":{"total":988,"covered":903,"skipped":0,"pct":91.4},"functions":{"total":219,"covered":203,"skipped":0,"pct":92.69},"branches":{"total":571,"covered":461,"skipped":0,"pct":80.74}}
,"/Users/tomaspietravallo/Documents/GitHub/sparkar-volts/volts.ts": {"lines":{"total":922,"covered":843,"skipped":0,"pct":91.43},"functions":{"total":219,"covered":203,"skipped":0,"pct":92.69},"statements":{"total":988,"covered":903,"skipped":0,"pct":91.4},"branches":{"total":571,"covered":461,"skipped":0,"pct":80.74}}
}
25 changes: 24 additions & 1 deletion tests/object3d.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('constructor', () => {
});
});

describe('fetch reactive values', () => {
describe('reactive values', () => {
test('from scene obj', async () => {
const Instance = World.getInstance({ mode: 'DEV' });
jest.advanceTimersByTime(100);
Expand All @@ -52,6 +52,7 @@ describe('fetch reactive values', () => {
obj.pos = new Vector(1, 2, 3);
obj.rot = new Quaternion(1, 0, 0, 0);
obj.update({ pos: true, rot: true });
obj.update({ pos: false, rot: false });
expect(obj.pos.values).toEqual([1, 2, 3]);
expect(obj.rot.values).toEqual([1, 0, 0, 0]);
});
Expand Down Expand Up @@ -93,9 +94,31 @@ describe('utils', () => {
const obj3d = new Object3D(sceneObj);
expect(Object3D.createDebugMaterial()).resolves.not.toThrow();
});

test('destroyDynamicBody', () => {
expect(() => new Object3D('JEST_DYNAMIC_INSTANCE').destroyDynamicBody()).not.toThrow();
expect(() => new Object3D().destroyDynamicBody()).not.toThrow();
expect(() => new Object3D(null).destroyDynamicBody()).toThrow();
});
});

describe('physics', () => {
test('createPhysicsSolver', () => {
expect(Object3D.createPhysicsSolver).not.toThrow();
});

test('usePhysics', () => {
const obj = new Object3D();
// test || [].push
new Object3D().usePhysics();
expect(() => obj.usePhysics()).not.toThrow();
expect( obj.Solver ).toBeDefined();
expect( obj.Solver ).not.toThrow();
const spy = jest.spyOn(obj, 'Solver').mockImplementation();
expect(() => obj.update({ solver: true }) ).not.toThrow();
expect(spy).toHaveBeenCalledTimes(1);
spy.mockReset();
spy.mockRestore();
});

})
44 changes: 30 additions & 14 deletions volts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1884,6 +1884,7 @@ export class State<Data extends { [key: string]: Vector<any> | Quaternion | numb
//#endregion

//#region Object3D
const DefaultPhysicsSettings = { solver: 'verlet', steps: 1, drag: 1.0, gravity: -9.7, floor: -0.5, group: 0xffff } as Parameters<typeof Object3D["prototype"]["usePhysics"]>[0];

export enum SceneObjectClassNames {
'Plane' = 'Plane',
Expand Down Expand Up @@ -1917,6 +1918,8 @@ export class Object3D<T extends SceneObjectBase = any> {
awake: boolean;
body: IfEquals<any, T, Promise<SceneObjectBase>, T>;
material: MaterialBase | undefined;
Solver: (obj: Object3D ) => void;
static colliders: { [key: string]: Object3D[] } = {};

constructor(body?: T) {
(this.pos = new Vector()),
Expand Down Expand Up @@ -1973,7 +1976,8 @@ export class Object3D<T extends SceneObjectBase = any> {
return this;
}

update({ pos, rot }: { pos?: boolean; rot?: boolean } = {}): void {
update({ pos, rot, solver }: { pos?: boolean; rot?: boolean, solver?: boolean } = {}): void {
if (solver && this.Solver) this.Solver(this);
if (pos) this.pos.setSignalComponents();
if (rot) this.rot.setSignalComponents();
}
Expand All @@ -1993,8 +1997,16 @@ export class Object3D<T extends SceneObjectBase = any> {
return this;
}

usePhysics(args = { solver: 'verlet', steps: 1, drag: 1.0, gravity: -9.7, floor: -0.5 }) {
/** .. */
usePhysics(args: {
solver?: 'verlet' | 'none',
steps?: number,
drag?: number,
gravity?: number,
floor?: number,
group?: number,
} = DefaultPhysicsSettings) {
this.Solver = Object3D.createPhysicsSolver(args);
(Object3D.colliders[args.group] = Object3D.colliders[args.group] || []).push(this);
}

bindMesh(sceneObjectBase: SceneObjectBase): Object3D {
Expand Down Expand Up @@ -2030,17 +2042,6 @@ export class Object3D<T extends SceneObjectBase = any> {
throw new Error(`Volts.Object3D.destroyDynamicBody: Function called on a non-dynamic bodied object`);
}

static async createDebugMaterial(hue?: number): Promise<MaterialBase> {
if (hue === undefined) hue = 0;
return Materials.create(MaterialClassNames.DefaultMaterial, {
opacity: 1.0,
blendMode: 'ALPHA',
doubleSided: true,
}).then((m) => {
return m.setTextureSlot('DIFFUSE', Reactive.pack4(...hsv2rgb(hue, 1, 1), 1) as any), m;
});
}

/**
* @description *Not available for Block assets*
*/
Expand Down Expand Up @@ -2085,6 +2086,21 @@ export class Object3D<T extends SceneObjectBase = any> {
// @ts-expect-error
this.body.then ? this.body.then((b) => set(b)) : set(this.body);
}

static async createDebugMaterial(hue?: number): Promise<MaterialBase> {
if (hue === undefined) hue = 0;
return Materials.create(MaterialClassNames.DefaultMaterial, {
opacity: 1.0,
blendMode: 'ALPHA',
doubleSided: true,
}).then((m) => {
return m.setTextureSlot('DIFFUSE', Reactive.pack4(...hsv2rgb(hue, 1, 1), 1) as any), m;
});
}

static createPhysicsSolver( args: Parameters<typeof Object3D["prototype"]["usePhysics"]>[0] ) {
return () => { /** ... */}
}
}

//#endregion
Expand Down

0 comments on commit bf93339

Please sign in to comment.