2D Sports Game Code Logic Help



  • Hi all,

    I have been putting more and more effort into my side project game for a while now and looking to show something soon.

    However I am stuck on what is the best way to simulate height/depth in a 2D game.

    So for instance I am trying to take the idea of pong and turn it into a tennis game.

    The main difference between pong and tennis is getting the ball over the net, which is how I am struggling to code around using a 2d javascript game engine (at the moment impactjs, but I am also looking into phaser).

    Does anyone have any recommendations around the vector math and collision detection from a code perspective?

    I am not looking for answers, more so just a spring board to get me thinking.

    Thanks and appreciate all the help!


  • Tiger Hat

    It sounds like you’re best off just working in 3D space, and representing it in 2D.

    This could be as simple as adding a float to your ball object, and calculating z bounce as a separate function, if the value is above a certain number when crossing the net it passes, otherwise it collides - you could simulate depth by scaling and changing palette on the ball.

    You may be better off using a pure 3D solution, and just using a single plane to represent it visually (so take x,y, or x,z)


  • LDG

    I was going to say the same thing as @salmonmoose, he is smart :)

    The good news is it could be as easy as tacking a “z” property onto your game object, and having it move from, say, 0 up to 1 as it travels from X1, Y1 to X2, Y2. If the net is N tall, you have only to do the check mentioned above. Sounds like it should be pretty easy to accomplish what you want.



  • Thanks guys for the feedback, super appreciated.

    Since this is my first game making adventure (whilst I have joined the forums a while ago, I shelved the project, and only recently started allocating time to it regularly), I don’t feel comfortable with jumping straight into 3D.

    However the idea of inventing a pseudo Z property is what I have wanted to experiment with, and so yeah I will definitely play around with that and add it to the other x,y attributes.


Log in to reply