I’m trying to implement force-based controls for a tank/vehicle style entity. The entity has a box collider (so it’s essentially a box) and I apply a force (along the local z-axis) to push it forward as well as a moment (around the local y-axis) to turn it. The entity also has a third-person camera attached to it, which is following it around.
During movement, the object is bumping a lot, which causes unwanted camera shake. I think this is caused by inaccuracies in the physics engine, which make me temporarily steer the corners and edges of the cube into the ground. The physics engine will then resolve this collision by displacing (teleporting) the entity, leading to undesirable bumps. It could be caused by something else though; this is my best guess so far.
Does anybody have any ideas what the cause is and how to remedy it?
Here is the control script and I will also add the project, once I figured out how to get a sharable link.
CozmoController.js
var CozmoController = pc.createScript('CozmoController');
CozmoController.attributes.add('translation_power', {
type: 'number',
default: 1,
description: 'Translational Acceleration'
});
CozmoController.attributes.add('rotation_power', {
type: 'number',
default: 0.01,
description: 'Rotational Acceleration'
});
// initialize code called once per entity
CozmoController.prototype.initialize = function() {
this.force = new pc.Vec3();
this.angular = new pc.Vec3();
this.eulers = new pc.Vec3();
this.colliderEntity = this.entity;
var app = this.app;
};
// update code called every frame
CozmoController.prototype.update = function(dt) {
var forward = this.colliderEntity.forward;
var up = this.colliderEntity.up;
var app = this.app;
// movement
var translationDirection = 0;
var rotationDirection = 0;
// Use W-A-S-D keys to move player
// Check for key presses
if (app.keyboard.isPressed(pc.KEY_A)) {
rotationDirection++;
}
if (app.keyboard.isPressed(pc.KEY_D)) {
rotationDirection--;
}
if (app.keyboard.isPressed(pc.KEY_W)) {
translationDirection++;
}
if (app.keyboard.isPressed(pc.KEY_S)) {
translationDirection--;
}
if (translationDirection !== 0) {
this.entity.rigidbody.applyForce(
forward.clone().normalize().scale(translationDirection * this.translation_power)
);
console.log(forward);
}
if (rotationDirection !== 0) {
this.entity.rigidbody.applyTorque(
up.clone().normalize().scale(rotationDirection * this.rotation_power)
);
}
};
Edit:
So based on my reading I should simply be able to share the editor link (the project is public), and that should give read-only access to people that are not me.
Here is the project link: https://playcanvas.com/editor/scene/1046551
Edit 2:
tMeqZZh0Fu