Performance issues with rigidbody.teleport()

2D Game, top view with fixed orthogonal camera. Player entity (a spaceship) with a rigidbody, moves along x and z axes. I’m trying to limit the ship movements so that it doesn’t go off the screen. In the Unity tutorial I’m following, they have this really neat way of doing it:

rigidbody.position = new Vector3 
            Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax), 
            Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)

I first tried converting this code in PlayCanvas with something like this:

    pc.math.clamp(this.entity.getPosition().x, this.boundary.xMin, this.boundary.xMax), 
    pc.math.clamp(this.entity.getPosition().z, this.boundary.zMin, this.boundary.zMax)

and quickly realized that entity.setPosition() doesn’t work on entities with a rigid body component. So I ended up substituting entity.setPosition() with entity.rigidbody.teleport(), which worked… kind of. It does what it is supposed to do but now the player entity moves much slower and kind of stutters at times. Any idea why that is?

Can you link to the project?

Teleport should be fine.
It is an intermittent problem and seems to happen more when playing from the editor.

As you are using linear velocity to move the rigidBody, you shouldn’t use teleport to then place the object. Since PlayCanvas doesn’t have a separate setPosition and setRotation for the rigidBody, I pulled the code out from the engine to just do the rotation.

Strictly speaking, it’s not ideal to be doing this every frame and we should be using apply torque to rotate the rigidBody but this will do for now.

This is a fork with the updated code:

I also changed up some of the logic so that you are not allocating variables and functions every update call.

On my Mac with Chrome, I don’t get any stutter but on my Windows with Chrome, I dropped frames every now and then. Looking at the profiler I couldn’t see why since the CPU time was still 4ms and the other 300ms wasn’t accounted for :confused:

Thanks for taking a look @yaustar. I’m curious, why is it that I shouldn’t use teleport to place the object because I use linear velocity to move it?

Also, I’ve noticed something weird, while keeping my original code: the slow down and stutter of the player movement usually only happens right after the game loads. If you play for awhile it fixes itself. Or if you reload the browser a few times, it seems to get incrementally better. Odd.

Also, while running the profiler, I noticed that the FPS was at a pretty steady 60 even when experiencing player movement slowdowns and stutters. What’s more, if you try my latest build, which has many other moving objects on the screen, you’ll notice that these other objects are not affected and move at the same steady speed even when the player object is experiencing slowdowns and stuttering. Weird.

Oh, you are talking about that stutter? Thats more todo with the teleporting to position every frame. If you try my version, It doesn’t get that ‘stutter’.

Usually with physics engines, teleporting is not recommended because it acts like it no longer exists in it’s previous position and then suddenly ‘pops’ into the new position as far as the physics simulation is concerned. This can lead to odd collision responses and behaviour on how it moves as you saw from the ‘stuttering’ of movement of the ship.

TL;DR: If you want to move a physics object, use apply forces/velocity/torque. If you need to reset a physics object, use teleport.

1 Like