Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So I'm making a tiled platform game in C#/XNA where the whole world is constantly rotating. The huge hurdle was to find out what side of the tile the player was touching, and what angle it was. I solved with this code that is called after collision detection:

/*This function takes the character object, the block object, and the block's
  transformation matrix and compares the two origins, finds out which side of the block
  the character is on, figures out the slope of the face and returns the angle
public static float quad(characterClass charObj, blockClass blockObj, Matrix transform, int i)
    float result = 0;
    float charx, chary, blockx, blocky;

    Vector2 blockTop, blockSide;

    //Character origin
    charx = charObj.position.X;
    chary = charObj.position.Y;

    blockx = blockObj.position.X;
    blocky = blockObj.position.Y;

    outputDebug = result.ToString();
    //if (xy < zy)
        blockTop = new Vector2(0, 0);

        //The top corner of the box
        blockTop = Vector2.Transform(blockTop, transform);

        if (blockTop.X > charx) //upper left
            blockSide = new Vector2(32, 0);
            blockSide = Vector2.Transform(blockSide, transform);

            //Solve the angle between the two corners
            float degree = (float)Math.Atan2(blockTop.Y - blockSide.Y, blockTop.X - blockSide.X);
            contactAngle = MathHelper.ToDegrees(degree) + 180;
            outputDebug = i.ToString() + " top left";
            result = contactAngle;
        else //upper right
            //same thing, checking bottom left corner
            blockSide = new Vector2(0, 32);
            blockSide = Vector2.Transform(blockSide, transform);

            float degree = (float)Math.Atan2(blockSide.Y - blockTop.Y, blockSide.X - blockTop.X);
            contactAngle = MathHelper.ToDegrees(degree);

            outputDebug = i.ToString() + "top right";
            result = contactAngle;

        line1 = blockTop;
        line2 = blockSide;
    /* if (xx < zx)
        if (xy < zy)
            result = 1;
            result = 2;
        if (xy < zy)
            result = 3;
            result = 4;

    return result;

The problem is, when the blocks are flat (90 degrees if 0 is up), the character comes in contact with the block to it's right> and it leads to a problem where it will calculate the left side of that block and point the character upwards at 360 (visuals: ) and it makes the character start bouncing/twitching:

I need help, I've been staring at this code for hours and can't find out how to fix it. Is there a way to improve the method to make it more accurate? Or, is there a better way to get the angle of the block the character is standing on?

share|improve this question

closed as too localized by Tetrad May 1 '12 at 15:45

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

You should really look at this if you intend to use C# as your language. :) – William Mariager Jan 10 '12 at 6:02
Haha, thanks, I remember reading up on it a long time ago but have long gone forgot – Sebastian Jan 10 '12 at 9:01
Your video is private... – Ken Jan 18 '12 at 10:12
private vid. fix please, I wanna see! – SirYakalot Jan 23 '12 at 16:26
Video is still private. – kaoD Feb 15 '12 at 19:12