### Author Topic: How to check which side is colliding?  (Read 1218 times)

#### Leebo

• Active Member
• Posts: 174
• Karma: +0/-0
##### How to check which side is colliding?
« on: Thu, Nov 8, 2012 »
I'm trying to make a simple breakout game as practice and I can't figure out how to handle the collisions for the ball when it's touching different sides of the blocks.

I have this to see if the ball is touching any blocks

Code: [Select]
`FlxG.overlap( ball, blocks, blockHit );`
and then this is the callback

Code: [Select]
` public function blockHit(s1:FlxSprite, s2:FlxSprite):void { if ( ( s2.isTouching( FlxObject.UP ) ) || ( s2.isTouching( FlxObject.DOWN ) ) ) { s1.velocity.y *= -1; } if ( ( s2.isTouching( FlxObject.LEFT ) ) || ( s2.isTouching( FlxObject.RIGHT ) ) ) { s1.velocity.x *= -1; } s2.visible = false; s2.solid = false; }`
The lower part that makes the blocks disappear works fine, but the part where I'm trying to check which side of the block the ball is touching doesn't work. The ball barrels through unaffected.

#### novellof

• Member
• Posts: 19
• Karma: +0/-0
##### Re: How to check which side is colliding?
« Reply #1 on: Thu, Nov 8, 2012 »
the real way to do collision is like this..

ill do an example like let say a pong game ill give you an example of a block that is 10px x 10px and a paddle that is 75px x 15px

block.y  is blocky position  same goes for block .x  and paddle.x , paddle.y

this is what your if statement should look like....it takes some figuring out

this will be for the top of the paddle

|                                                  | 15
__
|__|   // ball  10x10

now specify the bounds of the object

{
this will be a top collision
}

same goes for side collision

{
left side collision
}

{
right side collision
}

something to this affect my plus and minus are most likely messed up same goes with greater/less than signs, but it takes some fooling around with... its easier to see it written on paper...

hope this helps

#### Gama11

• Contributor
• Posts: 390
• Karma: +0/-0
##### Re: How to check which side is colliding?
« Reply #2 on: Fri, Nov 9, 2012 »
I'm making a breakout game too and I had similar issues. There is a really simple solution to it, all you have to do is set the blocks immoveable var to true and set the elasticity for the ball to 1. Flixel will then handle all the physics stuff for you, all you have to do is create some borders so the ball doesn't leave the screen.

#### Leebo

• Active Member
• Posts: 174
• Karma: +0/-0
##### Re: How to check which side is colliding?
« Reply #3 on: Fri, Nov 9, 2012 »
Awesome, that works really well. Not really an "optimal" solution for learning purposes, but at least it functions.

#### Leebo

• Active Member
• Posts: 174
• Karma: +0/-0
##### Re: How to check which side is colliding?
« Reply #4 on: Fri, Nov 9, 2012 »
Oh, and thanks novellof, I guess your reply was awaiting moderation. I was hoping flixel would handle that kind of stuff with the "touching" variables and functions, but if I really need to use that process, I can.