From 6f641e79d805a8308968d8155776edcc03d6624b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Davy=20H=C3=A9lard?= Date: Tue, 19 Nov 2024 21:04:27 +0100 Subject: [PATCH] Fix collision. --- .../Physics3DRuntimeBehavior.ts | 28 +++++-------- .../PhysicsCharacter3DRuntimeBehavior.ts | 42 ++++++++++++------- 2 files changed, 38 insertions(+), 32 deletions(-) diff --git a/Extensions/Physics3DBehavior/Physics3DRuntimeBehavior.ts b/Extensions/Physics3DBehavior/Physics3DRuntimeBehavior.ts index fce8645e87bd..1af3439aed36 100644 --- a/Extensions/Physics3DBehavior/Physics3DRuntimeBehavior.ts +++ b/Extensions/Physics3DBehavior/Physics3DRuntimeBehavior.ts @@ -98,7 +98,6 @@ namespace gdjs { bodyInterface: Jolt.BodyInterface; /** Contact listener to keep track of current collisions */ contactListener: Jolt.ContactListenerJS; - behaviorsByBodyID = new Map(); /** Avoid creating new vectors all the time */ _tempVec3 = new Jolt.Vec3(); _tempRVec3 = new Jolt.RVec3(); @@ -154,14 +153,6 @@ namespace gdjs { behaviorA.onContactBegin(behaviorB); behaviorB.onContactBegin(behaviorA); - this.behaviorsByBodyID.set( - bodyA.GetID().GetIndexAndSequenceNumber(), - behaviorA - ); - this.behaviorsByBodyID.set( - bodyB.GetID().GetIndexAndSequenceNumber(), - behaviorB - ); }; this.contactListener.OnContactRemoved = ( subShapePairPtr: number @@ -171,15 +162,17 @@ namespace gdjs { Jolt.SubShapeIDPair ); - const behaviorA = this.behaviorsByBodyID.get( - subShapePair.GetBody1ID().GetIndexAndSequenceNumber() - ); - const behaviorB = this.behaviorsByBodyID.get( - subShapePair.GetBody2ID().GetIndexAndSequenceNumber() - ); + // This is ok because bodies are not deleted during the Physics step. + const bodyLockInterface = this.physicsSystem.GetBodyLockInterface(); + const bodyA = bodyLockInterface.TryGetBody(subShapePair.GetBody1ID()); + const bodyB = bodyLockInterface.TryGetBody(subShapePair.GetBody2ID()); + + const behaviorA = bodyA.gdjsAssociatedBehavior; + const behaviorB = bodyB.gdjsAssociatedBehavior; if (!behaviorA || !behaviorB) { return; } + behaviorA.onContactEnd(behaviorB); behaviorB.onContactEnd(behaviorA); }; @@ -188,7 +181,9 @@ namespace gdjs { bodyPtrB: number, manifoldPtr: number, settingsPtr: number - ): void => {}; + ): void => { + // TODO we could rely on this event. + }; this.contactListener.OnContactValidate = ( bodyPtrA: number, bodyPtrB: number, @@ -244,7 +239,6 @@ namespace gdjs { physicsBehavior.contactsStartedThisFrame.length = 0; physicsBehavior.contactsEndedThisFrame.length = 0; } - this.behaviorsByBodyID.clear(); for (const physicsBehavior of this._registeredBehaviors) { physicsBehavior.updateBodyFromObject(); } diff --git a/Extensions/Physics3DBehavior/PhysicsCharacter3DRuntimeBehavior.ts b/Extensions/Physics3DBehavior/PhysicsCharacter3DRuntimeBehavior.ts index 99af31734f1e..964cd03e0f12 100644 --- a/Extensions/Physics3DBehavior/PhysicsCharacter3DRuntimeBehavior.ts +++ b/Extensions/Physics3DBehavior/PhysicsCharacter3DRuntimeBehavior.ts @@ -263,6 +263,8 @@ namespace gdjs { const body = sharedData.physicsSystem .GetBodyLockInterface() .TryGetBody(this.character.GetInnerBodyID()); + // TODO This is not really reliable. We could choose to disable it and force user to use the "is on platform" condition. + body.SetCollideKinematicVsNonDynamic(true); return body; } @@ -291,7 +293,7 @@ namespace gdjs { if (!this.character) { return; } - console.log('Step character'); + // console.log('Step character'); // console.log( // 'Character: ' + @@ -372,6 +374,7 @@ namespace gdjs { ); } + this.character.UpdateGroundVelocity(); const groundVelocity = this.character.GetGroundVelocity(); const forwardSpeed = @@ -389,11 +392,21 @@ namespace gdjs { const sharedData = behavior._sharedData; const jolt = sharedData.jolt; - extendedUpdateSettings.mStickToFloorStepDown.Set( - 0, - 0, - Math.min(-Math.abs(forwardSpeed) * timeDelta * this._slopeClimbingFactor, groundVelocity.GetZ()) + const onePixel = behavior._sharedData.worldInvScale; + const floorStepDownSpeedZ = Math.min( + -Math.abs(forwardSpeed) * this._slopeClimbingFactor, + groundVelocity.GetZ() ); + if ( + Math.abs(floorStepDownSpeedZ) <= + this._maxFallingSpeed * behavior._sharedData.worldInvScale + ) { + extendedUpdateSettings.mStickToFloorStepDown.Set( + 0, + 0, + -onePixel + floorStepDownSpeedZ * timeDelta + ); + } this.character.SetRotation(behavior._body!.GetRotation()); this.character.ExtendedUpdate( @@ -420,14 +433,14 @@ namespace gdjs { // this._currentFallSpeed // ); - console.log( - 'Ground: ' + - this.character.GetGroundVelocity().GetX() + - ' ' + - this.character.GetGroundVelocity().GetY() + - ' ' + - this.character.GetGroundVelocity().GetZ() - ); + // console.log( + // 'Ground: ' + + // this.character.GetGroundVelocity().GetX() + + // ' ' + + // this.character.GetGroundVelocity().GetY() + + // ' ' + + // this.character.GetGroundVelocity().GetZ() + // ); // console.log( // 'Speed: ' + @@ -477,8 +490,7 @@ namespace gdjs { this.hasPressedForwardKey = false; this.hasPressedJumpKey = false; - - console.log('END Step character'); + // console.log('END Step character'); } doStepPostEvents(instanceContainer: gdjs.RuntimeInstanceContainer) {}