Search code examples
javanetwork-programmingjavafxcollision-detectiongame-physics

JavaFX Pong Vertical Game issues


Hello dear stackoverflow users, I'm just exhausted of trying everything so far, can you pretty please help me with 2 issues at the game I'm developing ?

Let's get started, I will try to explain as clear as I can.

Here's my code so far:

WIN_H = 600
WIN_W = 450
BAT_W = 100
BAT_H = 20
BallRadius = 10

//Check against the ball if is touching the Y-0 or Y-WIN_H axis
ballUp = true //Boolean to check against the Y-axis like on the Tutorial.


//Check against the BATs on Yaxis

if(ball.getTranslateY + BallRadius = 0)
  restartGame();

if(ball.getTranslateY() + BallRadius = WIN_H - BAT_HEIGHT
&& ball.getTranslateX() + BallRadius >= bat.getTranslateX()
&& ball.getTranslateX() - BallRadius <= bat.getTranslateX() - BAT_WIDTH

&& ball.getTranslateX() + BallRadius >= bat2.getTranslateX()
&& ball.getTranslateX() - BallRadius <= bat2.getTranslateX() - BAT_WIDTH)
ballUp = true;

//Check against the Y (WIN_H) axis - bottom & 0 means top.
if(ball.getTranslateY() + BallRadius = WIN_H)
ballUp = true;

I'm making a Vertical Ping-Pong game with 2 Bats and 1 Ball, pretty mainstream so far,inspired from this tutorial , my "changed" code is at 13:31 here , my problems are the followings :

  • I don't know how to make the Player 2's bat on top to have collision detection for the ball ( bottom is working very well).

  • I don't know how to implement Multiplayer network connection between two player on a LAN (I can learn tho'). I researched some libraries but so far NettyIO seems my path. At this field of problem I just want to ask you if:

    1. Is this possible and reliable? (check the image bellow, StreamBuffer between players and ball's movement)
    2. Which "path" you recommend me to choose for Net part ?

I'm sorry if I wasn't clear enough, I just want Guideness from somebody willing to help me with 1-2 Ideas, Guide me please.

enter image description here

Have a nice day!

P.S: Even if you believe that my question isn't well formatted please format it and please don't remove it, I just can be relaxed until I solve my mystery :(


Solution

  • Found the solution for the first issue !!!

    I've made a method with checkTop() and I put it after the //Check against top

    checkTop(){
    if(ball.getTranslateY() + BallRadius = 0 + BAT2_HEIGHT*2
    && ball.getTranslateX() + BallRadius >= bat2.getTranslateX()
    && ball.getTranslateX() - BallRadius <= bat2.getTranslateX() - BAT_WIDTH
    
    }
    
    //and I set my top Bat like this
    BAT2.setTranslateX((WIN_H-WIN_H)+WIN_H/2);
    BAT2.setTranslateY(0+BAT_HEIGHT*2);
    

    I also removed the Top-Bat declaration in the if statements of checking against the bats.

    Now the collision with the Top bat works very well!

    I still have an issue with the Networking part, how should I implement it? Wich library to choose for a "starter junior" ? It will work with half screens buffering or you , guys, got a better idea?

    Anticipate thanks!