Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

8 years ago by BennyT

I have a player entity that can pick up other entities (in my sporting game, it is the ball).

the ball attaches itself to the owner and for the time being I am setting the position of the ball entity as so:

			if(this.owner != null) {
				this.pos.x = this.owner.pos.x - 5;
				this.pos.y = this.owner.pos.y - 5;
			}

however when I go to pass or kick the ball, it doesn't progress because of the collision and the existing logic automatically gives the possession back to the player.

To overcome this situation I change the collision properties when the player entity passes the ball:

				// if the player wants to pass right
				if(ig.input.pressed('passright') ) {
					// and the player also has possession of the ball
					if(this.possession === true) {
						this.possession = false;
						ball.owner = null;
						ball.type = ig.Entity.TYPE.NONE;
						ball.checkAgainst = ig.Entity.TYPE.NONE;
						ball.collides = ig.Entity.COLLIDES.NEVER;
						ball.update("right");
					}
				} 

however this has presented a new challenge as now no other players can "catch" the ball.

I tried using the .touch() method, as that just looks for any overlap and ignore the collision state but once again, that automatically gives the player that passed possession.

I guess the real question I am trying to understand is how is the best way to handle collisions between entities when they always have to be on screen and not spawn.

I understand things like spawning bullets etc, because you can get them to spawn ahead of the player/weapon entity, but the ball is always in play in a sports game.

Thanks for the help in advance!

8 years ago by Joncom

Maybe let all player entities catch the ball by default. And when a specific player catches the ball, turn off player-ball-collsion for that player only. In that entity, every frame, check if he is still touching the ball. As soon as his collision box is no longer touching the ball collision box (which will happen when he throws it or whatever), then turn player-ball-collision back on so he is ready to catch it again.

8 years ago by BennyT

Hi Joncom,

Thanks again for your help!

I took some of your advice and thought it through and I discovered a solution that worked, however I think it might be a bit expensive to keep running this check over and over again, but I can refactor/optimise later.

I am executing this code inside the sports person entity, rather than the ball entity:

if( !this.touches(ig.game.getEntitiesByType(EntityBall)[0]) ){
ball.type = ig.Entity.COLLIDES.ACTIVE;
ball.checkAgainst = ig.Entity.TYPE.BOTH;
}

So as long as no players are touching the ball, the ball turns on collisions, which means anyone can catch it.

As soon as the player passes again, it will turn off collisions so it can travel.

Thanks for helping me think more laterally about the problem!

8 years ago by Joncom

Glad you found a solution!
Page 1 of 1
« first « previous next › last »