[1.5 Physics] Ball stuck in sand (sensor) even when outside

Hi,

How can I test wether the ball is inside or outside a sensor that I have? The code that I’m using is very unreliable due to me checking in the Update function and hence being dependant on frame rates.

As you can see the balls building box is extended (being swept) between frames and the ball still thinks it’s on the sand even when it has come to a halt. (Maybe it’s a little hard to see but there is a purple debugging box connecting the ball to the sand.)

The code I’m currently using keeps saying isTouching is true when the ball is outside the sand.

Update() {
  if (this.rb._b2Body.m_contactList && this.rb._b2Body.m_contactList.other.m_userData) {
    // let isTouchingCollider = this.rb._b2Body.GetContactList().other.m_fixtureList.TestPoint(this.rb._b2Body.GetWorldPoint(this.rb._b2Body.GetLocalCenter()));
    let isTouchingCollider = this.rb._b2Body.GetContactList().contact.IsTouching();
    if (!isTouchingCollider) {
      return;
    }
    if (userData.type == ColliderType.MGEntityTypeSand) {
      if (this.cl.body.linearVelocity.x != 0 && this.cl.body.linearVelocity.y != 0) {
        var point = new cc.Vec2(0, 0);
        var oppositeForceMultiplier = -30;
        let oppositeForce = new cc.Vec2(this.cl.body.linearVelocity.x / this.sandFriction * oppositeForceMultiplier, this.cl.body.linearVelocity.y / this.sandFriction * oppositeForceMultiplier);
        this.rb.applyForce(oppositeForce, point, true);
      }
    }
  }
}

If Cocos Creator doesn’t have a FixedUpdate method then how would I check, independently of frame rate, where the ball currently is?

Many thanks in advance!

Hi,
I think a easier way is apply force to the ball in the collision callback onPostSolve.

@jyinkailej

Thanks for the suggestion but onPostSolve is not called when entering or exiting sensors.
I would need something that is called continuously so with every update I can check what type of sensor the ball is rolling on.

But thanks anyway.

onBeginContact is called when entering sensors and onEndContact is called when exiting sensors.
You can record the collision flag in these two collision callback, and check the flag in update function.

@jyinkailej
That was the way to do it. Thanks!