Jump to content
• Advertisement

#### Archived

This topic is now archived and is closed to further replies.

# Collision: Which side of a square was hit

This topic is 6925 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

I am making a game, and i am working on the collision of a ball and a square brick. First i test to see if the x & y coords of the ball are within the brick, if they are, then they are colliding. This works fine. Then i need to tell which side of the brick was hit so i know where to bounce the ball of too. I test to see if the left side was hit, and since the ball moves about 5 pixels each frame, it can be up to 5 pixels inside the brick. The problem is if it hits within the strip that is 5 pixels wide on the left, this xcoord might also be on the top or bottom of the brick. Its like this: _______ / / Heres the brick. / / / / ------- _______ /*/ / Here a strip on the left. /*/ / /*/ / ------- If the ball''s x coord is within this strip, it doesn''t always mean that it hit the brick on the left side; it could''ve hit the top or bottom left corner. And in that case i would have to bounce it up or down (respectively)instead of off to the left. I thought about testing the ycoord too but if the ball went through the corner of the brick at certain angles, the ycoord would be down low or high enough that it could''ve come from the bottom or top. Thanks to all who answered my last question about file input/output! t2sherm ô¿ô

#### Share this post

##### Share on other sites
Advertisement
how about saving the coordinates of the the last place that the ball has been, then when u see that the ball and the brick colliding, u can have the current position and the last stored positions as the two points on a line. so just check the closest intersection of this ball with the bounding rectangle of the brick. that way u can figure out which side was hit (which is simple the side that the line crosses first)

- pouya

#### Share this post

##### Share on other sites
Also you could use the movement of the ball:

if (dxBall > 0 && dyBall > 0){  // Ball (X) is going right and down:  Dosomething()}//////////////////////////////////////// Shown visual, where X is the ball X       2   --------- 1 /       /  3   /       /   ---------       4[/CODE]Now the ball can only hit 1 or 2, because it´s going down (2) and right (1).This way you can cover all cases and just calc the interceptions. Should be rather easy.

#### Share this post

##### Share on other sites
Had the same problem a while back. I used windows function intersectrect()

RECT dest;
RECT block;
RECT ball;

IntersectRect(&dest,&block,&ball)

look up the function in your help files to get more info.

/Macke

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Now

• 10
• 13
• 52
• 11
• 15
• Advertisement
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!