Skip to content

Commit

Permalink
fix: increase and decrease the matterport circle when a participant e…
Browse files Browse the repository at this point in the history
…nters or leave the room
  • Loading branch information
carlossantos74 committed Dec 4, 2024
1 parent ec99c92 commit f26ab8c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 28 deletions.
1 change: 1 addition & 0 deletions packages/matterport/src/common/types/coordinates.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export interface Simple2DPoint {
}

export type Coordinates = Simple2DPoint & { z: number }
export type CirclePosition = Coordinates & { slot: number }
69 changes: 41 additions & 28 deletions packages/matterport/src/services/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { isEqual } from 'lodash';
import { Vector3, Quaternion } from 'three';

import { Avatar, AvatarsConstants, Name } from '../common/types/avatars.types';
import { Coordinates, DefaultCoordinates, Simple2DPoint } from '../common/types/coordinates.types';
import { CirclePosition, Coordinates, DefaultCoordinates, Simple2DPoint } from '../common/types/coordinates.types';
import { Laser } from '../common/types/lasers.types';
import type { MpSdk as Matterport, Rotation } from '../common/types/matterport.types';
import { Logger } from '../common/utils/logger';
Expand Down Expand Up @@ -64,7 +64,7 @@ export class Presence3D {
private currentLocalFloorId: number;
private currentLocalMode: Matterport.Mode.Mode;
private currentLocalLaserDest: Coordinates = DefaultCoordinates;
private circlePositions: Coordinates[] = [];
private circlePositions: CirclePosition[] = [];
private currentSweepId: string;

private THREE;
Expand Down Expand Up @@ -475,6 +475,8 @@ export class Presence3D {
if (unsubscribe) {
this.presence3DManager?.unsubscribeFromUpdates(participant.id, this.onParticipantUpdated);
}

this.createCircleOfPositions();
};

private addParticipant = async (participant): Promise<void> => {
Expand Down Expand Up @@ -506,6 +508,8 @@ export class Presence3D {
this.config.isAvatarsEnabled && (await this.createAvatar(participantOn3D));
this.config.isLaserEnabled && this.createLaser(participantOn3D);
this.config.isNameEnabled && this.createName(participantOn3D, this.avatars[participant.id]);

this.createCircleOfPositions();
};

/**
Expand Down Expand Up @@ -889,58 +893,67 @@ export class Presence3D {
if (!this.presence3DManager) {
return position;
}

this.localSlot = this.localParticipant.slot?.index ?? -1;

if (!this.THREE || this.localSlot === -1) {
return position;
}

const calculatedPos = new this.THREE.Vector3(position?.x, position?.y, position?.z);
if (!this.circlePositions[this.localSlot]) {
const positionInTheCircle = this.circlePositions.find(
(position) => position.slot === this.localSlot,
);

if (!positionInTheCircle) {
return position;
}

if (!this.currentCirclePosition?.isVector3) {
this.currentCirclePosition = new this.THREE.Vector3(
this.circlePositions[this.localSlot].x,
positionInTheCircle.x,
position.y,
this.circlePositions[this.localSlot].z,
positionInTheCircle.z,
);
}

this.currentCirclePosition.set(
this.circlePositions[this.localSlot].x,
positionInTheCircle.x,
position.y,
this.circlePositions[this.localSlot].z,
positionInTheCircle.z,
);

calculatedPos.add(
this.currentCirclePosition.multiplyScalar(AvatarsConstants.DISTANCE_BETWEEN_AVATARS),
);

return { x: calculatedPos.x, y: position.y, z: calculatedPos.z };
};

private createCircleOfPositions(): void {
const amountOfPeople = 50;
let radiusOfCircle = amountOfPeople * 0.3;
this.circlePositions = [];
const participants = [
...Object.values(this.participants),
this.localParticipant,
].sort((a, b) => {
return (a.slot?.index || 0) - (b.slot?.index || 0);
});

let degrees = 0;
const participantCount = participants.length;
if (participantCount === 0) return;

// minimum radius
if (radiusOfCircle < 2) {
radiusOfCircle = 2;
}
const radius = Math.max(participantCount * 0.3, 2);
const angleStep = (2 * Math.PI) / participantCount;

for (let i = 0; i < amountOfPeople; i++) {
degrees = i * (360 / amountOfPeople);
const radian = degrees * (Math.PI / 180);
const x = radiusOfCircle * Math.cos(radian);
const y = 0;
const z = radiusOfCircle * Math.sin(radian);
if (i !== 0) {
const pos = { x, y, z };
if (i % 2 === 0) {
this.circlePositions.push(pos);
} else {
this.circlePositions.unshift(pos);
}
}
for (let i = 0; i < participantCount; i++) {
const angle = i * angleStep;
const x = radius * Math.cos(angle);
const z = radius * Math.sin(angle);
this.circlePositions.push({ x, y: 0, z, slot: participants[i]?.slot?.index ?? -1 });
}

this.adjustMyPositionToCircle(this.currentLocalPosition);
this.logger.log('Updated circle positions:', this.circlePositions);
}

private onParticipantsUpdated = (participants) => {
Expand Down

0 comments on commit f26ab8c

Please sign in to comment.