Rigidbody lookat

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.

See this thread: entity.lookAt not rotating entity

The project under the link works with errors. And it doesn’t solve my issue.

This log message causes big problems:

  1. there is constant spam in the log, and what’s worse is the class and line where this happens is not displayed in the log;
  2. earlier I could use the same entity to calculate new positions.
this.entity.lookAt(targetPoint);
const position = this.entity.getPosition();
const eulers = this.entity.getEulerAngles();
this.entity.rigidbody.teleport(position.x, position.y, position.z, eulers.x, eulers.y, eulers.z);

it worked. but now, because of the log, everything will have to be redone.

Hello @yaustar,

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?

Example: PlayCanvas | HTML5 Game Engine

This has been fixed now

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.

Hence the recommend pattern is to have a child entity for the graphical model that you can rotate which is what that thread I linked to should be doing. (and now fixed
https://playcanvas.com/project/808772/overview/look-at-with-physics)

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());
2 Likes

Scale wasn’t something that was considered when reviewing the PR as it isn’t a common path :thinking:

Created a ticket for this: Dynamic rigidbody dirty transform warning shouldn't be showing when changing scale? · Issue #5252 · playcanvas/engine · GitHub

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?

Using lookAt is changing the rotation of the entity which dynamic rigidbody overwrites in the simulation.

The warning was suppose to warn users that trying to change what the simulation overwrites which is position and rotation.

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.

const position = this.entity.getPosition();

// LookAt hack
const targetPoint = new pc.Vec3(pc.math.random(-5, 5), position.y, pc.math.random(-5, 5));
this.devEntity.lookAt(targetPoint);
const devEntityEulers = this.devEntity.getEulerAngles();

this.entity.rigidbody.teleport(position.x, position.y, position.z, devEntityEulers.x, devEntityEulers.y, devEntityEulers.z);

Hmm, it looks like we have to back out the PR. ‘getPosition()’ also dirties the transform unfortunately.

We will look at doing this in the next patch release

1 Like

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.

Thanks for the precision @LeXXik !
I just had my model and rigidbody on the same entity but were not trying to scale the shape.

This warning has been removed as part of release 1.62.1 that is now in the Editor

1 Like