Mask 和 Category Bits 仍然允许碰撞 Box2d
Mask and Category Bits still allowing collision Box2d
据我了解,在 Box2d 中,如果另一个对象的类别在掩码中,则对象将与另一个对象发生碰撞。
我设置了一个非常简单的场景,我有一个正方形应该从地板上掉下来。出于测试目的,我已经做到了,所以面具和类别在正方形和地板上完全不同,如下所示
FilterData cubeFilter = new() { CategoryBits = 0b0001, MaskBits = 0b0010 };
FilterData floorFilter = new() { CategoryBits = 0b0100, MaskBits = 0b1000 };
然而,当 MCVE 为 运行 时,立方体仍然落在地板上而不是掉落。
为什么两个物体还在碰撞?
完整 MCVE
NuGets
Install-Package Box2D.NetStandard -Version 1.0.3
Install-Package Box2D.Window -Version 1.0.3
代码
using Box2D.Window;
using Box2DX.Collision;
using Box2DX.Common;
using Box2DX.Dynamics;
using System.Threading;
namespace Box2dMcve
{
internal static class Program
{
private static void Main()
{
AABB aabb = new();
aabb.LowerBound.Set(-20, -10);
aabb.UpperBound.Set(20, 110);
World world = new(aabb, new Vec2(0.0f, -9.81f), false);
BodyDef cubeDefinition = new();
cubeDefinition.Position.Set(0, 70);
PolygonDef cubeFixture = new();
cubeFixture.SetAsBox(5, 5);
cubeFixture.Friction = 0;
cubeFixture.Restitution = 0;
cubeFixture.Density = 1;
FilterData cubeFilter = new() { CategoryBits = 0b0001, MaskBits = 0b0010 };
cubeFixture.Filter = cubeFilter;
Body basicCube = world.CreateBody(cubeDefinition);
basicCube.CreateFixture(cubeFixture);
basicCube.SetMassFromShapes();
BodyDef floorDefinition = new();
floorDefinition.Position.Set(0, 20);
PolygonDef floorFixture = new();
floorFixture.SetAsBox(20, 5);
floorFixture.Friction = 0;
floorFixture.Restitution = 0;
FilterData floorFilter = new() { CategoryBits = 0b0100, MaskBits = 0b1000 };
floorFixture.Filter = floorFilter;
Body floor = world.CreateBody(floorDefinition);
floor.CreateFixture(floorFixture);
floor.SetStatic();
Thread windowThread = new(() =>
{
SimulationWindow game = new("Physics Simulation", 800, 600);
game.UpdateFrame += (_, _) => { world.Step(0.02f, 8, 3); };
game.SetView(new CameraView());
DrawPhysics physicsDrawer = new(game);
physicsDrawer.AppendFlags(DebugDraw.DrawFlags.Aabb);
physicsDrawer.AppendFlags(DebugDraw.DrawFlags.Shape);
world.SetDebugDraw(physicsDrawer);
game.VSync = OpenTK.VSyncMode.Adaptive;
game.Run(60, 0);
});
windowThread.Start();
}
}
}
原来我没有为世界设置联系人过滤器。我想我只是假设它会默认执行此操作。
添加行 world.SetContactFilter(new ContactFilter());
解决了这个问题。
据我了解,在 Box2d 中,如果另一个对象的类别在掩码中,则对象将与另一个对象发生碰撞。
我设置了一个非常简单的场景,我有一个正方形应该从地板上掉下来。出于测试目的,我已经做到了,所以面具和类别在正方形和地板上完全不同,如下所示
FilterData cubeFilter = new() { CategoryBits = 0b0001, MaskBits = 0b0010 };
FilterData floorFilter = new() { CategoryBits = 0b0100, MaskBits = 0b1000 };
然而,当 MCVE 为 运行 时,立方体仍然落在地板上而不是掉落。
为什么两个物体还在碰撞?
完整 MCVE
NuGets
Install-Package Box2D.NetStandard -Version 1.0.3
Install-Package Box2D.Window -Version 1.0.3
代码
using Box2D.Window;
using Box2DX.Collision;
using Box2DX.Common;
using Box2DX.Dynamics;
using System.Threading;
namespace Box2dMcve
{
internal static class Program
{
private static void Main()
{
AABB aabb = new();
aabb.LowerBound.Set(-20, -10);
aabb.UpperBound.Set(20, 110);
World world = new(aabb, new Vec2(0.0f, -9.81f), false);
BodyDef cubeDefinition = new();
cubeDefinition.Position.Set(0, 70);
PolygonDef cubeFixture = new();
cubeFixture.SetAsBox(5, 5);
cubeFixture.Friction = 0;
cubeFixture.Restitution = 0;
cubeFixture.Density = 1;
FilterData cubeFilter = new() { CategoryBits = 0b0001, MaskBits = 0b0010 };
cubeFixture.Filter = cubeFilter;
Body basicCube = world.CreateBody(cubeDefinition);
basicCube.CreateFixture(cubeFixture);
basicCube.SetMassFromShapes();
BodyDef floorDefinition = new();
floorDefinition.Position.Set(0, 20);
PolygonDef floorFixture = new();
floorFixture.SetAsBox(20, 5);
floorFixture.Friction = 0;
floorFixture.Restitution = 0;
FilterData floorFilter = new() { CategoryBits = 0b0100, MaskBits = 0b1000 };
floorFixture.Filter = floorFilter;
Body floor = world.CreateBody(floorDefinition);
floor.CreateFixture(floorFixture);
floor.SetStatic();
Thread windowThread = new(() =>
{
SimulationWindow game = new("Physics Simulation", 800, 600);
game.UpdateFrame += (_, _) => { world.Step(0.02f, 8, 3); };
game.SetView(new CameraView());
DrawPhysics physicsDrawer = new(game);
physicsDrawer.AppendFlags(DebugDraw.DrawFlags.Aabb);
physicsDrawer.AppendFlags(DebugDraw.DrawFlags.Shape);
world.SetDebugDraw(physicsDrawer);
game.VSync = OpenTK.VSyncMode.Adaptive;
game.Run(60, 0);
});
windowThread.Start();
}
}
}
原来我没有为世界设置联系人过滤器。我想我只是假设它会默认执行此操作。
添加行 world.SetContactFilter(new ContactFilter());
解决了这个问题。