mironcaius Posted May 20, 2017 Share Posted May 20, 2017 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; ball.body.velocity.addeq( difference ); } 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? Link to comment Share on other sites More sharing options...
samid737 Posted May 22, 2017 Share Posted May 22, 2017 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): Link to comment Share on other sites More sharing options...
mironcaius Posted May 22, 2017 Author Share Posted May 22, 2017 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. Link to comment Share on other sites More sharing options...
samid737 Posted May 22, 2017 Share Posted May 22, 2017 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). Link to comment Share on other sites More sharing options...
samme Posted May 22, 2017 Share Posted May 22, 2017 I think you may be looking for difference.setMagnitude(500); Link to comment Share on other sites More sharing options...
mironcaius Posted May 22, 2017 Author Share Posted May 22, 2017 @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. Link to comment Share on other sites More sharing options...
samid737 Posted May 23, 2017 Share Posted May 23, 2017 @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. Link to comment Share on other sites More sharing options...
Recommended Posts