Skip to content

Commit

Permalink
updated entity constructor args (#21)
Browse files Browse the repository at this point in the history
* changed IEntity constructor interface

* updated examples
  • Loading branch information
AndyGura authored Nov 29, 2024
1 parent e4f8200 commit ed4d80b
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 64 deletions.
20 changes: 11 additions & 9 deletions examples/collision-groups-pool-three-ammo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,17 @@ world.init().then(async () => {
}

for (let i = 0; i < 4; i++) {
let wall = new Entity3d(null, world.physicsWorld.factory.createRigidBody({
shape: { shape: 'BOX', dimensions: { x: 40, y: 40, z: 400 } },
body: {
dynamic: false,
ownCollisionGroups: [ballsCommonCg],
interactWithCollisionGroups: [ballsCommonCg],
restitution: 0.3,
},
}));
let wall = new Entity3d({
objectBody: world.physicsWorld.factory.createRigidBody({
shape: { shape: 'BOX', dimensions: { x: 40, y: 40, z: 400 } },
body: {
dynamic: false,
ownCollisionGroups: [ballsCommonCg],
interactWithCollisionGroups: [ballsCommonCg],
restitution: 0.3,
},
}),
});
wall.position = Pnt3.rotAround({ x: 28, y: 0, z: 0 }, Pnt3.Z, Math.PI * i / 2);
world.addEntity(wall);
}
Expand Down
20 changes: 11 additions & 9 deletions examples/collision-groups-pool-three-rapier3d/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,17 @@ world.init().then(async () => {
}

for (let i = 0; i < 4; i++) {
let wall = new Entity3d(null, world.physicsWorld.factory.createRigidBody({
shape: { shape: 'BOX', dimensions: { x: 40, y: 40, z: 400 } },
body: {
dynamic: false,
ownCollisionGroups: [ballsCommonCg],
interactWithCollisionGroups: [ballsCommonCg],
restitution: 0.3,
},
}));
let wall = new Entity3d({
objectBody: world.physicsWorld.factory.createRigidBody({
shape: { shape: 'BOX', dimensions: { x: 40, y: 40, z: 400 } },
body: {
dynamic: false,
ownCollisionGroups: [ballsCommonCg],
interactWithCollisionGroups: [ballsCommonCg],
restitution: 0.3,
},
}),
});
wall.position = Pnt3.rotAround({ x: 28, y: 0, z: 0 }, Pnt3.Z, Math.PI * i / 2);
world.addEntity(wall);
}
Expand Down
37 changes: 21 additions & 16 deletions packages/core/src/2d/entities/entity-2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export class Entity2d<
this._rotation = value;
}

public readonly object2D: VTypeDoc['displayObject'] | null = null;
public readonly objectBody: PTypeDoc['rigidBody'] | null = null;

public updateVisibility(): void {
if (this.object2D) {
this.object2D.visible = this.worldVisible;
Expand All @@ -63,25 +66,27 @@ export class Entity2d<
this._rotation = quat;
}

constructor(
public readonly object2D: VTypeDoc['displayObject'] | null,
public readonly objectBody: PTypeDoc['rigidBody'] | null,
) {
constructor(options: { object2D?: VTypeDoc['displayObject']; objectBody?: PTypeDoc['rigidBody'] }) {
super();
if (objectBody) {
if (options.objectBody) {
this.objectBody = options.objectBody;
this.name = this.objectBody.name;
this.addComponents(this.objectBody);
}
if (options.object2D) {
this.object2D = options.object2D;
if (!options.objectBody) {
this._position = this.object2D.position;
this._rotation = this.object2D.rotation;
this.name = this.object2D.name;
}
this.addComponents(this.object2D);
}
if (this.object2D && this.objectBody) {
this.tick$.subscribe(() => {
this.runTransformBinding(objectBody, object2D);
this.runTransformBinding(this.objectBody!, this.object2D);
});
this.runTransformBinding(objectBody, object2D);
this.name = objectBody.name;
} else if (object2D) {
this._position = object2D.position;
this._rotation = object2D.rotation;
this.name = object2D.name;
} else {
throw new Error('Cannot create entity without an object2D and a body');
this.runTransformBinding(this.objectBody, this.object2D);
}
objectBody && this.addComponents(objectBody);
object2D && this.addComponents(object2D);
}
}
8 changes: 4 additions & 4 deletions packages/core/src/2d/gg-2d-world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ export class Gg2dWorld<
rotation: number = 0,
material: DisplayObject2dOpts<VTypeDoc['texture']> = {},
): Entity2d<VTypeDoc, PTypeDoc> {
const entity = new Entity2d<VTypeDoc, PTypeDoc>(
this.visualScene.factory.createPrimitive(descr.shape, material),
this.physicsWorld.factory.createRigidBody(descr),
);
const entity = new Entity2d<VTypeDoc, PTypeDoc>({
object2D: this.visualScene.factory.createPrimitive(descr.shape, material),
objectBody: this.physicsWorld.factory.createRigidBody(descr),
});
entity.position = position;
entity.rotation = rotation;
this.addEntity(entity);
Expand Down
37 changes: 21 additions & 16 deletions packages/core/src/3d/entities/entity-3d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ export class Entity3d<
this._rotation = value;
}

public readonly object3D: VTypeDoc['displayObject'] | null = null;
public readonly objectBody: PTypeDoc['rigidBody'] | null = null;

public updateVisibility(): void {
if (this.object3D) {
this.object3D.visible = this.worldVisible;
Expand All @@ -65,25 +68,27 @@ export class Entity3d<
this._rotation = quat;
}

constructor(
public readonly object3D: VTypeDoc['displayObject'] | null,
public readonly objectBody: PTypeDoc['rigidBody'] | null = null,
) {
constructor(options: { object3D?: VTypeDoc['displayObject'] | null; objectBody?: PTypeDoc['rigidBody'] | null }) {
super();
if (objectBody) {
if (options.objectBody) {
this.objectBody = options.objectBody;
this.name = this.objectBody.name;
this.addComponents(this.objectBody);
}
if (options.object3D) {
this.object3D = options.object3D;
if (!options.objectBody) {
this._position = this.object3D.position;
this._rotation = this.object3D.rotation;
this.name = this.object3D.name;
}
this.addComponents(this.object3D);
}
if (this.object3D && this.objectBody) {
this.tick$.subscribe(() => {
this.runTransformBinding(objectBody, object3D);
this.runTransformBinding(this.objectBody!, this.object3D);
});
this.runTransformBinding(objectBody, object3D);
this.name = objectBody.name;
} else if (object3D) {
this._position = object3D.position;
this._rotation = object3D.rotation;
this.name = object3D.name;
} else {
throw new Error('Cannot create entity without a mesh and a body');
this.runTransformBinding(this.objectBody, this.object3D);
}
objectBody && this.addComponents(objectBody);
object3D && this.addComponents(object3D);
}
}
10 changes: 5 additions & 5 deletions packages/core/src/3d/entities/raycast-vehicle-3d.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export class RaycastVehicle3dEntity<
public readonly chassis3D: IDisplayObject3dComponent | null,
public readonly vehicleComponent: IRaycastVehicleComponent,
) {
super(chassis3D, vehicleComponent);
super({ object3D: chassis3D, objectBody: vehicleComponent });
let wheelFullOptions: (WheelOptions & { display: WheelDisplayOptions })[] =
'wheelBase' in carProperties
? [
Expand Down Expand Up @@ -184,9 +184,9 @@ export class RaycastVehicle3dEntity<
this.wheelLocalRotation.push(null);
continue;
}
const entity = display.displayObject.clone();
const displayObj = display.displayObject.clone();
if (display.autoScaleMesh) {
const boundingBox = Box.expandByPoint(entity.getBoundings(), Pnt3.O);
const boundingBox = Box.expandByPoint(displayObj.getBoundings(), Pnt3.O);
const scale = { ...Pnt3.O };
const wheelObjectDirection = display.wheelObjectDirection || 'x';
for (const dir of ['x', 'y', 'z'] as (keyof Point3)[]) {
Expand All @@ -195,7 +195,7 @@ export class RaycastVehicle3dEntity<
? options.tyreWidth / (boundingBox.max[dir] - boundingBox.min[dir])
: (options.tyreRadius * 2) / (boundingBox.max[dir] - boundingBox.min[dir]);
}
entity.scale = scale;
displayObj.scale = scale;
}
const direction = display.wheelObjectDirection || 'x';
const flip = direction.includes('-') ? options.isLeft : !options.isLeft;
Expand All @@ -211,7 +211,7 @@ export class RaycastVehicle3dEntity<
localRotation = { x: 0, y: 0.707107 * (flip ? 1 : -1), z: 0, w: 0.707107 };
}
this.wheelLocalRotation.push(localRotation);
const wheelEntity = new Entity3d<VTypeDoc, PTypeDoc>(entity);
const wheelEntity = new Entity3d<VTypeDoc, PTypeDoc>({ object3D: displayObj });
wheelEntity.name = this.name + '__wheel_' + (options.isFront ? 'f' : 'r') + (options.isLeft ? 'l' : 'r');
this.wheels.push(wheelEntity);
}
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/3d/gg-3d-world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ export class Gg3dWorld<
rotation: Point4 = Qtrn.O,
material: DisplayObject3dOpts<VTypeDoc['texture']> = {},
): Entity3d<VTypeDoc, PTypeDoc> {
const entity = new Entity3d<VTypeDoc, PTypeDoc>(
this.visualScene.factory.createPrimitive(descr.shape, material),
this.physicsWorld.factory.createRigidBody(descr),
);
const entity = new Entity3d<VTypeDoc, PTypeDoc>({
object3D: this.visualScene.factory.createPrimitive(descr.shape, material),
objectBody: this.physicsWorld.factory.createRigidBody(descr),
});
entity.position = position;
entity.rotation = rotation;
this.addEntity(entity);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/3d/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export class Gg3dLoader<
const loadOptions = { ...defaultLoadOptions, ...options };
const { resources, meta } = await this.loadGgGlbResources(path, loadOptions.cachingStrategy);
const result: LoadResultWithProps<VTypeDoc, PTypeDoc> = {
entities: resources.map(({ object3D, body }) => new Entity3d(object3D, body)),
entities: resources.map(x => new Entity3d(x)),
meta,
};
if (loadOptions.loadProps) {
Expand Down

0 comments on commit ed4d80b

Please sign in to comment.