Jump to content

Collision Between the same group


non_tech_guy
 Share

Recommended Posts

Hi,

 

I have two groups, one is players that move from right to left. The other is opponents that move from left to right. The two groups collide which is exactly what I want. I also want the players in the players_group to collide with each other and the opponents in the opponents_group to collide with each other. The idea is that you can drop players in lanes and they push the opponents backwards if there are more players in the lane than opponents.

 

Currently some of the players overlap each other rather than stacking up against each other when they collide.

 

I currently have the following code in my update function

 

this.game.physics.arcade.collide(this.player_group, this.opponent_group); 

 

Thx.

 

Link to comment
Share on other sites

Thanks for your feedback.

 

I have noticed that this still produces some on the group elements to overlap so I have included my update function below. There are basically 3 types of players and it seems that players of the same type can still overlap. Players are move in and out of arrays as they are added and removed from the field and I have excluded the opponents code from the update function below.

 

update:function(){
        this.game.physics.arcade.collide(this.player_group); 
        this.game.physics.arcade.collide(this.opponent_group);
        
        this.game.physics.arcade.collide(this.player_group, this.opponent_group); 
        
        for(var i=0;i<this.forwardsOnField.length;i++){
            if(this.forwardsOnField.body.x > -150){
                this.forwardsOnField.body.velocity.x = this.forwardsOnField.getSpeed();
            }else{
                this.forwardsOnField.body.velocity.x = 0;
                this.forwardsOnField.body.x = this.game.width;
                this.forwardsOffField.push(this.forwardsOnField);
                this.forwardsOnField.splice(i,1);
                this.forwardsLabel.text = this.forwardsOffField.length;
            }
        }
        
        for(var j=0;j<this.backsOnField.length;j++){
            if(this.backsOnField[j].body.x > -75){
                this.backsOnField[j].body.velocity.x = this.backsOnField[j].getSpeed();
            }else{
                this.backsOnField[j].body.velocity.x = 0;
                this.backsOnField[j].body.x = this.game.width;
                this.backsOffField.push(this.backsOnField[j]);
                this.backsOnField.splice(j,1);
                this.backsLabel.text = this.backsOffField.length;
            }
        }
        
        for(var k=0;k<this.scrumhalfOnField.length;k++){
            if(this.scrumhalfOnField[k].body.x > -75){
                this.scrumhalfOnField[k].body.velocity.x = this.scrumhalfOnField[k].getSpeed();
            }else{
                this.scrumhalfOnField[k].body.velocity.x = 0;
                this.scrumhalfOnField[k].body.x = this.game.width;
                this.scrumhalfOffField.push(this.scrumhalfOnField[k]);
                this.scrumhalfOnField.splice(k,1);
                this.scrumhalfLabel.text = this.scrumhalfOffField.length;
            }
        }
    },
Link to comment
Share on other sites

I think the most efficient way to handle this would be to have ALL collidable objects in a single array (in addition to whatever other arrays are needed) and do a single collide call on that:

game.physics.arcade.collide(this.all_collidable);

It's hard to say what else could be causing things to overlap - I suspect the best course of action if the above doesn't work is to strip your code down to the bare essentials and test each part of it as individually as you can.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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