Skip to content

Commit 35c2504

Browse files
committed
fix(rapier): adjust rigidbody and collider input types
1 parent ff64753 commit 35c2504

File tree

3 files changed

+43
-26
lines changed

3 files changed

+43
-26
lines changed

libs/rapier/src/lib/instanced-rigid-bodies.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ const defaultOptions: NgtrRigidBodyOptions = rigidBodyDefaultOptions;
8383
imports: [NgtrRigidBody, NgtrAnyCollider],
8484
})
8585
export class NgtrInstancedRigidBodies {
86-
position = input<NgtVector3 | undefined>([0, 0, 0]);
87-
rotation = input<NgtEuler | undefined>([0, 0, 0]);
88-
scale = input<NgtVector3 | undefined>([1, 1, 1]);
89-
quaternion = input<NgtQuaternion | undefined>([0, 0, 0, 1]);
90-
userData = input<NgtThreeElements['ngt-object3D']['userData'] | undefined>({});
86+
position = input<NgtVector3>([0, 0, 0]);
87+
rotation = input<NgtEuler>();
88+
scale = input<NgtVector3>([1, 1, 1]);
89+
quaternion = input<NgtQuaternion>();
90+
userData = input<NgtThreeElements['ngt-object3D']['userData']>();
9191
instances = input([], {
9292
alias: 'instancedRigidBodies',
9393
transform: (value: Array<NgtrInstancedRigidBodyOptions> | '') => {
@@ -98,13 +98,25 @@ export class NgtrInstancedRigidBodies {
9898
options = input(defaultOptions, { transform: mergeInputs(defaultOptions) });
9999

100100
private object3DParameters = computed(() => {
101-
return {
102-
position: this.position(),
103-
rotation: this.rotation(),
104-
scale: this.scale(),
105-
quaternion: this.quaternion(),
106-
userData: this.userData(),
107-
};
101+
const [position, rotation, scale, quaternion, userData] = [
102+
this.position(),
103+
this.rotation(),
104+
this.scale(),
105+
this.quaternion(),
106+
this.userData(),
107+
];
108+
109+
const parameters = { position, scale, userData };
110+
111+
if (quaternion) {
112+
Object.assign(parameters, { quaternion });
113+
} else if (rotation) {
114+
Object.assign(parameters, { rotation });
115+
} else {
116+
Object.assign(parameters, { rotation: [0, 0, 0] });
117+
}
118+
119+
return parameters;
108120
});
109121

110122
private instanceWrapperRef = viewChild.required<ElementRef<THREE.Object3D>>('instanceWrapper');
@@ -156,7 +168,11 @@ export class NgtrInstancedRigidBodies {
156168
},
157169
},
158170
key: `${instance.key}-${index}` + `${instancedMesh?.uuid || ''}`,
159-
}) as Omit<NgtrInstancedRigidBodyOptions, 'options'> & { options: Partial<NgtrRigidBodyOptions> },
171+
}) as Omit<NgtrInstancedRigidBodyOptions, 'options' | 'position' | 'scale'> & {
172+
position: NgtVector3;
173+
scale: NgtVector3;
174+
options: Partial<NgtrRigidBodyOptions>;
175+
},
160176
);
161177
});
162178

libs/rapier/src/lib/mesh-collider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import { createColliderOptions } from './utils';
3535
imports: [NgtrAnyCollider],
3636
})
3737
export class NgtrMeshCollider {
38-
colliders = input.required<NgtrRigidBodyAutoCollider>({ alias: 'ngtrMeshCollider' });
38+
colliders = input.required<NgtrRigidBodyAutoCollider>({ alias: 'meshCollider' });
3939

4040
objectRef = inject<ElementRef<THREE.Object3D>>(ElementRef);
4141
private rigidBody = inject(NgtrRigidBody);

libs/rapier/src/lib/rigid-body.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ const colliderDefaultOptions: NgtrColliderOptions = {
5252

5353
@Directive({ selector: 'ngt-object3D[collider]' })
5454
export class NgtrAnyCollider {
55-
position = input<NgtVector3 | undefined>([0, 0, 0]);
56-
rotation = input<NgtEuler | undefined>();
57-
scale = input<NgtVector3 | undefined>([1, 1, 1]);
58-
quaternion = input<NgtQuaternion | undefined>();
59-
userData = input<NgtThreeElements['ngt-object3D']['userData'] | undefined>(undefined);
60-
name = input<NgtThreeElements['ngt-object3D']['name'] | undefined>(undefined);
55+
position = input<NgtVector3>([0, 0, 0]);
56+
rotation = input<NgtEuler>();
57+
scale = input<NgtVector3>([1, 1, 1]);
58+
quaternion = input<NgtQuaternion>();
59+
userData = input<NgtThreeElements['ngt-object3D']['userData']>();
60+
name = input<NgtThreeElements['ngt-object3D']['name']>();
6161
options = input(colliderDefaultOptions, { transform: mergeInputs(rigidBodyDefaultOptions) });
6262

6363
private object3DParameters = computed(() => {
@@ -70,7 +70,7 @@ export class NgtrAnyCollider {
7070
this.name(),
7171
];
7272

73-
const parameters = { position, scale, userData, name };
73+
const parameters = { position, scale, userData, name: name || `${untracked(this.shape)}-${Date.now()}` };
7474

7575
if (quaternion) {
7676
Object.assign(parameters, { quaternion });
@@ -85,6 +85,7 @@ export class NgtrAnyCollider {
8585

8686
// TODO: change this to input required when Angular allows setting hostDirective input
8787
shape = model<NgtrColliderShape | undefined>(undefined, { alias: 'collider' });
88+
// NOTE: this will be typed by individual collider
8889
args = model<unknown[]>([]);
8990

9091
collisionEnter = output<NgtrCollisionEnterPayload>();
@@ -442,11 +443,11 @@ export class NgtrRigidBody {
442443
return value;
443444
},
444445
});
445-
position = input<NgtVector3 | undefined>([0, 0, 0]);
446-
rotation = input<NgtEuler | undefined>([0, 0, 0]);
447-
scale = input<NgtVector3 | undefined>([1, 1, 1]);
448-
quaternion = input<NgtQuaternion | undefined>([0, 0, 0, 1]);
449-
userData = input<NgtThreeElements['ngt-object3D']['userData'] | undefined>(undefined);
446+
position = input<NgtVector3>([0, 0, 0]);
447+
rotation = input<NgtEuler>();
448+
scale = input<NgtVector3>([1, 1, 1]);
449+
quaternion = input<NgtQuaternion>();
450+
userData = input<NgtThreeElements['ngt-object3D']['userData']>();
450451
options = input(rigidBodyDefaultOptions, { transform: mergeInputs(rigidBodyDefaultOptions) });
451452

452453
private object3DParameters = computed(() => {

0 commit comments

Comments
 (0)