Skip to content

Ball Launch from Platform

noooway edited this page Mar 23, 2017 · 26 revisions

Traditionally, in arkanoid a level is started with the ball glued to the platform. The ball is launched, when the player presses a key. In this part, I want to implement such a behavior.

At first sight, to glue the ball to the platform, it is possible to set it's velocity to zero and make it react on arrow keys similarly to the platform (i.e. move left or right with the same speed as the platform). With such an approach, when the platform with the glued ball is pressed against the wall, the ball slides over the platform. There is nothing wrong with it, but I want a traditional mechanics, where the ball doesn't move over the platform. In order to do this, we can fix a distance between the centers of the ball and the platform. When platform moves, we update ball position relatively to the platform. So, in the Ball class we need a stuck_to_platform flag and a separation_from_platform_center distance. Level is started with ball stuck to platform. So we need an initial displacement, which is set as default for platform-ball distance. Default value for flag is true.

local ball_x_shift = -28
local platform_height = 16
local platform_starting_pos = vector( 300, 500 )
ball.stuck_to_platform = true
ball.separation_from_platform_center = vector(
   ball_x_shift, -1 * platform_height / 2 - ball.radius - 1 )
ball.position = platform_starting_pos +
   ball.separation_from_platform_center

In fact, there is no need to worry about the initial platform coordinates, since the ball will be positioned relative to platform center automatically on first call to update method.

In ball.update we need to insert check of the stuck_to_platform flag. If it is on, we need to set ball position relative to platform position. Unfortunately, to do this, we also have to pass platform coordinates to ball.update. There is no way to avoid this, unfortunately. It is easier to pass whole platform object alltogether instead of just coordinates.

function ball.update( dt, platform )
   ball.position = ball.position + ball.speed * dt
   if ball.stuck_to_platform then
      ball.follow_platform( platform )
   end
   ball.check_escape_from_screen()   
end

function ball.follow_platform( platform )   
   local platform_center = vector(
      platform.position.x + platform.width / 2,
      platform.position.y + platform.height / 2 )
   ball.position = platform_center + ball.separation_from_platform_center
end

function game.update( dt )
   ball.update( dt, platform )
   platform.update( dt )
   .....
end

Finally, we have to launch the ball on key press. This function just drops stuck_to_platform flag and sets nonzero velocity to the ball. Initial velocity have to be stored in the Ball class. For now, we can simply insert a keypress callback in the Ball class, but we'll have to modify this arrangement in the next part.

.....
local first_launch_speed = vector( -150, -300 )
ball.speed = vector( 0, 0 )
ball.image = love.graphics.newImage( "img/800x600/ball.png" )
.....

function ball.launch_from_platform()
   if ball.stuck_to_platform then
      ball.stuck_to_platform = false
      ball.speed = first_launch_speed:clone()
   end
end

function game.keyreleased( key, code )
   .....
   elseif key == ' ' then
      ball.launch_from_platform()
   elseif .....
end

When the ball is lost, it is necessary to reposition it on the platform. The ball position relative to the platform will be set automatically at the ball.update.

function ball.reposition()
   ball.escaped_screen = false
   ball.collision_counter = 0
   ball.stuck_to_platform = true
   ball.speed = vector( 0, 0 )
end

    Home
    Acknowledgements
    Todo

Chapter 1: Prototype

  1. The Ball, The Brick, The Platform
  2. Game Objects as Lua Tables
  3. Bricks and Walls
  4. Detecting Collisions
  5. Resolving Collisions
  6. Levels

    Appendix A: Storing Levels as Strings
    Appendix B: Optimized Collision Detection (draft)

Chapter 2: General Code Structure

  1. Splitting Code into Several Files
  2. Loading Levels from Files
  3. Straightforward Gamestates
  4. Advanced Gamestates
  5. Basic Tiles
  6. Different Brick Types
  7. Basic Sound
  8. Game Over

    Appendix C: Stricter Modules (draft)
    Appendix D-1: Intro to Classes (draft)
    Appendix D-2: Chapter 2 Using Classes.

Chapter 3 (deprecated): Details

  1. Improved Ball Rebounds
  2. Ball Launch From Platform (Two Objects Moving Together)
  3. Mouse Controls
  4. Spawning Bonuses
  5. Bonus Effects
  6. Glue Bonus
  7. Add New Ball Bonus
  8. Life and Next Level Bonuses
  9. Random Bonuses
  10. Menu Buttons
  11. Wall Tiles
  12. Side Panel
  13. Score
  14. Fonts
  15. More Sounds
  16. Final Screen
  17. Packaging

    Appendix D: GUI Layouts
    Appendix E: Love-release and Love.js

Beyond Programming:

  1. Game Design
  2. Minimal Marketing (draft)
  3. Finding a Team (draft)

Archive

Clone this wiki locally