# shoot ball in opposite direction

## Recommended Posts

Hi,

I am building a football game. When the player presses space and he is next to the ball, i want to apply a force in the opposite direction. With flash and NAPE physics I did something like this:

``````if (!ballHit && Vec2.distance( ball.body.position, playerBody.position ) <= ball.radius + extra )
{
ballHit = true;
difference = ball.body.position.sub( playerBody.position );
difference.length = BALL_SPEED;
}``````

With Phaser, I have the following but I am kinda stuck

`````` distance = Phaser.Point.distance(player.position, ball.position);

if (distance < 30) {
difference = Phaser.Point.subtract(player.position, ball.position);
difference.length = 500;
console.log(difference);
}``````

Could you help me?

##### Share on other sites

Hi, if you are using physics (arcade or P2), then the syntax for adding velocity to a body is:

``````            ball.body.velocity.x+=-difference.x;
ball.body.velocity.y+=-difference.y;``````

Here is an extra example in case you might be stuck (including drag to slow down ball):

##### Share on other sites

Hi,

Thanks for the sample. I tested it and it acted odd. If the player was close to the ball the velocity increased slowly than if the player was farther from the ball.

``````distance = Phaser.Point.distance(player.position, ball.position);
if (player.getShooting() && !ballHit && distance < player.width) {
ballHit = true;
var angle = Phaser.Math.angleBetweenPoints(player.position, ball.position);
ball.body.velocity.x += Math.cos(angle) * BALL_SPEED;
ball.body.velocity.y += Math.sin(angle) * BALL_SPEED;
}``````

This code here seems to be more accurate to what I was expecting. If the player is closer to the ball, it increases the velocity much more than if the player would be farther.

##### Share on other sites

I see your point, I was approaching it kind of differently.. sort of like kicking the ball from larger distance would give you a higher kick strength (more like a free kick/penalty).. But your solution would be the right way now that I think of it (if your BALL_SPEED is a function of the distance , that is).

##### Share on other sites

@samme

Yes, you are right.

``````distance = Phaser.Point.distance(player.position, ball.position);
if (player.getShooting() && !ballHit && distance < player.width) {
ballHit = true;
var angle = Phaser.Math.angleBetweenPoints(player.position, ball.position);
ball.body.velocity.x += Math.cos(angle) * BALL_SPEED;
ball.body.velocity.y += Math.sin(angle) * BALL_SPEED;

// difference = Phaser.Point.subtract(player.position, ball.position);
// difference.setMagnitude(BALL_SPEED);
// ball.body.velocity.x += -difference.x;
// ball.body.velocity.y += -difference.y;
}``````

Both solutions output the same result.

##### Share on other sites

@sammenice one I did not know about setting the magnitude.... @mironcaius a little bit of nitpicking, but setMagnitude() boils down to one Math.sqrt function call , so I would pick that one if it produces the exact same result.