Physics: Tween no effect on Collider?

Physics: Tween no effect on Collider? Am I right concluding that Nodes animated via Tween have no effect on Colliders? Specifically I have a node A with a boxcollider2d and rigidbody (type:static) and boxcollider2d set as sensor. I have another node B with rigidbody (type:kinematic) and boxcollider.

Physics used in settings: Box2D

Scenario 1:

  • Node A with Rigidbody (type: Static); BoxCollider2D set as sensor
  • Node B with Rigidbody (type: Dynamic)
    => Node B falls into the sensor area of Node A. Node A’s callbacks DO trigger.

Scenario 2:

  • Node A with Rigidbody (type: Static); BoxCollider2D set as sensor
  • Node B with Rigidbody (type: Kinematic)
    => Node B is animated via Tween into the position of Node A’s sensor area. Node A’s callback DO NOT trigger. Why?

When I animate B to the position of A , nothing happens, ie the callbacks which triggered without problem in scenario 1 do not trigger.

According to the documentation Cocos Creator 3.8 Manual - 2D RigidBody

If a node has a RigidBody2D component and the RigidBody type is Dynamic(which is the default value), it can’t be moved by setting its position.

If you want to change the position manually, the following steps may help:

  1. set the rigidbody type to Kinematic
  2. change the position by code.
  3. restore the rigidbody type to Dynamic after the position change is complete.
1 Like

Thank you for the reply.
My fault not to mention it (will edit the question). The type of Rigidbody is kinematic.
The problem arises only when animating Node B into the collider(sensor) area of Node A via Tweens. In this case, the callbacks of the sensor of Node A do not trigger. But they trigger when I use regular physics with type dynamic and let B drop in the area of node A.

@neocoretec what type of event did you listen on. As sensor doesn’t trigger collide event.

Hi zhangxm. Thanks for the reply.
I’m was using the following code with the sensor:

onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact : IPhysics2DContact | null) {
// code here
}

This code works fine when the otherCollider’s Rigidbody is of type:Dynamic. It stops working when the type is set to Kinematic.

I have changed the overall logic of my code and I’m using tweens instead of physics. But for future reference it is valuable to know why the problem occurs.

Got it, i will take a look.

@neocoretec i tested with v3.8.2(which will be released soon):

  • there are two rigid bodies A, one is is static(call it A), anther one(call it B) is Kinematic and set to sensor
  • listen begin contact event on B, and it works

Or could you provide a test case that reproduced the issue?

Every tile (dark brown) in the background is a sensor, sensing the diamond type whenever a diamond “touches” the tile’s collider.
Instead of animating each diamond via tween I wanted physics do the job. So each diamond is sitting above another stone until the diamond below stops existing, in which case every diamond above is falling down to a hidden ground below the bottom tiles.
In this setup each diamond had a RB with type Dynamic and the tile behind had a RB of type static. When I change the type of the diamond to Kinematic, and animate the stone via tween, the tiles stop sensing the stone. This happens with Box2D.
I know the whole setup is nonsense in the first place, I was playing around to test some ideas (I changed everything in the new version, using tweens and builtin physics.). But nevertheless the problem occurred exactly then. Change from Dynamic to Kinematic, and sensors stop triggering although stones are exactly on top of them.

The code below should not distract. There is nothing to see here. When a diamond is clicked, it gets destroyed. Each Tile has access to the diamond occupying the position, so it can call moveDown() from the diamond script. Nothing special. Very simple (although confusing for someone not knowing the relationship between nodes). When testing (with type kinematic) the stones fall to the position of the proper tiles, but the sensors do not trigger.

**Again, thank you for your time and your effort @zhangxm *. As I said, I changed everything, the whole logic, and code, and everything described here do not apply anymore. It is only interesting to see from where the problem arises. Should I delete this entry?

From Tile.ts:

onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact : IPhysics2DContact | null) {
        let stone = otherCollider.node;
        let id = stone.getComponent("Stone").getID();
        this.setState(id);
        ....
    }

From Stone (diamond):

onClick(event : EventMouse){
        this.node.getParent().destroy();
    }

    moveDown(pos : Vec3) {
        let tweenDuration : number = 0.5;
        
        tween(this.parent.position)
            .to(tweenDuration, new Vec3(200, 200, 0), {
                onUpdate : (target:Vec3, ratio:number) =>{
                    this.parent.position = target;
                }
            }).start();
    }

It is ok to keep the issue. And it is better you provide a demo when you meet it again.

I was not allowed to upload a video, so I shared the screenshot above. The rest is console output, which logs out the color of the stone “console.log(this.getID()”, triggered inside the “onBeginContact” callback above.
Again, thank you for your effort. I appreciate it.