Skip to content

Commit

Permalink
Fix velocity setters and thin shapes.
Browse files Browse the repository at this point in the history
  • Loading branch information
D8H committed Nov 13, 2024
1 parent e2ae7c3 commit 3b45463
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
4 changes: 1 addition & 3 deletions Extensions/Physics3DBehavior/JsExtension.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,9 +526,7 @@ module.exports = {
'Physics3DBehavior',
_('3D Physics Engine'),
'Physics3D',
_(
'Simulate realistic object physics with gravity, forces, etc.'
),
_('Simulate realistic object physics with gravity, forces, etc.'),
'',
'res/physics3d.svg',
'Physics3DBehavior',
Expand Down
40 changes: 22 additions & 18 deletions Extensions/Physics3DBehavior/Physics3DRuntimeBehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -647,10 +647,14 @@ namespace gdjs {
: onePixel;
const boxDepth =
shapeDimensionC > 0 ? shapeDimensionC : depth > 0 ? depth : onePixel;

// The convex radius should not eat up the whole volume.
const convexRadius = Math.min(
onePixel,
Math.min(boxWidth, boxHeight, boxDepth) / 4
);
shapeSettings = new Jolt.BoxShapeSettings(
this.getVec3(boxWidth / 2, boxHeight / 2, boxDepth / 2),
onePixel
convexRadius
);
quat = this.getQuat(0, 0, 0, 1);
} else if (this.shape === 'Capsule') {
Expand All @@ -663,7 +667,7 @@ namespace gdjs {
const capsuleDepth =
shapeDimensionB > 0 ? shapeDimensionB : depth > 0 ? depth : onePixel;
shapeSettings = new Jolt.CapsuleShapeSettings(capsuleDepth / 2, radius);
quat = this.getShapeOrientationQuat();
quat = this._getShapeOrientationQuat();
} else if (this.shape === 'Cylinder') {
const radius =
shapeDimensionA > 0
Expand All @@ -673,12 +677,17 @@ namespace gdjs {
: onePixel;
const cylinderDepth =
shapeDimensionB > 0 ? shapeDimensionB : depth > 0 ? depth : onePixel;
// The convex radius should not eat up the whole volume.
const convexRadius = Math.min(
onePixel,
Math.min(cylinderDepth, radius) / 4
);
shapeSettings = new Jolt.CylinderShapeSettings(
cylinderDepth / 2,
radius,
onePixel
convexRadius
);
quat = this.getShapeOrientationQuat();
quat = this._getShapeOrientationQuat();
} else {
// Create a 'Sphere' by default.
const radius =
Expand All @@ -704,19 +713,17 @@ namespace gdjs {
return rotatedShape;
}

private getShapeOrientationQuat(): Jolt.Quat {
let quat: Jolt.Quat;
private _getShapeOrientationQuat(): Jolt.Quat {
if (this.shapeOrientation === 'X') {
// Top on X axis.
quat = this.getQuat(0, 0, Math.sqrt(2) / 2, -Math.sqrt(2) / 2);
return this.getQuat(0, 0, Math.sqrt(2) / 2, -Math.sqrt(2) / 2);
} else if (this.shapeOrientation === 'Y') {
// Top on Y axis.
quat = this.getQuat(0, 0, 0, 1);
return this.getQuat(0, 0, 0, 1);
} else {
// Top on Z axis.
quat = this.getQuat(Math.sqrt(2) / 2, 0, 0, Math.sqrt(2) / 2);
return this.getQuat(Math.sqrt(2) / 2, 0, 0, Math.sqrt(2) / 2);
}
return quat;
}

recreateShape(): void {
Expand Down Expand Up @@ -813,9 +820,6 @@ namespace gdjs {
bodyCreationSettings.mLinearDamping = this.linearDamping;
bodyCreationSettings.mAngularDamping = this.angularDamping;
bodyCreationSettings.mGravityFactor = this.gravityScale;
// TODO Collision between 2 non-dynamic body should be checked during the
// collision condition to improve efficiency.
bodyCreationSettings.mCollideKinematicVsNonDynamic = true;

const bodyInterface = this._sharedData.bodyInterface;
this._body = bodyInterface.CreateBody(bodyCreationSettings);
Expand Down Expand Up @@ -1311,7 +1315,7 @@ namespace gdjs {
this._sharedData.bodyInterface.SetLinearVelocity(
body.GetID(),
this.getVec3(
linearVelocityX * this._sharedData.worldScale,
linearVelocityX * this._sharedData.worldInvScale,
body.GetLinearVelocity().GetY(),
body.GetLinearVelocity().GetZ()
)
Expand All @@ -1337,7 +1341,7 @@ namespace gdjs {
body.GetID(),
this.getVec3(
body.GetLinearVelocity().GetX(),
linearVelocityY * this._sharedData.worldScale,
linearVelocityY * this._sharedData.worldInvScale,
body.GetLinearVelocity().GetZ()
)
);
Expand All @@ -1363,7 +1367,7 @@ namespace gdjs {
this.getVec3(
body.GetLinearVelocity().GetX(),
body.GetLinearVelocity().GetY(),
linearVelocityZ * this._sharedData.worldScale
linearVelocityZ * this._sharedData.worldInvScale
)
);
}
Expand All @@ -1374,7 +1378,7 @@ namespace gdjs {
}
const body = this._body!;

return body.GetLinearVelocity().Length();
return body.GetLinearVelocity().Length() * this._sharedData.worldScale;
}

applyForce(
Expand Down

0 comments on commit 3b45463

Please sign in to comment.