Project: PlayCanvas 3D HTML5 Game Engine
Hello good. It is happening to me that when I manage to grab an object from the scene, it inexplicably rotates when it is in the air and when it crosses collisions with walls or the floor. When the player drops the item, this item comes out with inexplicable force.
the first part is when the object is grabbed by an event:
(on file first-person-camera.js)
app.on('firstperson:interact', async function () {
....
....
switch (typeInteract) {
case "pickable":
this.entity.pickedEntity = interactableEntities[i];
this.entity.pickedEntity.isPicking = true;
if (this.entity.pickedEntity.rigidbody) {
this.entity.pickedEntity.rigidbody.applyImpulse(0, 0, 0);
this.entity.pickedEntity.rigidbody.applyForce(0, 0, 0);
this.entity.pickedEntity.rigidbody.applyTorqueImpulse(0, 0, 0);
this.entity.pickedEntity.rigidbody.applyTorque(0, 0, 0);
}
this.entity.pickedEntity.distance = this.entity.pickedEntity.getPosition().distance(this.entity.cameraEntity.getPosition());
this.entity.pickedEntity.startPosition = new pc.Vec2();
this.entity.pickedEntity.startPosition.copy(this.entity.pickedEntity.getPosition());
break;
}
}
This second stop is triggered when the user stops grabbing the object.
(on file first-person-camera.js)
app.on('firstperson:interacted', async function () {
if (this.entity.pickedEntity) {
Ui.debug("UN picked: " + this.entity.pickedEntity.name, " THROW");
var direction = this.entity.pickedEntity.getPosition().sub(this.entity.pickedEntity.startPosition).normalize();
var force = direction.scale(1);
if (this.entity.pickedEntity.rigidbody) {
//this.entity.pickedEntity.rigidbody.enabled = true;
this.entity.pickedEntity.rigidbody.applyImpulse(0, 0, 0);
this.entity.pickedEntity.rigidbody.applyForce(0, 0, 0);
this.entity.pickedEntity.rigidbody.applyTorqueImpulse(0, 0, 0);
this.entity.pickedEntity.rigidbody.applyTorque(0, 0, 0);
this.entity.pickedEntity.rigidbody.useGravity = true;
this.entity.pickedEntity.rigidbody.enabled = false;
this.entity.pickedEntity.rigidbody.enabled = true;
}
this.entity.pickedEntity.isPicking = false;
this.entity.pickedEntity = null;
}
}, this);
When the user grabs the object, in the postUpdate() event, a block of code is executed as long as the pickedEntity is not null:
(on file first-person-camera.js)
/*Also move picked entities*/
if (this.entity.pickedEntity && this.cameraEntity) {
var cameraPos = this.entity.cameraEntity.getPosition();
var direction = this.entity.cameraEntity.forward;
var targetPos = new pc.Vec3().add2(cameraPos, direction.scale(this.entity.pickedEntity.distance));
if (this.entity.pickedEntity.rigidbody) {
this.entity.pickedEntity.rigidbody.applyImpulse(0, 0, 0);
this.entity.pickedEntity.rigidbody.applyForce(0, 0, 0);
this.entity.pickedEntity.rigidbody.applyTorqueImpulse(0, 0, 0);
this.entity.pickedEntity.rigidbody.applyTorque(0, 0, 0);
this.entity.pickedEntity.rigidbody.teleport(targetPos);
this.entity.pickedEntity.rigidbody.applyImpulse(0, 0, 0);
this.entity.pickedEntity.rigidbody.applyForce(0, 0, 0);
this.entity.pickedEntity.rigidbody.applyTorqueImpulse(0, 0, 0);
this.entity.pickedEntity.rigidbody.applyTorque(0, 0, 0);
} else {
this.entity.pickedEntity.setPosition(targetPos);
}
}
Several problems arise:
- the picked up object should not go through collisions with static rigidbody
- The object should not spin or rotate on its own when going through static collisions
- The object when it is released, it seems that it is thrown with an astronomical force being that the forces are “off”.
Has anyone had something similar happen. Does anyone have any suggestions, I would greatly appreciate it.
thanks in advance