Skip to content

Commit cfd3ea5

Browse files
committed
fix(rapier): compute rotation and quaternion properly
1 parent 4c7292d commit cfd3ea5

File tree

2 files changed

+47
-18
lines changed

2 files changed

+47
-18
lines changed

libs/rapier/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"@angular/common": ">=19.0.0 <20.0.0",
2727
"@angular/core": ">=19.0.0 <20.0.0",
2828
"@dimforge/rapier3d-compat": "~0.14.0",
29-
"three": ">=0.148.0 <0.173.0"
29+
"three": ">=0.148.0 <0.173.0",
30+
"three-stdlib": "^2.0.0"
3031
},
3132
"dependencies": {
3233
"tslib": "^2.7.0"

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

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,35 @@ const colliderDefaultOptions: NgtrColliderOptions = {
5353
@Directive({ selector: 'ngt-object3D[collider]' })
5454
export class NgtrAnyCollider {
5555
position = input<NgtVector3 | undefined>([0, 0, 0]);
56-
rotation = input<NgtEuler | undefined>([0, 0, 0]);
56+
rotation = input<NgtEuler | undefined>();
5757
scale = input<NgtVector3 | undefined>([1, 1, 1]);
58-
quaternion = input<NgtQuaternion | undefined>([0, 0, 0, 1]);
58+
quaternion = input<NgtQuaternion | undefined>();
5959
userData = input<NgtThreeElements['ngt-object3D']['userData'] | undefined>(undefined);
6060
name = input<NgtThreeElements['ngt-object3D']['name'] | undefined>(undefined);
6161
options = input(colliderDefaultOptions, { transform: mergeInputs(rigidBodyDefaultOptions) });
6262

63-
private object3DParameters = computed(() => ({
64-
position: this.position(),
65-
rotation: this.rotation(),
66-
scale: this.scale(),
67-
quaternion: this.quaternion(),
68-
userData: this.userData(),
69-
name: this.name(),
70-
}));
63+
private object3DParameters = computed(() => {
64+
const [position, rotation, scale, quaternion, userData, name] = [
65+
this.position(),
66+
this.rotation(),
67+
this.scale(),
68+
this.quaternion(),
69+
this.userData(),
70+
this.name(),
71+
];
72+
73+
const parameters = { position, scale, userData, name };
74+
75+
if (quaternion) {
76+
Object.assign(parameters, { quaternion });
77+
} else if (rotation) {
78+
Object.assign(parameters, { rotation });
79+
} else {
80+
Object.assign(parameters, { rotation: [0, 0, 0] });
81+
}
82+
83+
return parameters;
84+
});
7185

7286
// TODO: change this to input required when Angular allows setting hostDirective input
7387
shape = model<NgtrColliderShape | undefined>(undefined, { alias: 'collider' });
@@ -429,13 +443,27 @@ export class NgtrRigidBody {
429443
userData = input<NgtThreeElements['ngt-object3D']['userData'] | undefined>(undefined);
430444
options = input(rigidBodyDefaultOptions, { transform: mergeInputs(rigidBodyDefaultOptions) });
431445

432-
private object3DParameters = computed(() => ({
433-
position: this.position(),
434-
rotation: this.rotation(),
435-
scale: this.scale(),
436-
quaternion: this.quaternion(),
437-
userData: this.userData(),
438-
}));
446+
private object3DParameters = computed(() => {
447+
const [position, rotation, scale, quaternion, userData] = [
448+
this.position(),
449+
this.rotation(),
450+
this.scale(),
451+
this.quaternion(),
452+
this.userData(),
453+
];
454+
455+
const parameters = { position, scale, userData };
456+
457+
if (quaternion) {
458+
Object.assign(parameters, { quaternion });
459+
} else if (rotation) {
460+
Object.assign(parameters, { rotation });
461+
} else {
462+
Object.assign(parameters, { rotation: [0, 0, 0] });
463+
}
464+
465+
return parameters;
466+
});
439467

440468
wake = output<void>();
441469
sleep = output<void>();

0 commit comments

Comments
 (0)