Skip to content

Commit 5bdf2d3

Browse files
committed
Fix wheel settings.
1 parent 3cd59d2 commit 5bdf2d3

File tree

1 file changed

+45
-35
lines changed

1 file changed

+45
-35
lines changed

Extensions/Physics3DBehavior/PhysicsVehicle3DRuntimeBehavior.ts

+45-35
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
// TODO Remove
44
const vec3ToString = (vec3: Jolt.RVec3) =>
5-
Math.round(vec3.GetX() * 100) +
5+
vec3.GetX().toFixed(3) +
66
' ' +
7-
Math.round(vec3.GetY() * 100) +
7+
vec3.GetY().toFixed(3) +
88
' ' +
9-
Math.round(vec3.GetZ() * 100);
9+
vec3.GetZ().toFixed(3);
1010

1111
namespace gdjs {
1212
interface PhysicsVehicle3DNetworkSyncDataType {
@@ -329,13 +329,13 @@ namespace gdjs {
329329
forward = this._hasPressedForwardKey
330330
? 1.0
331331
: this._hasPressedBackwardKey
332-
? -1.0
333-
: 0.0;
332+
? -1.0
333+
: 0.0;
334334
right = this._hasPressedRightKey
335335
? 1.0
336336
: this._hasPressedLeftKey
337-
? -1.0
338-
: 0.0;
337+
? -1.0
338+
: 0.0;
339339

340340
if (this.previousForward < 0 !== forward < 0) {
341341
const velocity = carBody
@@ -378,9 +378,21 @@ namespace gdjs {
378378

379379
console.log(
380380
[
381-
"Car center",
381+
'Car center',
382382
vec3ToString(carBody.GetPosition()),
383-
"Wheels",
383+
'Mass center',
384+
vec3ToString(carBody.GetCenterOfMassPosition()),
385+
'Mass',
386+
1 / carBody.GetMotionProperties().GetInverseMass(),
387+
'Speed',
388+
(carBody
389+
.GetRotation()
390+
.InverseRotate(carBody.GetLinearVelocity())
391+
.GetX() *
392+
60 *
393+
60) /
394+
1000,
395+
'Wheels',
384396
vec3ToString(
385397
this._vehicleController
386398
.GetConstraint()
@@ -410,7 +422,7 @@ namespace gdjs {
410422

411423
// console.log(forward, right, brake, handBrake);
412424

413-
this._vehicleController.SetDriverInput(forward, right, brake, handBrake);
425+
this._vehicleController.SetDriverInput(forward, -right, brake, handBrake);
414426
if (
415427
right !== 0.0 ||
416428
forward !== 0.0 ||
@@ -675,24 +687,23 @@ namespace gdjs {
675687
return null;
676688
}
677689
const { behavior } = physics3D;
678-
const { owner3D, _sharedData } = this.vehicleBehavior;
690+
const { _sharedData } = this.vehicleBehavior;
679691

680-
const halfVehicleWidth =
681-
(owner3D.getWidth() / 2) * _sharedData.worldInvScale;
682-
const halfVehicleHeight =
683-
(owner3D.getHeight() / 2) * _sharedData.worldInvScale;
684-
const halfVehicleDepth =
685-
(owner3D.getDepth() / 2) * _sharedData.worldInvScale;
692+
const halfVehicleWidth = behavior._shapeHalfWidth;
693+
const halfVehicleHeight = behavior._shapeHalfHeight;
694+
const halfVehicleDepth = behavior._shapeHalfDepth;
695+
696+
console.log(halfVehicleWidth, halfVehicleHeight, halfVehicleDepth);
686697

687698
const wheelOffsetX = halfVehicleWidth;
688699
const wheelOffsetY = halfVehicleHeight;
689-
const wheelOffsetZ = halfVehicleDepth;
690-
const wheelRadius = wheelOffsetZ;
691-
const wheelWidth = wheelOffsetZ / 3;
692-
const suspensionMinLength = wheelOffsetZ / 4;
693-
const suspensionMaxLength = 2 * suspensionMinLength;
700+
const wheelOffsetZ = 0;
701+
const wheelRadius = halfVehicleDepth;
702+
const wheelWidth = halfVehicleDepth / 3;
703+
const suspensionMinLength = wheelRadius;
704+
const suspensionMaxLength = 1.5 * suspensionMinLength;
694705
const maxSteerAngle = gdjs.toRad(30);
695-
const fourWheelDrive = true;
706+
const fourWheelDrive = false;
696707
const frontBackLimitedSlipRatio = 1.4;
697708
const leftRightLimitedSlipRatio = 1.4;
698709
const antiRollbar = true;
@@ -706,16 +717,7 @@ namespace gdjs {
706717
const maxEngineTorque = 500;
707718
const clutchStrength = 10;
708719

709-
// TODO Use OffsetCenterOfMassShapeSettings to allow to set a custom center of mass in createShape.
710-
//const carShape = behavior.createShape();
711-
712-
const carShapeSettings = new Jolt.OffsetCenterOfMassShapeSettings(
713-
new Jolt.Vec3(0, 0, -halfVehicleDepth),
714-
new Jolt.BoxShapeSettings(
715-
new Jolt.Vec3(halfVehicleWidth, halfVehicleHeight, halfVehicleDepth)
716-
)
717-
);
718-
const carShape = carShapeSettings.Create().Get();
720+
const carShape = behavior.createShape();
719721

720722
// Create car body
721723
const carBodySettings = new Jolt.BodyCreationSettings(
@@ -790,10 +792,18 @@ namespace gdjs {
790792
mWheels.forEach((wheelS) => {
791793
wheelS.mWheelUp = new Jolt.Vec3(0, 0, 1);
792794
wheelS.mWheelForward = new Jolt.Vec3(1, 0, 0);
795+
wheelS.mSuspensionDirection = new Jolt.Vec3(0, 0, -1);
796+
wheelS.mSteeringAxis = new Jolt.Vec3(0, 0, 1);
793797
wheelS.mRadius = wheelRadius;
794798
wheelS.mWidth = wheelWidth;
795799
wheelS.mSuspensionMinLength = suspensionMinLength;
796800
wheelS.mSuspensionMaxLength = suspensionMaxLength;
801+
// wheelS.mLongitudinalFriction.Clear();
802+
// wheelS.mLongitudinalFriction.AddPoint(0, 1);
803+
// wheelS.mLongitudinalFriction.AddPoint(1, 1);
804+
// wheelS.mLateralFriction.Clear();
805+
// wheelS.mLateralFriction.AddPoint(0, 1.2);
806+
// wheelS.mLateralFriction.AddPoint(90, 1.2);
797807
});
798808

799809
const controllerSettings = new Jolt.WheeledVehicleControllerSettings();
@@ -856,8 +866,8 @@ namespace gdjs {
856866
// behavior.getBodyLayer(),
857867
// 0.05,
858868
// )
859-
// );
860-
869+
// );
870+
861871
constraint.ResetGravityOverride();
862872
_sharedData.physicsSystem.AddConstraint(constraint);
863873
this.vehicleBehavior._vehicleController = Jolt.castObject(

0 commit comments

Comments
 (0)