XNA Creators Club Online
Page 1 of 1 (2 items)
Sort Posts: Previous Next

2D Intersection Problem

Last post 05-11-2008 7:21 PM by JohnWestMinor. 1 replies.
  • 05-11-2008 12:52 PM

    2D Intersection Problem

    OK, so I've got 2D pixel intersection working great.

    Only problem is that if the velocity of the player is large enough, the player's texture can penetrate through the wall's texture. The collision is detected and the movement of the player is reversed (using the wall's normal). If the player has penetrated enough, the same thing happens and the player becomes stuck in the wall.

    I know that when intersection is detected, I need to move the player's texture so that it is not penetrating the wall's texture. How do I go about this, and how can I detect how many pixels are actually overlapping?
  • 05-11-2008 7:21 PM In reply to

    Re: 2D Intersection Problem

    This is how I would handle this:

    for(i=0;i<collisionobjectscount;i++)
    {
           xcollision=false;
           ycollision=false;
    // I have four sets to check for positive and negative speed values.
    //this checks if the object is past something before the speed is added and if it's past something after. If it isn't past it before, but is after the speed is added, there's an X axis collision. For there to be a real collision there needs to be an X and Y axis collision.
            if(Xposition>=collisionobject[ i ].X+collisionobject[ i ].spaceoffset && Xposition+Xspeed<=collisionobject[ i ].X-collisionobject[ i ].spaceoffset)
            {
                xcollision=true;
            }
    //this checks if the object is past something before the speed is added and if it's past something after. If it isn't past it before, but is after the speed is added, there's an X axis collision. For there to be a real collision there needs to be an X and Y axis collision.
            if(Xposition<=collisionobject[ i ].X-collisionobject[ i ].spaceoffset && Xposition+Xspeed>=collisionobject[ i ].X+collisionobject[ i ].spaceoffset)
            {
                xcollision=true;
            }
    //this checks if the object is past something before the speed is added and if it's past something after. If it isn't past it before, but is after the speed is added, there's a Y axis collision. For there to be a real collision there needs to be an X and Y axis collision.
            if(Yposition>=collisionobject[ i ].Y+collisionobject[ i ].spaceoffset && Yposition+Yspeed<=collisionobject[ i ].Y-collisionobject[ i ].spaceoffset)
            {
                ycollision=true;
            }
    //this checks if the object is past something before the speed is added and if it's past something after. If it isn't past it before, but is after the speed is added, there's a Y axis collision. For there to be a real collision there needs to be an X and Y axis collision.
            if(Yposition<=collisionobject[ i ].Y-collisionobject[ i ].spaceoffset && Yposition+Yspeed>=collisionobject[ i ].Y+collisionobject[ i ].spaceoffset)
            {
                ycollision=true;
            }
            if(ycollision==true && xcollision==true)
            {
            //these adjust the speed to hit the object rather than to go through it.
                if(xspeed>0)
                {
                    Xspeed=Xspeed-((Xposition+Xspeed)-(collisionobject[ i ].X-collisionobject[ i ].spaceoffset));
                }
                if(xspeed<0)
                {
                    Xspeed=Xspeed+((collisionobject[ i ].X+collisionobject[ i ].spaceoffset)-(Xposition-Xspeed));
                }
                if(Yspeed>0)
                {
                    Yspeed=Yspeed-((Yposition+Yspeed)-(collisionobject[ i ].Y-collisionobject[ i ].spaceoffset));
                }
                if(Yspeed<0)
                {
                    Xspeed=Xspeed+((collisionobject[ i ].Y+collisionobject[ i ].spaceoffset)-(Yposition-Yspeed));
                }
            }
    }
    Xposition+=Xspeed;
    Yposition+=Yspeed;

    This is a simple brute force collision detection algorithm. It will check if there's a collision with every object and lower the X and Y speeds so that it will stop at the object rather than go through the object. If it detects a collision with one object, it will adjust the speed for that item, and then with the new speed adjustments it will continue until it finds another collision with the updated speeds and it will do it again. Finally it will move your object after it's checked every item that can have a collision.

    This algorithm also assumes your objects are squares, but you can change that by giving every object its own X and Y spaceoffset variables for the algorithm to consider.
Page 1 of 1 (2 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG