Hello,
I have a game that is physics based where the player controls an astronaut by applying either positive or negative force to the astronaut to transport it between planets. I have run into the issue that the rate at which the force applied to the astronaut varies due to the fps of the device the game is played upon. For example, the astronaut moves more slowly in lower fps than on devices with higher fps. This is due to the default update method within playcanvas. I thought I could resolve this by implementing a fixed updated method so the rate at which force is applied to the astronaut would be the same independent of device type but I have come to understand that this fixed update method will not be implemented within playcanvas. Can anyone provide a way to ensure that the rate force is applied to the astronaut will be the same across devices?
Here is example code I would need modified.
XXX.attributes.add('speed', {
type: 'number',
default: 0.1,
min: 0.01,
max: 0.5,
precision: 2,
description: 'Controls the movement speed'
});
XXX.prototype.update = function(dt) {
if (this.app.keyboard.isPressed(pc.KEY_LEFT)) {
forceX = -this.speed;
forceZ += this.speed;
} else if (this.app.keyboard.isPressed(pc.KEY_RIGHT)) {
forceX += this.speed;
forceZ = -this.speed;
}
this.force.x = forceX;
this.force.z = forceZ;
// if we have some non-zero force
if (this.force.length()) {
// calculate force vector
var rX = Math.cos(-Math.PI * 0.25);
var rY = Math.sin(-Math.PI * 0.25);
this.force.set(this.force.x * rX - this.force.z * rY, 0, this.force.z * rX + this.force.x * rY);
// clamp force to the speed
if (this.force.length() > this.speed) {
this.force.normalize().scale(this.speed);
}
}
// apply impulse to move the entity
this.entity.rigidbody.applyImpulse(this.force);
}