Skip to content

Commit

Permalink
Fix collision.
Browse files Browse the repository at this point in the history
  • Loading branch information
D8H committed Nov 19, 2024
1 parent 3534a2d commit 6f641e7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 32 deletions.
28 changes: 11 additions & 17 deletions Extensions/Physics3DBehavior/Physics3DRuntimeBehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ namespace gdjs {
bodyInterface: Jolt.BodyInterface;
/** Contact listener to keep track of current collisions */
contactListener: Jolt.ContactListenerJS;
behaviorsByBodyID = new Map<number, gdjs.Physics3DRuntimeBehavior>();
/** Avoid creating new vectors all the time */
_tempVec3 = new Jolt.Vec3();
_tempRVec3 = new Jolt.RVec3();
Expand Down Expand Up @@ -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
Expand All @@ -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);
};
Expand All @@ -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,
Expand Down Expand Up @@ -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();
}
Expand Down
42 changes: 27 additions & 15 deletions Extensions/Physics3DBehavior/PhysicsCharacter3DRuntimeBehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -291,7 +293,7 @@ namespace gdjs {
if (!this.character) {
return;
}
console.log('Step character');
// console.log('Step character');

// console.log(
// 'Character: ' +
Expand Down Expand Up @@ -372,6 +374,7 @@ namespace gdjs {
);
}

this.character.UpdateGroundVelocity();
const groundVelocity = this.character.GetGroundVelocity();

const forwardSpeed =
Expand All @@ -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(
Expand All @@ -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: ' +
Expand Down Expand Up @@ -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) {}
Expand Down

0 comments on commit 6f641e7

Please sign in to comment.