根据45度斜率的工作代码,使物体上下移动315度

时间:2011-09-30 19:20:35

标签: c# xna

好吧,所以我需要知道如何根据我上下移动所述物体上/下45度斜率的代码,将物体上下移动315度。目前,物体向上/向下移动315度斜坡,就像它的45度一样,这是错误的方式(当他向下和向右时向上和向右)。

以下是代码:

//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 >= tileCol.X)
            {
                //this is where the error is.
                pos.Y = tileCol.Y + lv.tileSize + (dist * -1);
                onSlope = true;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我不完全理解你的代码,因为它的编写方式,你的玩家的位置.Y在“工作”if循环从tile的顶部递减,有效地将他放在矩形的顶部,当他认为从斜坡的底部上升。但是 - 根据上面的代码,以下应该适用于所有角度:

// Check if the tile is a slope
if (lv.type[lv.tile[x, y]] == Tiles.SLOPE)
{
    // Create rectangle collision that is the same size as the tile
    Rectangle tileCol = new Rectangle(x * lv.tileSize, y * lv.tileSize, lv.tileSize, lv.tileSize + 1);

    // If player collision "col" standing on "tileCol"
    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.Right - tileCol.X;

        // Figure out how far to offset the player 
        float offset = (float)(Math.Tan(MathHelper.ToRadians(angle)) * (dist));

        // If player's right is less then or equal to tile's right
        if (col.Right <= tileCol.Right)
        {
            if (angle % 180 < 90)
            {
                pos.Y = tileCol.Bottom + offset;
                onSlope = true;
            }
            else if (angle % 180 > 90)
            {
                pos.Y = tileCol.Top + offset;
                onSlope = true;
            }
        }
    }

45度= 1/8 Pi。 315度= 7/8 Pi。切线函数根据斜率分别给出1和-1,并相应地偏移字符。

相关问题