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
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
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