Collision detection

Hi,
I am new to phaser. I want to create carrom ,where striker hits a coin then all the coins collide .I could hit by striker and could set all coins to move.but all the coins continuously move with bounceX,Y value.The coins keeps on moving,the coin does not settle.can anyone help me how can I fix the problem.

Set bounce smaller than 1, add damping/drag.

I have set the bounce value to .1,the coins move slowly but are not settled

sprite.body.useDamping=true;
sprite.setDrag(0.99);///takes value between 0-1

The coin slows down but doesn’t settle.it keeps on moving slowly

this.physics.add.group({collideWorldBounds: true,bounceX: 0.1,
bounceY: 0.1,damping:true,Dragx: 0.99,DragY :0.99

});

I don’t see ‘damping’ as property for a physics group config. Maybe try setting it manually.

I tried manually adding ,but it didn’t work.i found allowdrag(), but it didn’t work.

One more thing I want to know how can handle collision between group.

image.png

Show how you did it manually? You need to iterate the group and set each body’s useDamping=true.

this.physics.add.collider(object1, object2, callback, null, this);

object1 and object2 can be sprites/groups/layers etc.

pirateship.body.setdamping( true)
pirateship=this.physics.add.group({collideWorldBounds: true,bounceX: 0.1,
bounceY: 0.1,Dragx: 0.99,DragY :0.99,allowdrag:true

});

I used above code for manually setdamping

Hmm, that’s never going to work :frowning:
You can’t set a property of pirateship before you declare pirateship.
Also, pirateship is the group, it has no body.
Look here for an example.
Follow tutorials before you jump in too deep…

// pirateship.body.setdamping( true)
pirateship=this.physics.add.group(
{key: ‘coins’,collideWorldBounds: true,
});
// pirateship.body.usedamp

// pirateship.setbounce(1,1)
// pirateship.body.usedamping=true;
pirateship.create(200,200,‘pirateShipred’);
pirateship.create(214,200,‘pirateShipb1’);
pirateship.create(207,214,‘pirateShipw1’);
pirateship.create(193,186,‘pirateShipw1’);

pirateship.children.iterate(function (child) {
child.setDraping(true)
child.setBounceY(Phaser.Math.FloatBetween(0.4, 0.8));
child.setBounceX(Phaser.Math.FloatBetween(0.4, 0.8));
child.setDragX(0.99);
child.setDragY(0.99);

});

I can see in the documentation setDraping(),but when used says undefined

Damping, not draping…

1 Like

I could resolve the error but now the coins are not moving

setDamping

Learn how to use the documentation.

Now it is working when i child.setDamping(false).

Thanks for your quick response Milton

Hi Milton,

I want to handle two consecutive mouse click
a) on first click ,stricker should move to the mouse position
b) on second click,stricker should hit the coin ie the new mouse position.

here the stricker is placed but the stricker does not hit the coin
here is my code update(){

if(strickerPos==false)
{

if(mouse.isDown&& control==false)
{
console.log(“indown”)
strickerball.x +=(input.x-strickerball.x);
strickerPos==true;
}

}
else{
if(mouse.isDown&& control==false ){
console.log(“second play”)
let angle=Phaser.Math.Angle.Between(strickerball.x,strickerball.y,input.x,input.y);
//rotation cannon with PI/2
strickerball.setRotation(angle+Math.PI/2);
this.physics.moveTo(strickerball,input.x,input.y,500);
strickerPos=false;
control=true;
}
// strickerPos=true;
//}

//check world bounds
if((strickerball.x>/worldBounds.width/400 )||
(strickerball.y>/worldBounds.height/400) ||
strickerball.x<0 ||
strickerball.y<0){
control=false;
}

//for collision
// this.physics.add.overlap(strickerball,pirateship,destroy,null,this);
// this.physics.collide(strickerball,pirateship)
//this.physics.collide(strickerball,[pirateship])
this.physics.add.collider(strickerball, pirateship);
this.physics.add.collider(pirateship,pirateship);
}

}

You’ll have to be more specific :slight_smile:

Be aware that Phaser doesn’t have Continuous Collision Detection, so fast moving objects often ‘tunnel’, skipping collision altogether…

See here.

Hi milton,

why am i getting this rectangular box at the starting coordinates of world bounds
this.physics.world.bounds.setTo(60, 60, 380,380);
how can i remove it?

image.png