#### Archived

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

# Easy, but cannot fix brick bounce problem...

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

## Recommended Posts

Okay, so I have been hacking at this all night, i must be tired because this shouldn''t be as hard as it is... so here is what im trying to do: I have a simple ball flyin around a screen, and if it hits a brick with four sides, then i want to reverse the velocities in the proper manner, i.e. if it hits the left side reverse to the left, etc... here is what i have as per code and its not really working all that hot: for(int i=0; i)==TRUE) //we collided with a bricks rectangle { if((Ballxv >= 0) && (Ballyv >= 0)) //ball traveling up right { if( ( ((theBall.y+theBall.height) > picBrick[i].y) && (theBall.y < (picBrick[i].y+picBrick[i].height))) && ( (theBall.y>picBrick[i].y)&& ( (theBall.y+theBall.height) < (picBrick[i].y+picBrick[i].height)))) { //did we hit within bounds of a brick? if( ( (theBall.x+theBall.width) > picBrick[i].x) && (theBall.x <= picBrick[i].x) ) //left side of brick? { Ballxv=-Ballxv; //reverse } else { //we must of hit the bottom of the brick while travling up right Ballyv=-Ballyv; } } else if((Ballxv < 0) && (Ballyv < 0)) //travling down left { if( ( ((theBall.y+theBall.height) > picBrick[i].y) && (theBall.y < (picBrick[i].y+picBrick[i].height))) && ( (theBall.y>picBrick[i].y)&& ( (theBall.y+theBall.height) < (picBrick[i].y+picBrick[i].height)))) { ///are we within the right side of the brick if( (theBall.x < (picBrick[i].x + picBrick[i].width) ) && ( (theBall.x +theBall.width) <= (picBrick[i].x+picBrick[i].width)) ) Ballxv=-Ballxv; //for sure the right side? if so reverse } else { //must of not been right side of brick, must of been top of brick reverse y Ballyv=-Ballyv; } } else if((Ballxv > 0) && (Ballyv < 0)) //traveling down right { // traveling if( ( ((theBall.y+theBall.height) > picBrick[i].y) && (theBall.y < (picBrick[i].y+picBrick[i].height))) && ( (theBall.y>picBrick[i].y)&& ( (theBall.y+theBall.height) < (picBrick[i].y+picBrick[i].height)))) { //did we hit the left side of the brick if( ( (theBall.x+theBall.width) > picBrick[i].x) && (theBall.x <= picBrick[i].x) ) Ballxv=-Ballxv; //are we sure ? if so reverse the x } else { //must of missed the left side of brick and hit the top of brick, reverse y Ballyv=-Ballyv; } } else if((Ballxv < 0) && (Ballyv > 0) ) //traveling up left { if( ( ((theBall.y+theBall.height) > picBrick[i].y) && (theBall.y < (picBrick[i].y+picBrick[i].height))) && ( (theBall.y>picBrick[i].y)&& ( (theBall.y+theBall.height) < (picBrick[i].y+picBrick[i].height)))) { //did we hit right side of brick? if( (theBall.x < (picBrick[i].x + picBrick[i].width) ) && ( (theBall.x +theBall.width) <= (picBrick[i].x+picBrick[i].width)) ) Ballxv=-Ballxv;//are we sure we hit right side? else { Ballyv=-Ballyv; //we must of hit the bottom of the brick , reverse y } } } } (any syntax errors are cut and paste errors.) My collision bobs routine is as follows: int Collision_BOBS(BOB_PTR bob1, BOB_PTR bob2) { // are these a valid bobs if (!bob1 || !bob2) return(0); // get the radi of each rect int width1 = (bob1->width>>1) - (bob1->width>>3); int height1 = (bob1->height>>1) - (bob1->height>>3); int width2 = (bob2->width>>1) - (bob2->width>>3); int height2 = (bob2->height>>1) - (bob2->height>>3); // compute center of each rect int cx1 = bob1->x + width1; int cy1 = bob1->y + height1; int cx2 = bob2->x + width2; int cy2 = bob2->y + height2; // compute deltas int dx = abs(cx2 - cx1); int dy = abs(cy2 - cy1); // test if rects overlap if (dx < (width1+width2) && dy < (height1+height2)) return(1); else // else no collision return(0); } // end Collision_BOBS Any help here would be great!. I know its probably simple and i am not seeing the problem. Im close but its still not working. thanks for any info. -Shane p.s. currently the ball doesnt work for hitting the top or the bottom of the bricks, i can get the left or the right just fine, I think my if''s are not in right order or i need more of them.

##### Share on other sites
If you tell us in what way it''s "not really working all that hot," we''ll know what to look for and be able to help you better.

##### Share on other sites
Hey,

I would use a reflection vector for this insteed of creating ifs for every possible situations.

But it depends on how long you have been working, and if you know what a Normal are and Dot product.

Its just simple math and theres alot of info about normals.
Anyhow, heres what you should do,

When hitting a line box / side you calculate the new ball direction with:

NewDirection = CurrentDirection - Normal * 2 * Normal.Dot(CurrentDirection)

So if you are heading straight down with 0, -12 and hitting a plane with a normal of 0, 1.. the new direction becomes 0, 12

So it doesnt mather where how why you hit stuff the ball will bounce like it should in real life,

Even if you dont add gravity or "friqtion? "

Good luck.

##### Share on other sites
I like the normal idea, I know what normals are for the most part, and dot products too. So let me just get this formula straight:

NewDirection = CurrentDirection - Normal * 2 * Normal.Dot(CurrentDirection)

currentDirection, is that a vector? I have my velocities in an x and y direction...

Normal: Normal of which? the brick right? how do I figure out a normal for a side again? I have to add another field to my struct to hold that info? or can I figure out the sides normal "on the fly" or brute force?

*2* : I suspect just a times 2

Normal.Dot(current direction) Just the normal dotted with the current direction. K sounds good.

-Shane
p.s. when I say more info I mean, how I get the normal of a the brick side again? Its a square so i suspect it will be easy...

##### Share on other sites
Its a bit simple for a decent game but you can just reverse the signs.

velocity X = +2 hit a vertical wall becomes -2
velocity Y = +2 hit a horizontal wall becomes -2

,Jay

1. 1
2. 2
Rutin
21
3. 3
JoeJ
17
4. 4
5. 5

• 37
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631705
• Total Posts
3001824
×