【问题标题】:Make object move up/down 315 degree slope based on working code from 45 degree slope根据工作代码从 45 度坡度使对象向上/向下移动 315 度坡度
【发布时间】:2011-10-02 02:08:44
【问题描述】:

好的,所以我需要知道如何根据我用于将所述对象向上/向下移动 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;
            }
        }
    }
}

【问题讨论】:

  • 也许我只是因为周五下午的冷漠而受苦,但两个代码示例看起来都一样。对于 315 度的情况,你到底做了什么不同的事情?
  • 手动计算出循环一次迭代中每一步的正确结果。单步执行调试器中的代码,一次一条语句。在每个语句的开头和结尾,将您手工计算出的正确结果与实际结果进行比较。结果与正确结果不同的语句是有错误的语句。
  • 对不起,我将错误的内容复制粘贴到问题中。现在应该更有意义了。
  • 利用游戏世界中的几何图形,在纸上制作一些图表并收集一些数字,然后将其与您在游戏中得到的结果进行比较。照 Eric Lippert 所说的去做。

标签: c# xna


【解决方案1】:

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

// 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 圆周率。 315 度 = 7/8 圆周率。切线函数根据斜率分别为您提供 1 和 -1,并相应地偏移字符。

【讨论】:

    猜你喜欢
    • 2019-12-29
    • 1970-01-01
    • 2017-06-27
    • 1970-01-01
    • 1970-01-01
    • 2019-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多