Mutual Displace in Game Lab

I am trying to create a sumo-wrestling type game in Game Lab. I want WASD and the arrow keys to guide two sprites. I want to have the sprites try to push each other out of a circle. It seems that displace only works for one sprite on another. Is there a way I can get sprites to mutually displace? So that if they were directly next to each other and moving in opposite directions, their displace would just cancel out? But either one could push the other one if they were the only one holding down a key? This would probably be boring with square colliders, but could be interesting with circular colliders.

Tim

I did get it to sort of work by setting up a count variable and doing the following:

if(count % 2 == 0){
count = count + 1;
player.displace(player2);
}
else {
count = count + 1;
player2.displace(player);
}

So it essentially alternates the displace with each frame. But if there is a better way, someone please let me know!

Hi @timothy.lee,

Sounds like a fun game! I like your solution with the use of modulus. I also like that you posted your solution. It is a reminder that just simply talking/typing through a solution can help you problem solve even further. It is basically like rubber duck debugging. :hatched_chick::slight_smile:

Perhaps another solution might be to change the velocity of the players on a collide. You could get them to stop if they change their x position by greater than some number. Or you could relocate them randomly on a collide? Just some ideas. If you post a share link to your code, it might help others better visualize ideas.

Good luck!
~Michelle

I found a better way, in the movement code just add:
player.displace(player2)
player2.displace(player)

if (keyDown("left")) {
     player.x = player.x - 5;
      player.displace(player2);
    }
    if (keyDown("right")) {
      player.x = player.x + 5;
      player.displace(player2);
    }
    if (keyDown("a")) {
      player2.x = player2.x - 5;
      player2.displace(player);
    }
    if (keyDown("d")) {
      player2.x = player2.x + 5;
      player2.displace(player);
    }
1 Like

That was better and works! Thanks! I’ll post the finished game shortly :slight_smile:

I’m glad to know about that!