Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm making a simple pong game, and things are great so far, but I have an odd bug which causes my ball (well, it's a box really) to get stuck on occasion when detecting collision against the ceiling or floor. It looks as though it is trying to update too frequently to get out of the collision check.

Basically the box slides against the top or bottom of the screen from one paddle to the other, and quickly bounces on and off the wall while doing so, but only bounces a few pixels from the wall.

What can I do to avoid this problem? It seems to occur at random. Below is my collision detection for the wall, as well as my update method for the ball.

public void UpdatePosition()
{
    size.X = (int)position.X;
    size.Y = (int)position.Y;
    position.X += speed * (float)Math.Cos(direction);
    position.Y += speed * (float)Math.Sin(direction);
    CheckWallHit();
}

// Checks for collision with the ceiling or floor.
// 2*Math.pi = 360 degrees
// TODO: Change collision so that ball bounces from wall after getting caught
private void CheckWallHit()
{
    while (direction > 2 * Math.PI)
    {
        direction -= 2 * Math.PI;
    }

    while (direction < 0)
    {
        direction += 2 * Math.PI;
    }

    if (position.Y <= 0 || (position.Y > resetPos.Y * 2 - size.Height))
    {
        direction = 2 * Math.PI - direction;
    }
}
share|improve this question
    
Can you explain what each of those points represents? You haven't told us what object size refers to, for example. And what's resetPos? –  Tharwen Jun 1 '12 at 7:11
2  
You can think about those "guys" as constants for his collision box. Not really relevant. I think he's doing it wrong for a reason: he updates the position and enters the wall. Then he performs collision detection and switches the direction. But at the next step, there's no guarantee he'll be numerically out of the wall.. so he might oscillate due to floating point errors. –  teodron Jun 1 '12 at 8:19
    
@Tharwen I apologize, I should have been more clear. size represents the new rectangle for the square which I am actually using for my "ball." resetPos is is the direct center point of the screen which the ball resets to after each point is scored. –  Dave Voyles Jun 1 '12 at 18:03
1  
Yes @Teodron, I am definitely oscillating. I just couldn't find the word for it late last night when writing this post :p. Thank you for tour input gentlemen, Dan included, I apprecaite it. I'll try to implement this tonight and report back with how it works. It seems that the bool may be key. –  Dave Voyles Jun 1 '12 at 18:04
add comment

2 Answers

up vote 3 down vote accepted

Check my comment for some explanations. Here's a hack:

 public void UpdatePosition()
{
    size.X = (int)position.X;
    size.Y = (int)position.Y;
    oldPos.X = position.X;
    oldPos.Y = position.Y;
    position.X += speed * (float)Math.Cos(direction);
    position.Y += speed * (float)Math.Sin(direction);
    bool collided = CheckWallHit();
    if (collided)
    {
       position.X = oldPos.X + speed * (float)Math.Cos(direction);
       position.Y = oldPos.Y + speed * (float)Math.Sin(direction);
    }
}

Make sure you return a bool for your collision query. That hack should do the trick..

share|improve this answer
    
The only issue I see to run into with this after ading the bool and oldPos as a Vector2 is with the line: bool collided = CheckWallHit(); as CheckWallHit() returns void, I receive the error: Cannot convert source type 'void' to target type 'bool.' I figure this is a pretty basic question to answer, but I'm still pretty new to typcasting. –  Dave Voyles Jun 1 '12 at 19:19
    
Could I use something such as "isTrue ? "true" : "false"" as paramater within CheckWallHit() ? –  Dave Voyles Jun 1 '12 at 19:33
    
Talking to myself at this point but I figured I'd update everyone. I changed CheckWallHit() from returning void to just being a private bool, and having it return true for each if statement. It all comes together fine. Thanks guys! –  Dave Voyles Jun 1 '12 at 19:55
    
Sorry,I was away for a day, glad it worked out (it did for me within this simple framework lots of times) –  teodron Jun 3 '12 at 9:03
add comment

I'm curious about your direction variable.... I see it being used but not how it is being defined. This may not have a thing to do with it but, it just might.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.