• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML pong collisoin

woods

Member
been away for a while and figured id get blow the dust off with a lil game of pong.


the paddle can move up/down, left/right
simple collision works fine, however doubletapping the ball is a bit wonky still.
the issue is when the ball is moving away from the paddle and it gets bumped, it reverses direction (unwanted)

i understand i am telling the ball to change direction whenever it hits the paddle... how do i make it so the ball continues forward when hitting it from behind?




obj_ball create event
Code:
/// start moving
direction = random(360);
speed = random(2);

obj_ball step event
Code:
//collision with paddle_1
//horizontal
if place_meeting(x+hspeed,y,obj_paddle_1)
{
    hspeed = -hspeed;
}

//vertical
if place_meeting(x,y+vspeed,obj_paddle_1)
{
   vspeed = -vspeed;
}


obj_paddle_1 step event
Code:
/// move with WASD

if keyboard_check(key_right)
    {
    dir = 0;
    x += 8;
    }
else
if keyboard_check(key_left)
    {
    dir = 2;
    x -= 8;
    }
else  
if keyboard_check(key_up)
    {
    dir = 1;
    y -= 8;
    }
else
if keyboard_check(key_down)
    {
    dir = 3;
    y += 8;
    }    
    
    

// keep from going off screen

x = clamp(x, 64, 256);
y = clamp(y, 96, room_height - 96);
 

Slyddar

Member
There are better ways, but using the code you have, once the ball is hit, maybe add a small delay before it can change direction again. 20 steps should be enough, depending on the balls speed. Declare hit_timer = 0 in the create event.
GML:
//collision with paddle_1
if hit_timer-- <= 0 {
  //horizontal
  if place_meeting(x+hspeed,y,obj_paddle_1)
  {
      hspeed = -hspeed;
      hit_timer = 20;
  }

  //vertical
  if place_meeting(x,y+vspeed,obj_paddle_1)
  {
     vspeed = -vspeed;
     hit_timer = 20;
  }
}
 

TheouAegis

Member
Is this normal pong with paddles on the side? If so, collisions can be simplified.

Code:
if hspeed > 0 {
if abs(x+hspeed-paddle2.x) <= paddle2.sprite_width/2
if abs(y+vspeed - paddle2.y) <= paddle2.sprite_height/2
    direction = (180 - direction mod) 360;
}
else
if hspeed < 0 {
if abs(x+hspeed-paddle1.x) <= paddle1.sprite_width/2
if abs(y+vspeed - paddle1.y) <= paddle1.sprite_height/2
    direction = (180 - direction mod) 360;
}
else
direction -= sign(x - room_width/2) * sign(vspeed);
The last case should never arise, but it's good to have a failsafe.

If the paddles are at the top and bottom, remove "180", swap hspeed and vspeed, swap x and y, and swap width and height. This code requires player 1 on the left and player 2 on the right and the paddle sprite origins to be centered.
 

woods

Member
yeah paddles are on left and right.. paddle sprites are centered
with wall objects on the top and bottom and score zones behind the paddles

both methods work well enough to fix the issue of the ball going BEHIND the paddle.
but i do see that i am loosing the ability to doubletap the ball all together.. (looking to add a speed boost to the ball after basic functionality is down)



thanks fellas for the assist
 

TheouAegis

Member
Define double-tapping the ball. Didn't know such a thing existed in pong. I remember doing that in shufflepuck cafe. And I mean in air hockey, too.

Are you talking about bouncing off the walls? If the walls are top and bottom,it's just direction=-direction mod 360;

If you have rules like air hockey where there's a wall behind the panel and the goal doesn't take up the whole left or right side of the screen, then you would have to check both panels it's better to just one. And if the ball can bounce off the top of the panel, rather than just the sides of the paddle, then you would have to check what side the ball is actually bouncing off of. If it's bouncing off the top or bottom side, simply negate the direction. If it's bouncing off the left or right side, add 180 to the reversal.
 
Last edited:

woods

Member
the standard pong game that i remmber (showing my age here. atari 2600) was super simple. the paddle movement is two directions only. with

what i am doing i guess is more of an airhockey situation. i have a range that my paddle can move around inside a boxed area.. sometimes the ball is at such a steep angle that it will take a long time to travel the playfield(bouncing off the top and bottom walls several times) i want to be able to smack the ball again to get it to cross the field more directly.

when the ball is going more vertical and barely horizontal, i want to [hit it again] while still inside my player's box so it goes more sideways.. this is what i am referencing when i say "doubletap"

===

i believe one issue may simply be the ball speed vs the paddle speed.. moving the paddle too fast when hitting the ball from behind, causing the paddle to overlap the ball and send it in reverse.(my original collision code for the ball with the paddle simply says switch directions on contact)
 
Top