1

I'm pretty new to Game maker, and i'm having some trouble with a function.

I'm making a pong-like game, and i'm running into an issue when the ball colides with the top of the paddle or the bottom (the smaller sides, not face on) the ball and paddle freeze.

The only code I have in the collision event of the ball with the paddle is to reverse the horizontal speed.

hspeed *= -1;

i understand the problem, that because the ball has made contact with the top, the horizontal direction reverses, but its still in contact with the surface as the veritcal speed hasn't changed so reverses again, and gets stuck in an infinite loop.

I've tried many things to work around it, such as setting an alarm to detect if the ball is still touching after one frame and to reverse the vertical speed ( no success)

I've also tried to make a check to see if the y position of the top of the ball is the same as the y position of the bottom of the paddle (and vice versa) and to reverse the vertical speed if true. but again no success.

i'm sure both these methods work if the correct code is used, but i must just be executing it wrong.

What method should i use to make it so that if the ball collides with the top or bottom of the paddle, it doesn't get stuck, and ideally just reverses the vertical speed instead of the horizontal speed?

i'm sure this has a simple solution but cause i'm new i cant work it out!

thanks!

Kane B
  • 21
  • 2

2 Answers2

0

My solution would be to change instance when the ball collides with the paddle. Create an object that is nearly identical to your current object, and then remove the collision event but set the create event to move at the reverse speed. The ball will move in your desired direction, but won't get stuck. You could then check if there is an empty space, and if there is, change the object back into the original ball.

0

First off, this is a good question. I don't know how you're checking for collisions, but if you're doing it in a step event (as opposed to a collision event or whatever), but you should be able to check for horizontal and vertical collisions separately. For example (replacing obj with the paddle object's name):

if (position_meeting(x-1, y, obj) || position_meeting(x+1, y, obj)) {
    //horizonal collision
}
if (position_meeting(x, y-1, obj) || position_meeting(x, y+1, obj)) {
    //vertical collision
}

This gives the advantage of detecting the edge case where it hits a corner exactly, in which case you would probably want to flip both speeds.

You can also do something more like you suggested, even using a collision event, simply checking if the ball's x-value (assuming you have centered both sprites) is less than {the paddle's x-value plus or minus its width}, which would indicate a vertical collision.

Timtech
  • 1,224
  • 2
  • 19
  • 30