Matter Collision Accuracy problem?

i am trying to make physic-based ball scooping game.

tried the basic example on balls and platform provided within example. but the collision doesn’t seems to work properly there are case where the scoop just overlap with the ball, and case where the ball jumped out of bound


the code simply looked like below. where the bar is a scoop rotated on update interval

create = ()=>{

    this.bar = this.matter.add.image(200,600, 'loading_bar', 0,{angle:-0.2,isStatic:true})
    this.bar.setRectangle(400, 20, {isStatic:true});
    this.matter.world.setBounds(0, 0, 400, 800 , 32, true, true, false, true);
    for (let i = 0; i < 15; i++)
    {
        const ball = this.matter.add.image(Math.Between(100, 100), Math.Between(300, 200), 'circle');
        ball.setScale(0.2);
        ball.setCircle(ball.displayWidth/2);
        ball.setFriction(0);
        ball.setBounce(1);
        ball.setTint(0xff0000)

    }
    // this.input.on('pointerdown',()=> bar.setAngle(bar.angle- 10))
}

update = (time, dt) =>{
    this.bar.setAngle(this.bar.angle- 10* dt/1000)
}

did i do something wrong ?
thanks beforehand

It seems to me that the bar has no momentum. You never apply force, you set it as static, which gives it infinite mass and inertia. When you then place it on top of existing objects, the engine has no clue what to do. Setting the Angle does not give the bar a velocity.

1 Like

is there any way to add spin velocity ?
im still doesn’t understand how to do stuff with physics and mostly following the example

What ‘basic example on balls’ are you referring to?

You need a revolute joint.
The only example I see is this one.
To ‘spin’ it, set an angular velocity or apply a force.

1 Like

these one : ball example

i’ll try reading the example.
but i haven’t even played with base matter js(without phaser). so i guess i need to research more about this.

I guess you could use worldConstraint.

after additional searching and triage it seems like the main problem comes from enableSleeping configuration. it works just fine as soon as i disabled those.

i also found these which helps a lot

It is a bit of a hack. As long as it’s slow it seems to work. If you would speed up the bar, setAngle would position it on top (through) other objects. No collision check is done between last and current angle. The correct way is with a constraint.

1 Like

i see, and yeah you are right with faster speed it won’t give accurate representation of spinning. i’ve managed to set up the constraint. but still confused about how to add angular velocity to spin the scoop.

ahh i actually did it ! :smiley:

below are the code written to achieve those.

create() {
    this.bar = this.matter.add.image(this.scale.width / 2, this.scale.height / 2, 'loading_bar', 0, {density: 10})
    this.bar.setRectangle(this.scale.width * 0.7, 1, {density: 1});

    // this.bar.setAngularVelocity(1);
    let constraint = Phaser.Physics.Matter.Matter.Constraint.create({
        bodyA: this.bar.body,
        pointB: {x:this.bar.x, y:this.bar.y},
        length: 0,
        stiffness: 0
    });
    this.matter.world.add(constraint); 
}
update = (time, dt) => {
    this.bar.setAngularVelocity(0.1);
}

thanks a lot for the help :+1: