Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

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

Alright, so I got this code I'm trying to write, but the player won't go down the slope, it goes down and up perfectly on the 45 degree slope, but the 315 degree one is a mess. it goes "up" the downward slope, get's stuck and some other stuff. Anyway here's the code:

//you're in a double for loop going through every tile in the map, the int's being used are x, and y

//check if the tile is a slope
if (lv.type[lv.tile[x, y]] == Tiles.SLOPE)
   //create a rectangle collision box
    Rectangle tileCol = new Rectangle(x * lv.tileSize, (y * lv.tileSize), lv.tileSize, lv.tileSize + 1);

    //if player collision "col" collides with "tileCol" and you haven't done this before this itteration (only happens once per full double loop)
    if (col.Intersects(tileCol) && !onSlope)
        //get the angle of the tile
        float angle = lv.angle[lv.tile[x, y]];

        //get the x distance of how far away the player's right is inside the tile
        float dist = (col.X + col.Width) - tileCol.X;

        //constructs the opposite of a right triangle
        float opposite = (float)(Math.Tan(MathHelper.ToRadians(angle)) * (dist));

        if (angle < 90)
            //if player's right is less then or equal to tile's right
            if (col.X + col.Width <= tileCol.X + tileCol.Width)
                //place player on slope. this works properly
                pos.Y = tileCol.Y - opposite;
                //tell the program we don't wanna go through this again until the next full loop starts.
                onSlope = true;

        else if (angle > 90){
            if ((col.X + col.Width) >= tileCol.X)
                //this is where the error is. the player goes "up" a slope that's 315 degrees, instead of down it.
                //how do I make the player go down the slope that's 315 degrees!?
                pos.Y = tileCol.Y + opposite;
                onSlope = true;

currently using this code, it makes the player move in a 45 degree angle on the 315 degree blocks.

else if (angle > 90)

                                if (col.X >= tileCol.X)
                                    pos.Y = tileCol.Y + lv.tileSize + (dist * -1);

                                    onSlope = true;
share|improve this question
do you have a picture or movie to show what is happening? – Roy T. Sep 30 '11 at 5:46 – CyanPrime Sep 30 '11 at 6:01
up vote 3 down vote accepted

Handle all slope tiles as a rise/run ratio. Each slope tile will have a ratio, and assuming 45 degrees means going up as you go right, 315 degrees will have a ratio of -1. Think back to basic coordinate algebra. Using the player's X position local to the tile, which is the run, solve for pos.Y by multiplying the X position by -1.

However since y = 0 is likely at the bottom of the tiles, you'll have to offset the height by adding the height of the tile.

So the player is colliding with a downwards sloping tile, the slope ratio is negative and the formula should be

pos.Y = lv.tileSize + (pos.X * slopeRatio)


pos.Y = pos.X * slopeRatio

if the slope is going up. Determine if the slope is upwards or downards by the sign of the slope ratio.

share|improve this answer
Thank you, but how do I convert the angle to a rise/run ratio? – CyanPrime Sep 30 '11 at 6:21
@CyanPrime ratio = tan(angle). – ChrisC Sep 30 '11 at 6:25
Sorry, but this doesn't seem to be working. Can you think of any other ways to maybe solve this? – CyanPrime Sep 30 '11 at 6:41
wait, it almost works. it's going to 45 degree angle on the 315 tiles. I'll post what I got so far. – CyanPrime Sep 30 '11 at 6:47

Your Answer


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.