0
votes

A new problem i have at the moment is sometimes when the ball is bouncing around the screen, it may all of a sudden get stuck colliding with a rectangle, so it may hit a spot between the rectangle and the side of the screen. This sometimes results in either: a) the ball looks like it's shaking or something, which i am assuming as it's still colliding with the rectangle, it's velocity (as i have defined it) is constantly inversing... but it's still stuck... (that's what i cant solve... lol :( ), or b) when the ball collides with the rectangle, it starts to move along the edge of the rectangle and goes back and forth but it continues to do this... (so it's basically stuck moving just along the edge).

I have uploaded a video to youtube showing this in action (btw... the ball gets stuck and goes up and down the right side of the screen and then starts going along the top edge of the white rectangle and then eventually bounces off into oblivion... well basically frees itself, which is kind of ok but then if you wait 5 more seconds, it repeats this process)

http://www.youtube.com/watch?v=3qfpgtoWbIU&feature=youtu.be

the code i am using for collision detection with the rectangle is:

if (theBall.GetRectangle.Bottom >= cornerSquare.GetRectangle.Top && theBall.GetRectangle.Bottom <= cornerSquare.GetRectangle.Bottom)
{
    theBall.pVelocity.Y = -theBall.pVelocity.Y;
}

thats for detection if the ball hit's the top of the rectangle.

detection with the right side of the screen is:

if (pPosition.X + pTexture.Width >= screenWidth)
{
    pVelocity.X = -pVelocity.X;
}

I hope someone has a simple and effective solution to this as i just want to 'be done' with all this collision detection nonsense lol... as it's taking up most of my time whilst i could be alot more productive doing other parts of the game.

Thanks for reading, taking the time to read, taking the time to help.... ETC.... :D

2

2 Answers

2
votes

Try to check for collision first using a calculated test-rectangle.

For example the ball moves for Vector2(1, 1) per Update. Find out where the Rectangle for collision WOULD be at the next step BEFORE moving.

int nextPosX = currRect.X + (int)movementVector.X;
int nextPosY = currRect.Y + (int)movementVector.Y;

// find out where the rectangle would be after this update:
Rectangle nextStepsCollisionRect = new Rectangle(nextPosX, nextPosY, width, height);

// check if the test rectangle would collide:
if( collisionTest(nextStepsCollisionRect, sideWall) )
{
    // do not move the intended way!
    // invert X and recalc with pre-calculated rectangle again...
} else {
    // no collision. now move the caculated way:
    ball.CollisionRectangle = nextStepsCollisionRect;
}

This is a quite simple way. Use "ray tracing" if the ball is a very fast bullet (just to be mentioned).

1
votes

If your ball is stuck along the edge you can simply bring the ball position to its previuos position before the collision (the one it had in the previous frame), in this way you change its velocity and ensure that the ball is not colliding anymore, this should do the trick.
You can achieve this by subtracting ball speed to ball position.