Skip to content

Commit

Permalink
fix: character controller delta operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Gugustinette committed Sep 1, 2024
1 parent 05af1db commit a270013
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 15 deletions.
9 changes: 4 additions & 5 deletions packages/2d/src/controllers/FCharacterControllerK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,16 @@ export abstract class FCharacterControllerK extends FCharacterController {

/**
* Return the corrected movements for the current frame.
* @param delta The time elapsed since the last frame.
*/
getCorrectedMovements(delta: number): { x: number, y: number } {
getCorrectedMovements(): { x: number, y: number } {
const movementDirection = new RAPIER.Vector2(0, 0)
// Compute the movement direction
movementDirection.x = this.inputs.left ? -1 : this.inputs.right ? 1 : 0

// Create movement vector
const desiredMovement = {
x: movementDirection.x * delta * 8 * this.speed,
y: this.yVelocity * delta,
x: movementDirection.x * 8 * 0.01 * this.speed,
y: this.yVelocity * 0.01,
}
// Compute the desired movement
this.characterController.computeColliderMovement(
Expand All @@ -61,7 +60,7 @@ export abstract class FCharacterControllerK extends FCharacterController {

// If yVelocity is not 0, apply gravity
if (this.yVelocity > this.scene.world.gravity.y) {
this.yVelocity += this.scene.world.gravity.y * delta * 4
this.yVelocity += this.scene.world.gravity.y * 0.00981 * 4
}
else {
this.yVelocity = this.scene.world.gravity.y
Expand Down
2 changes: 1 addition & 1 deletion packages/2d/src/controllers/FCharacterControllerKP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class FCharacterControllerKP extends FCharacterControllerK {

onFrame(delta: number): void {
// Get the corrected movement
const correctedMovement = this.getCorrectedMovements(delta)
const correctedMovement = this.getCorrectedMovements()

// Apply the movement to the rigid body
this.component.rigidBody?.rigidBody.setNextKinematicTranslation({
Expand Down
2 changes: 1 addition & 1 deletion packages/2d/src/controllers/FCharacterControllerKV.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class FCharacterControllerKV extends FCharacterControllerK {

onFrame(delta: number): void {
// Get the corrected movement
const correctedMovement = this.getCorrectedMovements(delta)
const correctedMovement = this.getCorrectedMovements()

// Apply the movement to the rigid body
this.component.rigidBody?.rigidBody.setLinvel({
Expand Down
11 changes: 5 additions & 6 deletions packages/3d/src/controllers/FCharacterControllerK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ export abstract class FCharacterControllerK extends FCharacterController {

/**
* Return the corrected movements for the current frame.
* @param delta The time elapsed since the last frame.
*/
getCorrectedMovements(delta: number): { x: number, y: number, z: number } {
getCorrectedMovements(): { x: number, y: number, z: number } {
let worldDirection = new THREE.Vector3(0, 0, 0)
// Compute the movement direction
worldDirection.x = this.inputs.left ? 1 : this.inputs.right ? -1 : 0
Expand All @@ -58,9 +57,9 @@ export abstract class FCharacterControllerK extends FCharacterController {

// Create movement vector
const desiredMovement = {
x: worldDirection.x * delta * 8,
y: this.yVelocity * delta,
z: worldDirection.z * delta * 8,
x: worldDirection.x * 8 * 0.01,
y: this.yVelocity * 0.01,
z: worldDirection.z * 8 * 0.01,
}
// Compute the desired movement
this.characterController.computeColliderMovement(
Expand All @@ -71,7 +70,7 @@ export abstract class FCharacterControllerK extends FCharacterController {

// If yVelocity is not 0, apply gravity
if (this.yVelocity > this.scene.world.gravity.y) {
this.yVelocity += this.scene.world.gravity.y * delta * 4
this.yVelocity += this.scene.world.gravity.y * 0.00981 * 4
}
else {
this.yVelocity = this.scene.world.gravity.y
Expand Down
2 changes: 1 addition & 1 deletion packages/3d/src/controllers/FCharacterControllerKP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class FCharacterControllerKP extends FCharacterControllerK {
/**
* Get the corrected movements for the current frame
*/
const correctedMovement = this.getCorrectedMovements(delta)
const correctedMovement = this.getCorrectedMovements()

// Apply the movement to the rigid body
this.component.rigidBody?.rigidBody.setNextKinematicTranslation({
Expand Down
2 changes: 1 addition & 1 deletion packages/3d/src/controllers/FCharacterControllerKV.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class FCharacterControllerKV extends FCharacterControllerK {
/**
* Get the corrected movements for the current frame
*/
const correctedMovement = this.getCorrectedMovements(delta)
const correctedMovement = this.getCorrectedMovements()

// Apply the movement to the rigid body
this.component.rigidBody?.rigidBody.setLinvel({
Expand Down

0 comments on commit a270013

Please sign in to comment.