Skip to content

Ball Launch from Platform

noooway edited this page May 15, 2017 · 26 revisions

Traditionally, in arkanoid a level is started with the ball glued to the platform. When the player presses a key, the ball is launched. 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. To implement it, a different strategy is required. For example, it is possible to fix a distance between the centers of the ball and the platform and when the platform moves, update the ball position relatively to the platform. This requires to store a stuck_to_platform flag and a separation_from_platform_center vector in the ball table. Since a level is started with the ball glued to the platform, default value for the ball.stuck_to_platform is true. Also, it is necessary to provide some initial ball-platform displacement.

local ball_x_shift = -28
local platform_height = 16
local platform_starting_pos = vector( 300, 500 )              --(*1)
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  --(*1)

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

In the ball.update it is necessary to insert a check for the stuck_to_platform flag. If it is on, the ball should be positioned relatively to the platform. This requires to pass the platform coordinates into the ball.update. I pass the the whole platform object instead of coordinates only, because other platform properties will be necessary in the future.

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, the ball has to be launched on a key press. An appropriate function in the game.keyreleased callback drops the stuck_to_platform flag and sets nonzero velocity to the ball (some initial velocity has to be stored in the ball table).

.....
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 == 'space' or key == ' ' then   --(*1)
      ball.launch_from_platform()
   elseif .....
end

(*1): Space key is represented as 'space' in love-0.10 and as ' ' in love-0.9.

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 during the first call to 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