Jump to content

Collision problem between 2 sprites in the same collision group


Recommended Posts

hello !


I have a problem to make collision between 2 sprites works.


I use P2 physics system and my main sprite is a player and the other sprite is also player but they are loaded after (it's a multi player game).


Init of the map :

this.game.stage.backgroundColor = '#2f8136';        this.map = this.game.add.tilemap('map'); // Preloaded tilemap        this.map.addTilesetImage('grass'); // Preloaded tileset        this.map.addTilesetImage('treetop'); // Preloaded tileset        this.map.addTilesetImage('trunk'); // Preloaded tileset        this.map.addTilesetImage('watergrass'); // Preloaded tileset        this.layerBackground = this.map.createLayer('background'); // This is the default name of the first layer in Tiled        this.layerBackground.resizeWorld(); // Sets the world size to match the size of this layer.        this.layerForeground = this.map.createLayer('foreground'); // This is the default name of the first layer in Tiled        this.layerForeground.resizeWorld(); // Sets the world size to match the size of this layer.        this.layerTop = this.map.createLayer('top'); // This is the default name of the first layer in Tiled        this.layerTop.resizeWorld(); // Sets the world size to match the size of this layer.        this.map.setCollisionBetween(1, 300, true, 'foreground');

Init of the game :

this.game.world.setBounds(0, 0, 2000, 2000); //Permet de definir la taille et les bords de la carte        this.game.physics.startSystem(Phaser.Physics.P2JS);        this.game.physics.p2.setBoundsToWorld(true, true, true, true, false);this.game.physics.p2.updateBoundsCollisionGroup();

Init of collision group :

playerCG = this.game.physics.p2.createCollisionGroup();

Init of player (create) :

this.sprite = this.game.add.sprite(200, 200, 'player');        //Definition de l'echelle :        this.sprite.scale.x = 0.9;        this.sprite.scale.y = 0.9;        //Definition des animations :        this.sprite.animations.add('left', [9, 10, 11, 12, 13, 14, 15, 16, 17], 20, true);        this.sprite.animations.add('right', [27, 28, 29, 30, 31, 32, 33, 34, 35], 20, true);        this.sprite.animations.add('up', [0, 1, 2, 3, 4, 5, 6, 7, 8], 20, true);        this.sprite.animations.add('down', [18, 19, 20, 21, 22, 23, 24, 25, 26], 20, true);        //On active le physic sur le sprite (permet d'utiliser fixedRotation....)        this.game.physics.p2.enable(this.sprite);        this.sprite.body.setRectangle(22, 7); //permet de redefinir la taille du body du perso(util pour les collisions par ex)        this.sprite.anchor.setTo(0.5, 0.95); //permet de redifinir la position du sprite        this.sprite.body.fixedRotation = true; //permet de fixer la rotation du perso (cela arrive en bordure de terrain que le perso tourne sur lui meme !)        this.sprite.body.setZeroDamping();        this.sprite.id = index;        //Permet de prendre en charge le clavier :        this.cursors = this.game.input.keyboard.createCursorKeys();        //Gestion des collisions :        this.sprite.body.setCollisionGroup(playerCG);        this.sprite.body.collides([levelCG, playerCG]);        this.game.physics.p2.updateBoundsCollisionGroup();

Player collision work with tiles and word bound but do not work with other player and I don't understand why...

Someone can help me please ?


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...