Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get collisions between two GImpactMesh? #82

Open
kvasnevskyivlad opened this issue Jan 20, 2022 · 0 comments
Open

How to get collisions between two GImpactMesh? #82

kvasnevskyivlad opened this issue Jan 20, 2022 · 0 comments

Comments

@kvasnevskyivlad
Copy link

kvasnevskyivlad commented Jan 20, 2022

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();
        }
    }
@kvasnevskyivlad kvasnevskyivlad changed the title How to get collisions between to GImpactMesh? How to get collisions between two GImpactMesh? Jan 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant