I’m making an engine for a game. Give me some good advice.
I did it in a complicated way and it lags, maybe there are options to make it simpler.
I use Kinematics on the character. Because the dynamics don’t suit me
var PlayerController = pc.createScript('playerController');
PlayerController.attributes.add('speed', { type: 'number', default: 7 });
PlayerController.prototype.initialize = function () {
this.gamepadEnabled = false;
this.setupUI();
this.Test = 0;
this.Test1 = 0;
this.Test2 = 0;
this.entity.collision.on('collisionstart', this.onCollisionStart, this);
};
PlayerController.prototype.update = function (dt) {
if (this.app.keyboard.wasPressed(pc.KEY_Q)) {
this.gamepadEnabled = true;
}
if (this.gamepadEnabled) {
const gamepad = navigator.getGamepads()[0];
if (gamepad) {
this.handleMovement(gamepad, dt);
this.displayGamepadInfo(gamepad);
}
}
};
PlayerController.prototype.onCollisionStart = function (result) {
this.Test2 = result.other.name === "Box4" ? 1 : 0;
this.Test1 = result.other.name === "Box1" ? 1 : 0;
this.Test = result.other.name === "Box" ? 0 : 0;
this.Test = result.other.name === "Ramp" ? 1 : 0;
};
PlayerController.prototype.handleMovement = function (gamepad, dt) {
let horiz = -gamepad.axes[0] || 0;
let vert = -gamepad.axes[1] || 0;
const deadZone = 0.2;
horiz = Math.abs(horiz) < deadZone ? 0 : horiz;
vert = Math.abs(vert) < deadZone ? 0 : vert;
let moveX = 0, moveY = 0, moveZ = 0;
let angle = null;
if (vert > 0) { moveZ = this.speed * dt; angle = 0; }
else if (vert < 0) { moveZ = -this.speed * dt; angle = 180; }
else if (horiz < 0) { moveX = -this.speed * dt; angle = 270; }
else if (horiz > 0) { moveX = this.speed * dt; angle = 90; }
if (this.Test1 === 1) {
moveX = 0;
const pushBack = 0.5;
if (gamepad.axes[0] > 0) moveX += pushBack;
else if (gamepad.axes[0] < 0) moveX -= pushBack;
this.Test1 = 0;
}
if (this.Test2 === 1) {
moveZ = 0;
const pushBack = 0.5;
if (-gamepad.axes[1] > 0) moveZ -= pushBack;
else if (-gamepad.axes[1] < 0) moveZ += pushBack;
this.Test2 = 0;
}
if (this.Test === 1 && moveX !== 0) {
const rad = 32.3 * (Math.PI / 180);
const direction = gamepad.axes[0] > 0.1 ? -1 : 1;
const originalMoveX = moveX;
moveX *= Math.cos(rad);
moveY = direction * Math.abs(originalMoveX) * Math.sin(rad);
}
this.entity.anim.setBoolean('walk', angle !== null);
if (angle !== null) {
const targetRot = new pc.Quat().setFromEulerAngles(0, angle, 0);
this.entity.setRotation(targetRot);
}
this.entity.translate(moveX, moveY, moveZ);
};
PlayerController.prototype.applyPushback = function (axisValue, strength) {
if (axisValue > 0.1) return -strength;
if (axisValue < -0.1) return strength;
return 0;
};
PlayerController.prototype.displayGamepadInfo = function (gamepad) {
const pos = this.entity.getPosition();
this.gamepadInfo.innerHTML =
`<strong>Gamepad Info:</strong><br>` +
gamepad.axes.map((val, i) => `Axis ${i}: ${val.toFixed(2)}<br>`).join('') +
gamepad.buttons.map((btn, j) => `Button ${j}: ${btn.pressed ? 'Pressed' : 'Released'}<br>`).join('') +
`<br><strong>Position:</strong> X: ${pos.x.toFixed(2)} Z: ${pos.z.toFixed(2)} Y: ${pos.y.toFixed(2)}<br>`;
};
PlayerController.prototype.setupUI = function () {
this.gamepadInfo = document.createElement('div');
Object.assign(this.gamepadInfo.style, {
position: 'absolute',
top: '50px',
left: '10px',
backgroundColor: 'rgba(0,0,0,0.5)',
color: 'white',
padding: '10px',
fontFamily: 'Arial',
fontSize: '12px'
});
document.body.appendChild(this.gamepadInfo);
};


