I'm making my first GUI based game in Java using Slick2D, and I have hit a sort of wall, no pun intended . This might be a really simple question but honestly my mathematics are not amazing by no means and I'm more use to general stuff in Java than gaming math.
I have my ball moving on the screen and when it hit's a paddle, I can get it to change direction and to move up or down but I want to to change direction depending on where it hits. If it hits the lower half then it will go down, if it hits the upper half then it should go up. Any ideas on how I can do this? I have posted a segment of my code below showing what is currently happening, sorry if it's not the correct way to do these sort of mechanics D:
if(ballIcon.intersects(playerIcon)){ vecX = 2 * delta * 0.2f; }else if(ballIcon.intersects(cpuIcon)){ vecX = -2 * delta * 0.2f; } if(ballIcon.intersects(side)){ vecY = 1; }else if(ballIcon.intersects(sideBottom)){ vecY = -1; }
This code basically changes the direction of the ball. I'm using Slick2D shapes to detect collisions and these are invisible, but their x and y cords are equal to the images on the screen so they follow the images around.