我有一个冲突检测代码,它不起作用,我也不知道如何纠正它,我已经尝试了Internet,但是我发现仅用于固定盒检测。我只显示左侧,但是所有方向都是相同的问题。
所以我有返回数据和tileSize的“地图”类。我有一个“ testCollisionSolid”,如果它在两个不同的点检测到与盒子的碰撞,则返回true。我有一个Vector2“位置”和一个矩形“盒子”,它们不在同一位置,我让盒子计算出他的最大值以使其静止,所以当位置为(x:0,y:0)时盒子(x:0,y:10,w:200,h:400)在这种情况下。
bool testCollisionSolid()
{
if (map.IsSolid(collisionPointA) || map.IsSolid(collisionPointB))
{
return true;
}
else
return false;
}
if (velocity.X > 0) // collision right
{
collisionPointA = new Point(Box.Right, staticBoxQuarter1X4);
collisionPointB = new Point(Box.Right, staticBoxQuarter3X4);
if (testCollisionSolid())
{
velocity.X = 0;
int correctedPositionX = (int)Math.Floor(((Position.X + map.TileSize.X / 2f) / map.TileSize.X) * map.TileSize.X) + 1;
Position = new Vector2(correctedPositionX, Position.Y);
}
}
问题来自“ correctedPositionX”,当我触摸实心砖(重新定位)时,如何创建弹出式窗口?
re @mrblewog,我尝试使其保持简单,因此我给出了更完整的代码,但没有构造函数:
The ChracterAnimation类:
private Texture2D spriteSheet;
private Rectangle BiggerStaticBox;
public Point StaticQuarter;
public Rectangle StaticBox(Point pPosition) // Box static
{
return new Rectangle(pPosition + BiggerStaticBox.Location, BiggerStaticBox.Size);
}
玩家类别:
public Rectangle Box { get; private set; }
private int staticBoxQuarter1X4,staticBoxQuarter3X4,
staticBoxQuarter1Y4, staticBoxQuarter3Y4;
private ChractersAnimation playerAnimations;
private Vector2 _position;
public Vector2 Position
{
get
{
return _position;
}
protected set
{
_position = value;
Box = playerAnimations.StaticBox(Position.ToPoint());
staticBoxQuarter1X4 = Box.Center.X - playerAnimations.StaticQuarter.X;
staticBoxQuarter3X4 = Box.Center.X + playerAnimations.StaticQuarter.X;
staticBoxQuarter1Y4 = Box.Center.Y - playerAnimations.StaticQuarter.Y;
staticBoxQuarter3Y4 = Box.Center.Y + playerAnimations.StaticQuarter.Y;
}
}
bool testCollisionSolid()
{
if (map.IsSolid(collisionPointA) || map.IsSolid(collisionPointB))
{
return true;
}
else
return false;
}
if (velocity.X > 0) // collision right
{
collisionPointA = new Point(Box.Right, staticBoxQuarter1X4);
collisionPointB = new Point(Box.Right, staticBoxQuarter3X4);
if (testCollisionSolid())
{
velocity.X = 0;
int correctedPositionX = (int)Math.Floor(((Position.X + map.TileSize.X / 2f) / map.TileSize.X) * map.TileSize.X) + 1;
Position = new Vector2(correctedPositionX, Position.Y);
}
}
MapEditor类:
private TextureSheet tiles;
private int[,] tilesData;
public Point TileSize { get; private set; }
private Point mapSize;
private int GetIdWithPosition(Point pPosition)
{
Point id = new Point((int)Math.Floor(pPosition.X / (float)TileSize.X), (int)Math.Floor(pPosition.Y / (float)TileSize.Y));
if (id.X >= 0 && id.X < mapSize.X && id.Y >= 0 && id.Y < mapSize.Y)
return tilesData[id.X, id.Y];
else
return -1;
}
public bool IsSolid(Point pPosition)
{
int id = GetIdWithPosition(pPosition);
if (id == 0)
return false;
else
return true;
}
“ correctedPositionX”占据Vector2位置,并添加tileSize的一半。然后将其除以tileSize.X并获得多个新位置。我加了1,因为它在磁贴和Box之间留下了不可见的像素。但是现在盒子不在Vector2的中心位置,而是创建播放器Box的弹出窗口。我不知道该让谁做,谢谢你的帮助Position Green/Yellow, Box Red, it's a really bad diagrams i know