AndresTraks / BulletSharpPInvoke

.NET wrapper for the Bullet physics library using Platform Invoke

Home Page:http://andrestraks.github.io/BulletSharp/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to get collisions between two GImpactMesh?

kvasnevskyivlad opened this issue · comments

Hello!
I tried code above, but it doesn't work for me, what is wrong? In Contact_Test_1 I use BoxShape and test passed, but in Contact_Test_2 I use GImpactMeshShape and it doesn't work.

    [TestFixture]
    public class ContactTestCallbackTests
    {
        private PhysicsContext _context;

        [OneTimeSetUp]
        public void SetUp()
        {
            _context = new PhysicsContext();
            _context.InitializeWorld();
        }

        private CollisionShape CreateGImpactShape(TriangleIndexVertexArray shapeData)
        {
            var shape = new GImpactMeshShape(shapeData)
            {
                Margin = 0
            };
            shape.UpdateBound();
            return shape;
        }

        private CollisionShape CreateTorusCollisionShape()
        {
            return CreateGImpactShape(new TriangleIndexVertexArray(Torus.Indices, Torus.Vertices));
        }

        [Test]
        public void Contact_Test_2()
        {
            var shape = CreateTorusCollisionShape();

            GImpactCollisionAlgorithm.RegisterAlgorithm(_context.Dispatcher);
            
            var obj1 = _context.AddStaticBody(shape, Matrix.Translation(0, 0, 0));
            var obj2 = _context.AddStaticBody(shape, Matrix.Translation(0, 0, 0));

            using (var callback = new SimpleCallback())
            {
                _context.World.ContactPairTest(obj1, obj2, callback);
                Assert.That(callback.WasCalled, Is.True);
            }
        }

        [Test]
        public void Contact_Test_1()
        {
            var shape = new BoxShape(2);

            var sphere1 = _context.AddStaticBody(shape, Matrix.Translation(0, 0, 0));
            var sphere2 = _context.AddStaticBody(shape, Matrix.Translation(0, 0, 0));

            using (var callback = new SimpleCallback())
            {
                _context.World.ContactPairTest(sphere1, sphere2, callback);
                Assert.That(callback.WasCalled, Is.True);
            }
        }

        private sealed class SimpleCallback : ContactResultCallback
        {
            public SimpleCallback()
            {
            }

            public bool WasCalled { get; private set; }

            // Called with each contact for your own processing (e.g. test if contacts fall in within sensor parameters)
            public override double AddSingleResult(ManifoldPoint contact,
                CollisionObjectWrapper colObj0, int partId0, int index0,
                CollisionObjectWrapper colObj1, int partId1, int index1)
            {
                WasCalled = true;
                return 0;
            }
        }

        [OneTimeTearDown]
        public void TearDown()
        {
            _context.Dispose();
        }
    }