I use this.entity.lookAt(endPos);
to rotate object and it gives error in console debug.js:106 Cannot set rigid body transform from entity. Use entity.rigidbody#teleport instead.
But Rigidbody doesn’t have a method like “lookAt()”.
How can I rotate a rigidbody entity without error?
I don’t know where to get the values to pass them to the ‘teleport()’ function. How to get them?
If the entity is a dynamic rigidbody, then the physics simulation controls the rotation and position of the entity. This means that functions like entity setPosition and lookAt won’t work or give odd results.
A new warning was added to engine 1.62 when users try to use these functions on dynamic physics objects.
It seems the warning is linked to entity._wasDirty
If an entity containing a dynamic rigidbody is scaled the warning will trigger.
Isn’t it a bit extreme to have a warning at this level?
The key point here is that a dynamic rigidbody position and rotation is fully simulated by the physics. Setting the rotation and position directly on the entity will not work as that gets overwritten by the physics.
This warning is to prevent users from getting confused when they try to do this but nothing happens because the physics simulation is directly setting the position and rotation of the Entity.
Generally, users should not be using teleport every frame because that can affect the simulation but if you want to go down that route either use kinematic rigidbody so you can set the position and rotation directly.
Or use a Mat4 to calculate the position and rotation. eg:
this.lookAtMat4 = new pc.Mat4();
this.lookAtMat4.copy(this.entity.getWorldTransform());
this.lookAtMat4.setLookAt(this.lookAtMat4.getTranslation(), this.lookAtPosition, pc.Vec3.UP);
var forward = this.lookAtMat4.getZ().clone().mulScalar(-1);
var right = this.lookAtMat4.getX();
this.entity.rigidbody.teleport(this.lookAtMat4.getTranslation(), this.lookAtMat4.getEulerAngles());
I was able to reproduce the error. https://playcanvas.com/editor/scene/1721509
And it doesn’t seem to be related to the fact that I’m trying to move the entity itself.
What’s wrong here?
But I’m changing another entity ‘devEntity’ without ‘Rigidbody’, which I specifically created in order to use ‘lookAt’ with it.
Then I take the angles of ‘devEntity’ and apply their values through ‘teloport’ to the main entity.
Aside from the warning note, changing an entity scale on a dynamic rigidbody component is a feature with an undefined behavior. The engine can only change the scale of the model, but Ammo cannot change the shape. If someone wants to change the scale of an entity, one would probably mean to scale both the body shape and the visual model. I’d say the engine shouldn’t support changing the scale, if the physics engine doesn’t support it.