Prabhu Kumar

a tech twaddler..
posts - 125, comments - 97, trackbacks - 0

My Links

News

Archives

Post Categories

Blogs I follow

CSS Templates

Twitter

A glitch corrected, DirectDraw, Part 11

In part 8 of this series of posts on DirectDraw, I had mentioned that there still seems to be a minor problem with the program. Read the "Note" section at the end of the post. The problem now has been solved.

Recap
The problem was that even after calculating the new positions for the marbles, the marbles would still be overlapping because of the approximations that we use. And this would sometimes repeat many times over which creates the problem.

Solution
The fix that I'd said in the "Note" section works well. After calculating the new position for the marbles you check again if they collide. And if they do then adjust their positions so that they don't overlap anymore. Every time I move the marbles 2 pixels away from each other and check the distance again. And repeat this until they are no more overlapping. Here is the code:

     MinDistance = ball1->radius + ball2->radius;

    dxx = ball1->xPos - ball2->xPos;
    dyy = ball1->yPos - ball2->yPos;

    //calculate new distance between marbles
    distance = sqrt(dxx*dxx + dyy*dyy);
    printf("COLLISION: New Distance:%d \r\n", (int)distance);

    //this part makes sure that the new positions of the marbles are not overlapping
    //sometimes the new position itself overlaps and that leads to weird behaviour
    //check if the marbles are overlapping
    if ((int)distance <= MinDistance)
    {
        //adjust their positions
        do
        {
            if (ball1->xPos > ball2->xPos)
            {
                ball1->xPos += 1;
                ball2->xPos -= 1;
            }
            else
            {
                ball1->xPos -= 1;
                ball2->xPos += 1;
            }

            if (ball1->yPos > ball2->yPos)
            {
                ball1->yPos += 1;
                ball2->yPos -= 1;
            }
            else
            {
                ball1->yPos -= 1;
                ball2->yPos += 1;
            }

            dxx = ball1->xPos - ball2->xPos;
            dyy = ball1->yPos - ball2->yPos;

            distance = sqrt(dxx*dxx + dyy*dyy);

        }while((int)distance <= MinDistance);
    }
 
So the same set of values for xPos and yPos which used to create the problem before, now work fine.
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Print | posted on Friday, July 31, 2009 11:26 AM |

Feedback

No comments posted yet.
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: