Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problems with collisions in pong
#3
These things can become quite tricky, and I'm not sure if my solution works well in all cases.

If the ball and the paddle overlap, I decide wether the ball should bounce vertically or horizontally by looking at along which axis they overlap the least. If they overlap the least along the x-axis, it's a horizontal bounce, else a vertical.

ball.n7
Code:
...
    ball.collision_player = function()
        'colision con la x
        'if collision_rect(this.x+this.speedX,this.y,this.Width,this.Height,player.x,player.y,player.Width,player.Height)
        '    this.speedX = this.speedX * -1
        'endif
        'colision con la y
        'if collision_rect(this.x,this.y+this.speedY,this.Width,this.Height,player.x,player.y,player.Width,player.Height)
        '    this.speedY = this.speedY * -1
        'endif
       
        ' Marcus.
        ' Check if they overlap.
        if collision_rect(this.x,this.y,this.Width,this.Height,player.x,player.y,player.Width,player.Height)
            ' Calculate delta x and delta y, between the center of the ball and the center of the
            ' paddle. Divide the delta x value width the width of the paddle and delta y with the
            ' height of the paddle to normalize them (make comparison valid).
            dx = (this.x + this.Width/2 - (player.x + player.Width/2))/player.Width
            dy = (this.y + this.Height/2 - (player.y + player.Height/2))/player.Height
            ' If dx is higher, it means that there's less overlapping along the x-axis. In that
            ' case bounce left or right.
            if |dx| >= |dy|
                ' dx < 0, ball should bounce to the left, and we also move the ball to the left so
                ' that there's no longer any collision.
                if dx < 0
                    this.speedX = -|this.speedX|
                    this.x = player.x - this.Width               
                ' dx > 0, ball should bounce to the right, and move the ball to the right of the
                ' paddle.
                else
                    this.speedX = |this.speedX|
                    this.x = player.x + player.Width
                endif
            ' dy is higher, same principle as for dx :)
            else
                if dy < 0
                    this.speedY = -|this.speedY|
                    this.y = player.y - this.Height
                else
                    this.speedY = |this.speedY|
                    this.y = player.y + player.Height
                endif
            endif
        endif
    endfunc
    ...

Hope this helps! There are probably much better ways of doing it.

Edit: By the way, the code looks nice! I see now that you're already using "getters" and "setters" to share variables between files. In my opinion that makes the code way more readable than it would be if you could make variables visible across files - but that's just my opinion Smile
Reply


Messages In This Thread
RE: problems with collisions in pong - by Marcus - 01-24-2024, 07:10 PM
RE: problems with collisions in pong - by Marcus - 01-25-2024, 04:30 PM
RE: problems with collisions in pong - by Marcus - 01-25-2024, 08:34 PM
RE: problems with collisions in pong - by Marcus - 01-25-2024, 09:32 PM

Forum Jump:


Users browsing this thread: 2 Guest(s)