public void ResetBall() { ball.x = 0; ball.y = 0; // grabbed the following from the tutorial. it gives the ball a default velocity in a random direction, within 45 degrees on either side. there's likely a better way to do it. ball.yVelocity = (ball.defaultVelocity / 2) - (RXRandom.Float() * ball.defaultVelocity); ball.xVelocity = Mathf.Sqrt((ball.defaultVelocity * ball.defaultVelocity) - (ball.yVelocity * ball.yVelocity)) * (RXRandom.Int(2) * 2 - 1); }
private void ResetBall() { _ball.x = 0; _ball.y = 0; // Ensure that the ball starts at a random angle that is never greater than 45 degrees from 0 in either direction _ball.yVelocity = (_ball.defaultVelocity / 2) - (RXRandom.Float() * _ball.defaultVelocity); // Make sure that the defaultVelocity (hypotenuse) is honored by setting the xVelocity accordingly, then choose a random horizontal direction _ball.xVelocity = Mathf.Sqrt((_ball.defaultVelocity * _ball.defaultVelocity) - (_ball.yVelocity * _ball.yVelocity)) * (RXRandom.Int(2) * 2 - 1); }