-
Notifications
You must be signed in to change notification settings - Fork 17
Ball Launch from Platform
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). However, with such an approach, when the ball is glued and the platform is pressed against the wall, the ball slides over the platform. I want traditional mechanics, where ball doesn't move. 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
Feedback is crucial to improve the tutorial!
Let me know if you have any questions, critique, suggestions or just any other ideas.
Chapter 1: Prototype
- The Ball, The Brick, The Platform
- Game Objects as Lua Tables
- Bricks and Walls
- Detecting Collisions
- Resolving Collisions
- Levels
Appendix A: Storing Levels as Strings
Appendix B: Optimized Collision Detection (draft)
Chapter 2: General Code Structure
- Splitting Code into Several Files
- Loading Levels from Files
- Straightforward Gamestates
- Advanced Gamestates
- Basic Tiles
- Different Brick Types
- Basic Sound
- 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
- Improved Ball Rebounds
- Ball Launch From Platform (Two Objects Moving Together)
- Mouse Controls
- Spawning Bonuses
- Bonus Effects
- Glue Bonus
- Add New Ball Bonus
- Life and Next Level Bonuses
- Random Bonuses
- Menu Buttons
- Wall Tiles
- Side Panel
- Score
- Fonts
- More Sounds
- Final Screen
- Packaging
Appendix D: GUI Layouts
Appendix E: Love-release and Love.js
Beyond Programming: