Home Game Development Why examine the ball and brick collision twice?

Why examine the ball and brick collision twice?

0
Why examine the ball and brick collision twice?

[ad_1]

I watched https://www.youtube.com/watch?v=F86edI_EF3s.

I consider the next code is the whole code

https://github.com/games50/breakout/tree/grasp/breakout13

And

https://github.com/games50/breakout/blob/grasp/breakout13/src/states/PlayState.lua
is the code in play.

related code:

-- detect collision throughout all bricks with the ball
for okay, brick in pairs(self.bricks) do

    -- solely examine collision if we're in play
    if brick.inPlay and self.ball:collides(brick) then

        -- add to attain
        self.rating = self.rating + (brick.tier * 200 + brick.shade * 25)

        -- set off the brick's hit perform, which removes it from play
        brick:hit()

        -- if we have now sufficient factors, get better a degree of well being
        if self.rating > self.recoverPoints then
            -- cannot go above 3 well being
            self.well being = math.min(3, self.well being + 1)

            -- multiply get better factors by 2
            self.recoverPoints = math.min(100000, self.recoverPoints * 2)

            -- play get better sound impact
            gSounds['recover']:play()
        finish

        -- go to our victory display if there are not any extra bricks left
        if self:checkVictory() then
            gSounds['victory']:play()

            gStateMachine:change('victory', {
                degree = self.degree,
                paddle = self.paddle,
                well being = self.well being,
                rating = self.rating,
                highScores = self.highScores,
                ball = self.ball,
                recoverPoints = self.recoverPoints
            })
        finish

        --
        -- collision code for bricks
        --
        -- we examine to see if the alternative aspect of our velocity is exterior of the brick;
        -- whether it is, we set off a collision on that aspect. else we're inside the X + width of
        -- the brick and will examine to see if the highest or backside edge is exterior of the brick,
        -- colliding on the highest or backside accordingly 
        --

        -- left edge; solely examine if we're transferring proper, and offset the examine by a few pixels
        -- in order that flush nook hits register as Y flips, not X flips
        if self.ball.x + 2 < brick.x and self.ball.dx > 0 then
            
            -- flip x velocity and reset place exterior of brick
            self.ball.dx = -self.ball.dx
            self.ball.x = brick.x - 8
        
        -- proper edge; solely examine if we're transferring left, , and offset the examine by a few pixels
        -- in order that flush nook hits register as Y flips, not X flips
        elseif self.ball.x + 6 > brick.x + brick.width and self.ball.dx < 0 then
            
            -- flip x velocity and reset place exterior of brick
            self.ball.dx = -self.ball.dx
            self.ball.x = brick.x + 32
        
        -- high edge if no X collisions, all the time examine
        elseif self.ball.y < brick.y then
            
            -- flip y velocity and reset place exterior of brick
            self.ball.dy = -self.ball.dy
            self.ball.y = brick.y - 8
        
        -- backside edge if no X collisions or high collision, final risk
        else
            
            -- flip y velocity and reset place exterior of brick
            self.ball.dy = -self.ball.dy
            self.ball.y = brick.y + 16
        finish

        -- barely scale the y velocity to hurry up the sport, capping at +- 150
        if math.abs(self.ball.dy) < 150 then
            self.ball.dy = self.ball.dy * 1.02
        finish

        -- solely permit colliding with one brick, for corners
        break
    finish
finish

Query

Why does the above code examine if self.ball.x + 2 < brick.x and self.ball.dx > 0 then?

I’m particularly confused as a result of the code already did the collision examine with if brick.inPlay and self.ball:collides(brick) then, however now the code provides a +2.

The identical goes for the opposite comparable sections
self.ball.x + 6 > brick.x + brick.width and self.ball.dx < 0
I do not know why +6 can also be checked.

I additionally don’t perceive why ball velocity (dx) can also be used for checking.

In accordance with the feedback within the supply code, it appears to be a course of on the edge, however I do not actually perceive it.
Is that this a typical course of (or patterns) in recreation applications (particularly in 2D collision)?


I assumed it was to unravel the next downside(tunneling), however I felt it was one thing completely different and couldn’t perceive the code myself.

In video games physics engines, what’s “tunneling”, also called the “bullet by means of paper downside”?


In scripting this query, I feel I get somewhat extra organized.

the ball must bounce at a leftward velocity whether it is on the left edge, and the ball must bounce at a rightward velocity whether it is on the precise edge, so I feel (unsure precisely) that is what’s being dealt with right here.

I assume the +2 and +6 are arbitrary by the writer.
I simply do not know why these values ought to be added as a substitute of the present place.

Through the use of the algorithm above code, is it attainable to find out which aspect (high, backside, left, or proper) the collision occurred with?

I’m having a tough time imagining it clearly.

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here