Skip to content

Commit

Permalink
Fix 3D character to keep rotation around X and Y intact
Browse files Browse the repository at this point in the history
  • Loading branch information
D8H committed Jan 17, 2025
1 parent 64771c6 commit 0308c33
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions Extensions/Physics3DBehavior/PhysicsCharacter3DRuntimeBehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,21 @@ namespace gdjs {
return result;
}

getPhysicsRotation(result: Jolt.Quat): Jolt.Quat {
// Characters body should not rotate around X and Y.
const rotation = result.sEulerAngles(
this.getVec3(0, 0, gdjs.toRad(this.owner3D.getAngle()))
);
result.Set(
rotation.GetX(),
rotation.GetY(),
rotation.GetZ(),
rotation.GetW()
);
Jolt.destroy(rotation);
return result;
}

moveObjectToPhysicsPosition(physicsPosition: Jolt.RVec3): void {
const { behavior } = this.getPhysics3D();
this.owner3D.setCenterXInScene(
Expand All @@ -346,6 +361,19 @@ namespace gdjs {
);
}

moveObjectToPhysicsRotation(physicsRotation: Jolt.Quat): void {
const threeObject = this.owner3D.get3DRendererObject();
threeObject.quaternion.x = physicsRotation.GetX();
threeObject.quaternion.y = physicsRotation.GetY();
threeObject.quaternion.z = physicsRotation.GetZ();
threeObject.quaternion.w = physicsRotation.GetW();
// TODO Avoid this instantiation
const euler = new THREE.Euler(0, 0, 0, 'ZYX');
euler.setFromQuaternion(threeObject.quaternion);
// No need to update the rotation for X and Y as CharacterVirtual doesn't change it.
this.owner3D.setAngle(gdjs.toDegrees(euler.z));
}

onDeActivate() {
this.collisionChecker.clearContacts();
}
Expand Down Expand Up @@ -1495,7 +1523,6 @@ namespace gdjs {
}

updateObjectFromBody() {
const { behavior } = this.characterBehavior.getPhysics3D();
const { character } = this.characterBehavior;
if (!character) {
return;
Expand All @@ -1504,8 +1531,9 @@ namespace gdjs {
this.characterBehavior.moveObjectToPhysicsPosition(
character.GetPosition()
);
// TODO No need to update the rotation for X and Y as CharacterVirtual doesn't change it.
behavior.moveObjectToPhysicsRotation(character.GetRotation());
this.characterBehavior.moveObjectToPhysicsRotation(
character.GetRotation()
);
}

updateBodyFromObject() {
Expand All @@ -1528,7 +1556,9 @@ namespace gdjs {
) {
// TODO No need to update the rotation for X and Y as CharacterVirtual doesn't change it.
character.SetRotation(
behavior.getPhysicsRotation(_sharedData.getQuat(0, 0, 0, 1))
this.characterBehavior.getPhysicsRotation(
_sharedData.getQuat(0, 0, 0, 1)
)
);
}
}
Expand Down

0 comments on commit 0308c33

Please sign in to comment.