本文整理汇总了C#中Entities.Collision方法的典型用法代码示例。如果您正苦于以下问题:C# Entities.Collision方法的具体用法?C# Entities.Collision怎么用?C# Entities.Collision使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Entities
的用法示例。
在下文中一共展示了Entities.Collision方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Collide
public void Collide(Entities.Entity entity)
{
if(_tempCollisions == null)
_tempCollisions = new GridCollision[16];
bool runAgain = true;
for (int check = 0; check < 16 && runAgain; check++)
{
runAgain = false;
int collisionCounter = 0;
for (int i = Math.Max(0, (int)(entity.Position.X - entity.Radius) / _tileSize);
i < Math.Min(_width, (entity.Position.X + entity.Radius) / _tileSize)
&& collisionCounter < _tempCollisions.Length;
i++)
{
for (int j = Math.Max(0, (int)(entity.Position.Y - entity.Radius) / _tileSize);
j < Math.Min(_height, (entity.Position.Y + entity.Radius) / _tileSize)
&& collisionCounter < _tempCollisions.Length;
j++)
{
if (_heightMap.Get(i, j, 1) != 0) //does it collide
{
_tempCollisions[collisionCounter].Set(i, j);
collisionCounter++;
}
}
}
for (int i = 0; i < collisionCounter; i++)
{
float d1 = Vector2.DistanceSquared(
new Vector2((_tempCollisions[i].x + 0.5f) * _tileSize, (_tempCollisions[i].y + 0.5f) * _tileSize),
entity.Position);
float d2 = 0;
for (int j = i + 1; j < collisionCounter; j++)
{
d2 = Vector2.DistanceSquared(
new Vector2((_tempCollisions[j].x + 0.5f) * _tileSize, (_tempCollisions[j].y + 0.5f) * _tileSize),
entity.Position);
if (d1 > d2)
{
d1 = d2;
GridCollision tmp = _tempCollisions[j];
_tempCollisions[j] = _tempCollisions[i];
_tempCollisions[i] = tmp;
}
}
Vector2 v = new Vector2(
MathHelper.Clamp(entity.Position.X, (_tempCollisions[i].x) * _tileSize, (_tempCollisions[i].x + 1) * _tileSize),
MathHelper.Clamp(entity.Position.Y, (_tempCollisions[i].y) * _tileSize, (_tempCollisions[i].y + 1) * _tileSize)
);
if (Vector2.DistanceSquared(v, entity.Position) < entity.Radius * entity.Radius)
{
Vector2 n = Vector2.Normalize(entity.Position - v);
if (!(float.IsNaN(n.X) && float.IsNaN(n.Y)))
{
entity.Collision(v, n);
runAgain = true;
}
}
else
break;
}
}
}