Problems trying to lerp entities between positions

Hi! First time trying to do a project, this is what I’ve got so far, I’ve been having fun with it, but a bug has been stumping me, PlayCanvas | HTML5 Game Engine.

On the car.js file, I’m trying to make it so after an entity tagged with “dettach” is uncoupled from the car it was parented to, I’m able to re-attach it by pressing f. My problem is, after starting the attach process, and taking clones of the vectors that describe the world position of the wheels, I’m trying to lerp them from their current position to the spot where they will fit, I’ve added some debugging as spheres and lines to try and help, but I’m unsure of how to proceed, because my current understanding of the local and world positions is as follows:

The world position of an entity would not change when being reparented, and that can be confirmed by it not changing places visually. On that case, if I .getPosition() from an entity and try to pc.Vec3().lerp(positionA, positionB, time), it should smoothly transition between the two points as expected. I’ve even made a proof of concept of that idea with the white sphere in the (0,0,0) starting position and ending at the player entity.

I believe some misconception is happening where if I .getPosition() of something that was reparented and try to setPosition through lerp iterations, it is not, actually, at that position, and that’s why it teleports around when trying to perform that “animation”.

Any help on the matter would be of great help, thanks!

You are using a compound physical shape. Moving a child of a compound body is not supported. You can move it by destroying the shape (disabling an entity), moving a child to a new position, then creating a new body (enabling entity). Obviously, doing that is not really optimal performance wise.

Secondly, you cannot move a dynamic body (entity with a dynamic rigidbody component) via setting a position on an entity. That would only move the visual entity to a new position and the engine would move it back later after it syncs with the physics world. If you want to set position on a rigidbody, your option is to either switch the body to kinematic, move and set back to dynamic. Or use a teleport. For continous moves, like you are trying to do, which spans over multiple frames, use kinematic approach.

Now, to sum it up, you cannot use a compound shape, so your wheels must be separate rigidbodies. It starts to get complicated from here (making vehicles always is), since you would need your car body to be attached to the wheel. THe most common approach is to use a physical joint. There is an example of how it can be done:
https://playcanvas.com/project/643289/overview/vehicle-physics

If you want to detach the wheel, you can destroy the joint. That will make the wheel fall off and bounce off the ground, collide with objects etc. Once you want to reattach it - switch it to kinematic, lerp to its intended location (you can have an empty entity as a child of a car body that represents a wheel position, which you can getPosition() to know the target location), then switch it to dynamic, reset linear and angular velocities, create a new physical joint and attach it to the body. You might need to recreate a physical wheel as well, or you might not, I am not sure.

1 Like

Thanks for the detailed response, I wasn’t aware of that compound shape limitation, and moved the reparenting of the wheel to the end of the process, upon detacching the entity from its parent(wheel from the car), I create an copy of that entity and set its position and rotation to the entity that was the wheel, and let that be parented to the root of the app, is that enough to circumvent the compound shape limitation? I did try to set it as a kinematic body, move and then set as rigidbody once it is parented to the car, but it seems to be teleporting as well, could it be some leftover jitterness created by reparenting an entity that was part of a compound shape? Because it collides and is acted upon by physics nonetheless, and only seems to find trouble when I try to setPosition it, which, I do remove the rigidbody component before trying to move it, because of that physics overriding, but apparently there’s something else lingering that causes it to pop around. On occasion, some of my iterations of the code had the wheel doing the lerp movement but from the origin(0,0,0) of its coordinate space, which led me to believe something was resetting it to origin before or during my “animation”, I even tried checking it out with breakpoints on the browser debugger which managed to show me the steps through which the engine goes to set position, but didn’t give me any ideas on what to try and clear out before or during the animation phase.

I am interested in the vehicle-physics example but am trying to digest the engine and the ideal way to use it bit by bit, and that one is a little more involved than I thought I’d need to tackle right now, if it ends up being the only way, I’ll try to parse its code and adapt to my own, for sure.

If I’d do it with a compound shape:

Detaching:

  1. Create a separate wheel with dynamic rigidbody and collision components, which I would clone later and disable it.
  2. Save linear/angular velocities (so I can restore them later) and disable compound entity.
  3. Disable wheel child.
  4. Clone the wheel I prepared, add it to app root, move to current wheel position/rotation. Then enable it (important to do it after you move it, since it is dynamic).
  5. Enable compound entity and apply saved linear/angular velocities.

Now you should have a compound entity, with a detached wheel.

Attaching:

  1. Disable rigidbody and collision components on the wheel, so we don’t collide with the car body, but still have visual mesh.
  2. Lerp position/rotation to where the wheel should be. You can getPosition/getRotation of the disabled child.
  3. Once lerp is over, destroy the wheel entity.
  4. Save lin/ang velocities and disable compound entity
  5. Enable compound child and enable compound entity, restore velocities.

Now you should have the wheel back as a compound child.

1 Like