How do I create PONG with Drag'n Drop behaviour?

  • Hello everyone,

    I'm trying to make a small Pong game in which the player needs to drag his bar. It looks good so far, but there's one major problem:

    The angle of the ball is being determined via the 'Bounce of Solid' option. But that way the speed and angle of movement of the player's or opponent's bar won't affect the trajectory of the ball on collision, which is quite bad.

    Sadly, there is no way to compare the speed and angle of movement of an object that is currently being dragged to another quantity, so I don't have the option to change the ball's trajectory according to how it collides with the bars.

    Any ideas? How come that this basic function is missing?

  • You can calculate the speed and angle by checking the change in position while dragging.

    Here's an example: ... .capx?dl=1

    Hope this helps. Cheers!

  • Works great. Thanks!

    Fun fact: The hard thing about creating a Pong AI is making it slightly more dumb than the player.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • CreativeMind

    Could you share you code please? I'm making a simple Arkanoid game and would also like to adjust ball angle depending on paddle speed. I'm using Bullet behavior with "Bounce off solids=yes".

  • Sure. There might be some confusing parts, but I tried to make it as clear as possible:

    This is the code for the player's paddle, which is on the left side of the screen, drag is enabled in vertical direction.

    The ball's initial angle is zero (in a horizontal direction).

    I decided to use 'Bounce of solid' for the border of the layout and to use 'On collision' for the paddles.

    The first two events are used to calculate the drag velocity, as described by brunopalermo.

    On collision, I use trigonometry to split the ball's total speed into Ball_Velocity_X (the speed in x direction) and Ball_Velocity_Y (the speed in y direction). Then I add Drag_Velocity of the paddle to Ball_Velocity_Y of the ball and use trigonometry again to calculate the new angle of motion of the ball. That way, the velocity of the paddle affects the ball's trajectory.

    Here comes the confusing thing: C2 defines a bullet's angle of motion in the range from 0 to 180 (lower half circle, clockwise speaking) and -180 to 0 (upper half circle, clockwise speaking). That means you have to use different trigonometric functions depending on whether the angle is positive or negative. If you want to program the opponents paddle, theses dependencies change!

    For the opponents AI you can vary the time span in which the paddle moves towards the ball's Y coordinate.

    Little bonus detail 1:

    I figured out that it is very annoying when the ball is dodging almost vertically, so I decided to limit the angle to 65 (or rather -65) degrees.

    Little bonus detail 2:

    I think it is more interesting if the ball gains some speed at every collision, that way longer rallies get more intense.

    Hope that helps!

  • CreativeMind

    Thanks! I'm using Solid paddle with curved collision polygon, so will need to adapt your code.

    Here is one more tip - when the ball bounces between two walls at close to right angle (85-95 degrees), it can bounce for ages, which is quite annoying. So I increase the angle a little bit with every collision. Say, if the angle was 86, I make it 83.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)