【发布时间】: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 所说的去做。