I am learning game programming with Phaser and I am currently building a simple breakout game.
{// Ball is on the left hand side of the paddle diff = paddle.x - ball.x; Ball.body.velocity.x = (-10 * difference); } And if (ball.x> paddle.x) {// Ball paddle is the deficient = ball.x-pedal.x is on the right hand; Ball.body.velocity.x = (10 * difference); } And {// the ball is completely in the middle / / add a little random X to stop it jumping straight up! Ball.body.velocity.x = 2 + Math.Random () * 8; }
This code is originally taken, I believe with Stackworflow, although I do not remember which post I'm afraid of. The problem with this is that when the ball is left at an angle or goes right, it looks fast on the screen, if it goes straight to
Does anyone Also know to solve this problem?
Regards Crouz
ball speed horizontally ( x
) Speed and vertical ( y
) speed, as given by:
z 2 = x 2 + Y 2
where the overall speed will be z
.
If you are less than x
then y
is appropriate, then overall speed
will also increase.
If you want the same speed, you must also adjust the y
example pseudo code:
speed = sqrt (x ^ 2 + y ^ 2) x = 2 + random () * 8 y = sqrt (speed ^ 2 - x ^ 2)
Before you make any adjustment, speed you can add it to your code, and then you can adjust
y
:
// Calculate the full speed of the var speed = Math.Sqrt (Math.pow (ball.body.velocity.x, 2) + Math.pow (ball.body.velocity.y, 2)); If (ball.x> paddle.x) {// Ball paddle is defined = Paddle.x - Ball.x is on the left hand; Ball.body.velocity.x = (-10 * difference); } And if (ball.x> paddle.x) {// Ball paddle is the default = ball.x-pedal.x; Ball.body.velocity.x = (10 * difference); } And {// the ball is completely in the middle / / add a little random X to stop it jumping straight up! Ball.body.velocity.x = 2 + Math.Random () * 8; } // Adjust y to maintain the same overall speed ball. Body.velocity.y = Math.sqrt (Math.pow (speed, 2) - Math.pow (ball.body.velocity.x, 2));
This will always give the result of a positive y
, so depending on which direction you want it to run (up or down) it may have to be rejected < / P>
No comments:
Post a Comment