Teleporting rigid bodies

Hello everyone.
In my game, I’m using this.entity.rigidbody.teleport(this.startPosition); for resetting the basketball position once it goes out of screen. But the problem is the ball moves forward or backward when after resetting like it has the previous momentum before the reset.
I also tried disabling the rigid body and set the player position to the initial position and enabling the rigid body, but it didn’t work.

    this.entity.rigidbody.linearVelocity = pc.Vec3.ZERO;
    
    this.gameStart = false;
    this.entity.tween({}).to({}, 3, pc.Linear).on('complete', function(){
        this.entity.rigidbody.enabled = false;
        this.entity.setPosition(this.startPosition);
        this.indicator.script.indicator.reset();
        this.entity.rigidbody.enabled = true;
        // this.entity.parent.script.template.spawn();
    }, this).start();

and

    this.entity.rigidbody.linearVelocity = pc.Vec3.ZERO;
    
    this.gameStart = false;
    this.entity.tween({}).to({}, 3, pc.Linear).on('complete', function(){
        this.entity.rigidbody.teleport(this.startPosition, pc.Vec3.ZERO);
        this.indicator.script.indicator.reset();
    }, this).start();

Project link: PlayCanvas | HTML5 Game Engine

Hi @maheshnaik,

Can you try resetting the velocity of the body as well? That will stop all momentum:

this.entity.rigidbody.linearVelocity = pc.Vec3.ZERO;
this.entity.rigidbody.angularVelocity = pc.Vec3.ZERO;

@Leonidas I tried that but it didn’t work.

You should reset the angular velocity as well, not only the linear one. This reset method works for me, the ball resets and remains in place:

Ball.prototype.reset = function(){
    
    this.entity.rigidbody.teleport(this.startPosition, pc.Vec3.ZERO);
    
    this.entity.rigidbody.linearVelocity = pc.Vec3.ZERO;
    this.entity.rigidbody.angularVelocity = pc.Vec3.ZERO;
};

@Leonidas Thank you, it works fine now.
I was resetting the velocity first and then I was teleporting, instead of teleporting first and resetting it.

1 Like

This should not matter, you can teleport first and then reset velocity, or first reset and then teleport. It is same for the physics engine.

1 Like

But in my case, it was not the same. I will look at that part of the code again.

When I tried a code change in your project, the angular velocity wasn’t set to ZERO and the ball started to roll after the teleport. I think that was the case with that bug.

1 Like

Yes, and also that I was resetting the velocity and calling the teleport() after 2 second delay. When I removed the delay and set both the velocity to zero, it works fine. Thank you