Ball Physics
-
Hi, I'm creating a brick game and am having trouble merging these two if blocks. They work independantly fine but not together. Any ideas on how to get them to work. They reverse the velocities of the ball when hitting a brick. I'm also having a problem where the ball sometimes goes through a brick, it flickers and slowly makes its way through,kinda glitchy.
if(Ball\_List\[b\]->gameBall->Bounds.IntersectsWith(Brick1->Bounds)) { if((Ball\_List\[b\]->gameBall->Right >= Brick1->Left || Ball\_List\[b\]->gameBall->Left <= Brick1->Right)) { Ball\_List\[b\]->xVel = -Ball\_List\[b\]->xVel; } if((Ball\_List\[b\]->gameBall->Bottom >= Brick1->Top || Ball\_List\[b\]->gameBall->Top <= Brick1->Bottom)) { Ball\_List\[b\]->yVel = -Ball\_List\[b\]->yVel; } }
-
Hi, I'm creating a brick game and am having trouble merging these two if blocks. They work independantly fine but not together. Any ideas on how to get them to work. They reverse the velocities of the ball when hitting a brick. I'm also having a problem where the ball sometimes goes through a brick, it flickers and slowly makes its way through,kinda glitchy.
if(Ball\_List\[b\]->gameBall->Bounds.IntersectsWith(Brick1->Bounds)) { if((Ball\_List\[b\]->gameBall->Right >= Brick1->Left || Ball\_List\[b\]->gameBall->Left <= Brick1->Right)) { Ball\_List\[b\]->xVel = -Ball\_List\[b\]->xVel; } if((Ball\_List\[b\]->gameBall->Bottom >= Brick1->Top || Ball\_List\[b\]->gameBall->Top <= Brick1->Bottom)) { Ball\_List\[b\]->yVel = -Ball\_List\[b\]->yVel; } }
You are using logical OR (
||
) instead of AND (&&
). In the first case if BALL.RIGHT is greater than BRICK.LEFT OR BALL.LEFT is less than BRICK.RIGHT you slow it down. However you need both conditions to be true; for example if BALL.RIGHT is greater than BRICK.LEFT then you change velocity, but the ball is not necessarily less than BRICK.RIGHT at this point. I think the same holds true for the vertical movement.The best things in life are not things.
-
You are using logical OR (
||
) instead of AND (&&
). In the first case if BALL.RIGHT is greater than BRICK.LEFT OR BALL.LEFT is less than BRICK.RIGHT you slow it down. However you need both conditions to be true; for example if BALL.RIGHT is greater than BRICK.LEFT then you change velocity, but the ball is not necessarily less than BRICK.RIGHT at this point. I think the same holds true for the vertical movement.The best things in life are not things.
I tried using && instead of || but it messed up the ball physics. The ball would return in the same direction back towards the paddle. The problem seems to be that Ball.right is is always going to be greater then Ball.left when the ball is colliding with the top or bottom of the brick so the check for horizontal collision will also get executed. I'm not sure how to work around this. Thanks.
-
Hi, I'm creating a brick game and am having trouble merging these two if blocks. They work independantly fine but not together. Any ideas on how to get them to work. They reverse the velocities of the ball when hitting a brick. I'm also having a problem where the ball sometimes goes through a brick, it flickers and slowly makes its way through,kinda glitchy.
if(Ball\_List\[b\]->gameBall->Bounds.IntersectsWith(Brick1->Bounds)) { if((Ball\_List\[b\]->gameBall->Right >= Brick1->Left || Ball\_List\[b\]->gameBall->Left <= Brick1->Right)) { Ball\_List\[b\]->xVel = -Ball\_List\[b\]->xVel; } if((Ball\_List\[b\]->gameBall->Bottom >= Brick1->Top || Ball\_List\[b\]->gameBall->Top <= Brick1->Bottom)) { Ball\_List\[b\]->yVel = -Ball\_List\[b\]->yVel; } }
Cyclone_S wrote:
Ball_List[b]->gameBall->Right >= Brick1->Left
Physically, the ball have already passed the bound of the brick, i.e. reversing the speed is not enough: you should update the position accordingly. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Cyclone_S wrote:
Ball_List[b]->gameBall->Right >= Brick1->Left
Physically, the ball have already passed the bound of the brick, i.e. reversing the speed is not enough: you should update the position accordingly. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]This is as close as I got so far but it makes the ball pause for a moment when it hits the top and bottom of the brick. This code is in the timer event. Any solution to this? Thanks.
// Check for brick collisions.
if(left == false && Ball_List[b]->gameBall->Bounds.IntersectsWith(Brick1->Bounds))
{if((Ball\_List\[b\]->gameBall->Right >= Brick1->Left || Ball\_List\[b\]->gameBall->Left <= Brick1->Right)) { Ball\_List\[b\]->xVel = -Ball\_List\[b\]->xVel; left = true; top = true; } } else {left=false;} if(top == false && Ball\_List\[b\]->gameBall->Bounds.IntersectsWith(Brick1->Bounds)) { if((Ball\_List\[b\]->gameBall->Bottom >= Brick1->Top || Ball\_List\[b\]->gameBall->Top <= Brick1->Bottom)) { Ball\_List\[b\]->yVel = -Ball\_List\[b\]->yVel; top = true; } } else{top=false;}
-
Hi, I'm creating a brick game and am having trouble merging these two if blocks. They work independantly fine but not together. Any ideas on how to get them to work. They reverse the velocities of the ball when hitting a brick. I'm also having a problem where the ball sometimes goes through a brick, it flickers and slowly makes its way through,kinda glitchy.
if(Ball\_List\[b\]->gameBall->Bounds.IntersectsWith(Brick1->Bounds)) { if((Ball\_List\[b\]->gameBall->Right >= Brick1->Left || Ball\_List\[b\]->gameBall->Left <= Brick1->Right)) { Ball\_List\[b\]->xVel = -Ball\_List\[b\]->xVel; } if((Ball\_List\[b\]->gameBall->Bottom >= Brick1->Top || Ball\_List\[b\]->gameBall->Top <= Brick1->Bottom)) { Ball\_List\[b\]->yVel = -Ball\_List\[b\]->yVel; } }
Recently I've found a very neat blog that explains how to implement some basic physics of a very similar kind as what you are trying to achieve. Check out Physics engine for dummies for some very useful explanations and code that can get you started. The explanations you can find there on how best to implement collision control are very exhaustive. Although the code examples do not feature rectangular bricks, I am sure you can adapt them quite easily by stealing the code from the collision control with the outside border. You just need to make sure that you will always have to check all borders, rather than just 1 at a time. However, for a start it may be easier to just use circular bricks and a circular ball.