Collision (Working Nearly)
I am testing for collsion with a Ball against blocks. At the moment i am testing the balls x,y co-ordinates are with in the blocks co-ordinates. if they are then reflect the ball. But i want to be able to test for different sides of the blocks so that the ball reflects at different angles depending on the side of the block.
But at the moment this doesnt happen even though i think i have implemented it right.
Here is the collision code.
bool Ball::CheckForBrickCollision(float TopLeftX,float
TopRightX,float BottomLeftY,
floatTopLeftY,floatBottomLeftX,
float BottomRightX,
float BottomRightY,
float TopRightY)
{
bool Hit;
if ((ball_xpos>=TopLeftX ) && (ball_xpos<=TopRightX)&&
(ball_xpos>=BottomLeftX ) && (ball_xpos<=BottomRightX
&& (ball_ypos>=BottomLeftY ) && (ball_ypos<=TopLeftY)&&
(ball_ypos>=BottomRightY ) && (ball_ypos<=TopRightY))
{
if ((ball_ypos>=BottomLeftY) && (ball_ypos<=TopLeftY)&& (ball_ypos>=BottomRightY) && (ball_ypos<=TopRightY))
{
Hit = true;
// reflect x-axis velocity
ball_dy=-ball_dy;
// update position
ball_ypos+=ball_dy;
}
else if ((ball_xpos>=TopLeftX ) && (ball_xpos<=TopRightX)|| (ball_xpos>=BottomLeftX ) && (ball_xpos<=BottomRightX))
{
Hit = true;
// reflect x-axis velocity
ball_dx=-ball_dx;
// update position ball_xpos+=ball_dx;
}
}
else
{
Hit = false;
}
return(Hit);
}
i am passing the blocks co-ordinates to the function.
I have tryied testing the balls co-ordinates with the blocks co-ordinates but that didnt work right either.
Any help would be greatful
It has being recking my head for weeks now
Thanks
Ructions
I can''t quite tell how this is supposed to work but there''s a logical error of some sort.
Sorry, but Since I can''t quite tell what you''re trying to do here, I can''t tell you how to make it work, I can just point out what''s obviously wrong.
You''ve already established this. They outer "if" block already checks for these conditions and then some. If the outer if block executes this one automaticaly executes. It''s as if that inner if wasn''t even there.
And of course, if an if block always executes then it''s else block never executes.
Sorry, but Since I can''t quite tell what you''re trying to do here, I can''t tell you how to make it work, I can just point out what''s obviously wrong.
quote:
if ((ball_ypos>=BottomLeftY) && (ball_ypos<=TopLeftY)&& (ball_ypos>=BottomRightY) && (ball_ypos<=TopRightY))
{
.... // reflect x-axis velocity
}
You''ve already established this. They outer "if" block already checks for these conditions and then some. If the outer if block executes this one automaticaly executes. It''s as if that inner if wasn''t even there.
quote:
else if .... ....
And of course, if an if block always executes then it''s else block never executes.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement