linearVelocity strange behaviour

I’m experimenting with entity motion and collisions, and have found some strange behaviour.,8028520,8028673,8028078,8028674,8028723

Velocity.prototype.update = function(dt) {
    if (this.entity.rigidbody && this.entity.rigidbody.type === pc.BODYTYPE_DYNAMIC){
        var p = this.entity.getPosition();
       //var a = new pc.Vec3(this.direction * 5,0,0);// 

        if(this.direction > 0 && p.x < 2) { 
        else if(this.direction < 0 && p.x > -2) {
            this.entity.rigidbody.linearVelocity = new pc.Vec3();

The actual velocity of the entity seems to depend on the content of the two “empty” lines.
Removing the lines, commenting them or even adding the comment
//this.entity.rigidbody.linearVelocity = new pc.Vec3();
All radically affect the speed and smoothness of the motion.

I’ve tested this in Chrome and Opera with the same result.

Any ideas why it’s happening or how to fix it?

What do you mean by ‘strange’ behaviour?

All radically affect the speed of the motion.

In a nutshell, you shouldn’t be calling this.entity.rigidbody.syncEntityToBody(); every frame as its supposed to be used when you move the physics objects directly and want the entity to match the position. E.g. teleporting. It is also a private member function (despite the lack of a leading underscore).

You can see in my fork here that the motion is a lot smoother than before now that I’ve removed that sync function:


Hah! The one thing that I didn’t try.

Thanks for that. The this.entity.rigidbody.syncEntityToBody(); crept in from some old example somewhere.

I’m still very curious why changes to comments and empty lines make such a difference.

syncEntityToBody sets the physics body to be the position of the entity so that would affect the movement the of the entity in a lot of (potentially undefined ways).

//var a = new pc.Vec3(this.direction * 5,0,0);// 

Shouldn’t do anything


This would change the behaviour of the physics, not sure in what way though. Perhaps the syncEntityToBody also affects the linearVelocity in some way in Ammo that setting the it each frame to be the same ‘smooths’ it out?

Both lines are commented and should have no effect, but I found that changing the comment (to //), removing the comment (with ; ) or removing the line completely had a significant effect on performance.

Removing this.entity.rigidbody.syncEntityToBody(); seems to mitigate any problems.Still, I suspect it’s some weird JavaScript behaviour that’s the cause, rather than being some quirk/bug of PlayCanvas.