C# 三维壁面滑移碰撞
在我的游戏中,我正在处理墙壁的碰撞问题,而我目前的处理方式是,我被困在墙壁上。我试图让我的角色在墙上滑动,但仍然会发生碰撞 我的角色从我使用他所面对的角度创建的向量中移出 这是我的碰撞功能:C# 三维壁面滑移碰撞,c#,3d,xna,collision,C#,3d,Xna,Collision,在我的游戏中,我正在处理墙壁的碰撞问题,而我目前的处理方式是,我被困在墙壁上。我试图让我的角色在墙上滑动,但仍然会发生碰撞 我的角色从我使用他所面对的角度创建的向量中移出 这是我的碰撞功能: private static bool CheckForCollisions(ref Crate c1, ref Player c2,bool direction) { for (int i = 0; i < c1.model.Meshes.Count; i++)
private static bool CheckForCollisions(ref Crate c1, ref Player c2,bool direction)
{
for (int i = 0; i < c1.model.Meshes.Count; i++)
{
// Check whether the bounding boxes of the two cubes intersect.
BoundingSphere c1BoundingSphere = c1.model.Meshes[i].BoundingSphere;
c1BoundingSphere.Center += c1.position + new Vector3(2, 0, 2);
c1BoundingSphere.Radius = c1BoundingSphere.Radius / 1.5f;
for (int j = 0; j < c2.model.Meshes.Count; j++)
{
BoundingSphere c2BoundingSphere = c2.model.Meshes[j].BoundingSphere;
if (direction)
c2BoundingSphere.Center += c2.position + new Vector3(c2.getPlannedDirection().X, 0, 0);
else if (!direction)
c2BoundingSphere.Center += c2.position + new Vector3(0, 0, c2.getPlannedDirection().Y);
//c2BoundingSphere.Center += c2.position;
if (c1BoundingSphere.Intersects(c2BoundingSphere))
{
return true;
}
}
}
return false;
}
private static bool CheckForCollisions(参考框c1,参考玩家c2,bool方向)
{
对于(int i=0;i
这是我的更新:
for (int x = 0; x <= 29; x++)
{
for (int y = 0; y <= 29; y++)
{
if (crate[x, y].getType() == 11 && collisionEnabled)
{
if (CheckForCollisions(ref crate[x, y], ref player,true))
{
player.clearPlannedDirectionX();
//Console.Write(player.getPosition().X + "," + player.getPosition().Y + "," + player.getPosition().Z);
//movePlayer = false;
}
if (CheckForCollisions(ref crate[x, y], ref player,false))
{
player.clearPlannedDirectionZ();
//Console.Write(player.getPosition().X + "," + player.getPosition().Y + "," + player.getPosition().Z);
//movePlayer = false;
}
}
}
}
for(intx=0;x这看起来应该在gamedev堆栈交换上,我建议切换到光线测试而不是sphere
无论如何,如果你与基于球体的系统结婚,你可以将玩家“推”出墙:
private static bool CorrectCollisions(ref Crate c1, ref Player c2)
{
for (int i = 0; i < c1.model.Meshes.Count; i++)
{
// Check whether the bounding boxes of the two cubes intersect.
BoundingSphere c1BoundingSphere = c1.model.Meshes[i].BoundingSphere;
c1BoundingSphere.Center += c1.position + new Vector3(2, 0, 2);
c1BoundingSphere.Radius = c1BoundingSphere.Radius / 1.5f;
for (int j = 0; j < c2.model.Meshes.Count; j++)
{
BoundingSphere c2BoundingSphere = c2.model.Meshes[j].BoundingSphere;
c2BoundingSphere.Center += c2.position;
Vector3 dir=c2BoundingSphere.Center - c1BoundingSphere.Center;
float center_dist_sq=dir.dot(dir);
float min_dist=c2BoundingSphere.Radius+c1BoundingSphere.Radius;
if (center_dist_sq < min_dist*min_dist) {
dir.normalize();
c2.position += dir*(min_dist-sqrt(center_dist_sq));
}
}
}
return false;
}
private static bool corrector碰撞(参考板条箱c1,参考玩家c2)
{
对于(int i=0;i
然后,您的更新看起来更像:
for (int x = 0; x <= 29; x++) {
for (int y = 0; y <= 29; y++) {
if (crate[x, y].getType() == 11 && collisionEnabled) {
CorrectCollisions(ref crate[x, y], ref player);
}
}
}
for(int x=0;x您在第一个代码中使用了getPlannedDirection().x和getPlannedDirection().Y,但您使用的是clearPlannedDirectionX()和clearPlannedDirectionZ()第二种情况是,如果它在z轴上发生碰撞,那么它会阻止它发生。与x轴相同。这都是在播放器更新其位置之前运行的。这就是为什么它被称为“PlannedDirection”,抱歉,我应该在我的问题中澄清这一点。c2.position+=(c2BoundingSphere.Center-c1BoundingSphere.Center)-(c2BoundingSphere.Radius-c1BoundingSphere.Radius);"无法将Vector3转换为浮点。第二个减号应该是逗号吗?哎呀,对不起,我应该想到这一点。这是因为我在1D中设计了这个想法。我编辑了这个示例来演示如何在3D中提取方向向量。我还结合了球体交点,因为数学重叠,所以没有必要重复两次。在这一行:浮点c输入_dist_sq=dir.dot(dir);一个点积不需要两个矢量吗?@bbdude95:是的,它们是“dir”和“dir”,注意这个变量叫做中心距离平方。我最初写的是=dot(dir,dir)但是我意识到你的框架看起来是面向对象的。我尝试在JavaScript中实现类似的东西,没有任何向量库,你知道如何解决这个问题吗?