Author Topic: Pong collisions  (Read 991 times)


  • New Member
  • *
  • Posts: 1
  • Karma: +0/-0
    • View Profile
Pong collisions
« on: Mon, Apr 16, 2012 »

I'm making a simple PONG clone to brush up on the new version of Flixel and get back into programming, and I don't seem to get how to get a ball to bounce properly. Before getting the ball to bounce in interesting ways, I want to get it to simply reverse its X and Y upon contact.

Code: [Select]
override public function update():void {

FlxG.collide(ball, wallGroup, wallBounce);
FlxG.collide(ball, paddleL, paddleBounce);
FlxG.collide(ball, paddleR, paddleBounce);

public function wallBounce(b:Ball,w:Wall):void {
ball.velocity.y = ball.velocity.y * -1;

public function paddleBounce(b:Ball, p:Paddle):void {
ball.velocity.x = ball.velocity.x * -1;

wall and paddle are both set to "immovable = true" btw. Setting ball to that makes it ignore collision, and making the walls and paddles immovable=false means they bounce around and drift on impact. Overlap doesn't do what I want either, as the ball ends up "discovering" it's overlapping too late then freaks out. I don't quite know how to get the behavior I want, where upon touching the object simply changes its direction, or why it collides the way it does. Thoughts?


  • happens to be a
  • Active Member
  • ***
  • Posts: 112
  • Karma: +0/-0
  • Howdy
    • View Profile
    • twitter
Re: Pong collisions
« Reply #1 on: Wed, Apr 18, 2012 »
You could do the overlap thing, but give your balls some state  - if it hits player  1's paddle, it goes into a state that only lets it touch player 2's paddle, and vice versa. This way you're guaranteed to just flip directions until it hits the other side, and not do anything funky. (Likewise, you could do this with the wall.)

I *think* that will work. I've thought about this kind of thing a little and didn't think to do this until now (so thanks for asking the question!).

Another way, in addition to flipping the velocities, move the ball just outside of the overlap zone. So if a ball is going to paddle 1 at x = 0, the ball "overlaps" with its x = -1, then flip the ball's x velocity and set x = 1.

I still think there must be better ways, though.
making anodyne, a zelda-like (with flixel!)