You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In pong-5/Ball,lua, the ball velocity along the x-axis and y-axis is not so good compared to pong-4. The ball will either has -100 or 100 velocity along y-axis but will have a random velocity from -50 to 50 along x-axis. As a result, the ball movement is unnatural and does not look like random (we have longer x distance and less y distance. It will be more natural if the x-axis velocity is a fixed big number and y-axis velocity is a random relatively small number). This issue affects all programs with the Ball.lua class. Namely, from pong-5 to pong-12. The issue will be more obvious with the interaction with paddles since the ball x-axis velocity can be captured as drastically being faster or slower after colliding with the paddle (again, the reason is that we have a long x distance).
functionBall:reset()
self.x=VIRTUAL_WIDTH/2-2self.y=VIRTUAL_HEIGHT/2-2self.dy=math.random(2) ==1and-100or100self.dx=math.random(-50, 50)
end
However, in pong-4/main.lua, it will look like the ball is having a natural and random movement.
functionlove.keypressed(key)
-- keys can be accessed by string nameifkey=='escape' then-- function LÖVE gives us to terminate applicationlove.event.quit()
-- if we press enter during the start state of the game, we'll go into play mode-- during play mode, the ball will move in a random directionelseifkey=='enter' orkey=='return' thenifgameState=='start' thengameState='play'elsegameState='start'-- start ball's position in the middle of the screenballX=VIRTUAL_WIDTH/2-2ballY=VIRTUAL_HEIGHT/2-2-- given ball's x and y velocity a random starting value-- the and/or pattern here is Lua's way of accomplishing a ternary operation-- in other programming languages like CballDX=math.random(2) ==1and100or-100ballDY=math.random(-50, 50) *1.5endendend
In summary, the dx and dy velocity of ball in all Ball.lua class should be swapped.
The text was updated successfully, but these errors were encountered:
Rye-Catcher
changed the title
Incorrect ball velocity in Ball.lua class
Better ball velocity in Ball.lua class
Aug 17, 2022
In
pong-5/Ball,lua
, the ball velocity along the x-axis and y-axis is not so good compared topong-4
. The ball will either has -100 or 100 velocity along y-axis but will have a random velocity from -50 to 50 along x-axis. As a result, the ball movement is unnatural and does not look like random (we have longer x distance and less y distance. It will be more natural if the x-axis velocity is a fixed big number and y-axis velocity is a random relatively small number). This issue affects all programs with theBall.lua
class. Namely, frompong-5
topong-12
. The issue will be more obvious with the interaction with paddles since the ball x-axis velocity can be captured as drastically being faster or slower after colliding with the paddle (again, the reason is that we have a long x distance).However, in
pong-4/main.lua
, it will look like the ball is having a natural and random movement.In summary, the dx and dy velocity of ball in all
Ball.lua
class should be swapped.The text was updated successfully, but these errors were encountered: