![imp in a ball imp in a ball](https://i.redd.it/mcwcvi3k43gy.png)
Note that we also move the position of the ball alongside the paddle's position whenever the ball is stuck. Here, if the user presses the space bar, the ball's Stuck variable is set to false. If (Player->Position.x Width - Player->Size.x) This means we have to change the processInput function a little: We select the space key for freeing the ball from the paddle.
![imp in a ball imp in a ball](https://www.wowtcgloot.com/images/screenshots-original/fortune_telling_gm2.jpg)
Imp in a ball update#
Then we have to update the position of the ball each frame by calling its Move function within the game code's Update function:įurthermore, because the ball is initially stuck to the paddle, we have to give the player the ability to remove it from its stuck position. Glm::vec2 ballPos = playerPos + glm::vec2(PLAYER_SIZE.x / 2.0f - BALL_RADIUS,īall = new BallObject(ballPos, BALL_RADIUS, INITIAL_BALL_VELOCITY, As for the texture of the ball, we're going to use an image that makes perfect sense in a LearnOpenGL Breakout game: ball texture.Ĭonst glm::vec2 INITIAL_BALL_VELOCITY(100.0f, -350.0f) Just like the player paddle, we create a BallObject and define two constants that we use to initialize the ball.
Imp in a ball code#
You can find the code for the ball object below:įirst, let's add the ball to the game. We do need to later implement this logic somewhere in the game code though. In addition to reversing the ball's velocity, we also want relocate the ball back along the edge the ball is only able to move if it isn't stuck.īecause the player is game over (or loses a life) if the ball reaches the bottom edge, there is no code to let the ball bounce of the bottom edge. If (this->Position.y Velocity.y = -this->Velocity.y This->Position.x = window_width - this->Size.x If (this->Position.x Velocity.x = -this->Velocity.x Įlse if (this->Position.x + this->Size.x >= window_width) check if outside window bounds if so, reverse velocity and restore at correct position Glm::vec2 BallObject::Move(float dt, unsigned int window_width) It also checks if it reaches any of the scene's edges and if so, reverses the ball's velocity: The BallObject class hosts a Move function that moves the ball based on its velocity. The constructor of BallObject initializes its own values, but also initializes the underlying GameObject. Void Reset(glm::vec2 position, glm::vec2 velocity)
![imp in a ball imp in a ball](https://i.ytimg.com/vi/4eYDXn08Xm4/hqdefault.jpg)
Glm::vec2 Move(float dt, unsigned int window_width) When the game starts, the ball is initially stuck on the player paddle until the player starts the game by pressing some arbitrary key.īecause the ball is effectively a GameObject with a few extra properties it makes sense to create a BallObject class as a subclass of GameObject:īallObject(glm::vec2 pos, float radius, glm::vec2 velocity, Texture2D sprite) In addition to the general game object components, a ball has a radius, and an extra boolean value indicating whether the ball is stuck on the player paddle or it's allowed free movement. The objective is to let the ball collide with all the bricks until each of the destroyable bricks are destroyed, but this all within the condition that the ball is not allowed to reach the bottom edge of the screen. The only thing missing from the classic Breakout recipe is the ball.
Imp in a ball full#
At this point we have a level full of bricks and a movable player paddle.