Is there an easy way to add collision when dragging items?

In my game I have a set of objects that the user can drag around with the pointer, and another set that they can’t. I want to be able to have the two sets collide with one another so that the user can’t drag one onto the other. Here’s my create code:

let dynamicGroup = this.physics.add.group();
let staticGroup = this.physics.add.staticGroup();

...populate groups...

this.physics.add.collider(staticGroup, dynamicGroup);

Regardless, the collision detection doesn’t work. I’ve tried just putting them all in the same group, manually setting collisions with each other, and yet I can’t seem to get it to work.

Turn on physics debug.

I did. The objects can move into each other without any output in the console.

your assumption is everyone already knows how to do that. I don’t please explain how to turn on the physics debug or point me to some documentation and I’ll teach myself. Thanks.

You turn on the physics debug by modifying the game config:

// main.js
let config = {
    type: ...
    ...
    physics: {
         default: 'arcade',
         arcade: {
              debug: true // This is where debug is turned on for arcade physics
         }
    }
}

let game = new Phaser.Game(config)
3 Likes

So I’ve realized that I haven’t included the code for dragging. Here it is:

this.input.on('drag', (pointer, gameObject, dragX, dragY) => {
	gameObject.x = dragX;
	gameObject.y = dragY;
});

When I setup a collider that fires a function when a collision occurs, I get that function to fire, which tells me that (most likely) the draggable game objects go where the pointer takes them, regardless of a detection of collision.

1 Like

hy man,
have you solved this ? I have the same question …

I don’t think there is an easy way to do this. I got it partly working by checking collisions (twice) during the drag update and setting the body deltas manually.

The problem here is that arcade physics cannot collide if velocity to that direction is zero. So anything not moving with physics. The solution might not using colliders but use Phaser.Geom. Intersects and sprite.getBounds() in update function similar to this phaser 2 example https://phaser.io/examples/v2/sprites/overlap-without-physics .

2 Likes

Hey there,
Has anyone made it work, after 3 years? :slight_smile: