20929

Detect whether a specific part of body collided with another body in box2d

Question:

Kindly suggest some explanation or code regarding how can i detect <strong>collision between a specific part of one body with another body</strong> in box2d with libgdx.I am able to detect simple collision between two bodies using Contact Listener as mentioned <a href="https://github.com/libgdx/libgdx/wiki/box2d#contact-listeners" rel="nofollow">here</a>But I also want to check <strong>which part of bodies are ovelapping</strong>.

Thanks,

Answer1:

The ContactListener provides you with Contact as a callback parameter. Those contacts will tell you which Fixtures did collide via contact.getFixtureA() and contact.getFixtureB().

What people usually do to find out which part of their bodies collided, is to build them with several Fixtures via body.createFixture(...).

You can set user data on Fixture as well as on Body with fixture.setUserData() and body.setUserData(). You could either save your fixture somewhere else and compare via contact.getFixtureA() == xxx.savedFixture.

That might be in your entity for example like the following:

public class Player { public Fixture arm; // create the player body and store the arm fixture body.setUserData(this); arm = body.createFixture(...); }

Then later you can do this in your contact listener:

public void beginContact(Contact contact) { if (contact.getFixtureA().getBody().getUserData().getClass().equals(Player.class)) { if (contact.getFixtureA() == ((Player)contact.getFixtureA().getBody().getUserData()).arm == contact.getFixtureA()) { // the arm collided with something } } }

Or you might just add some user data like fixture.setUserData("arm") which you can then easily check. In your contact callback handler.

Recommend

  • Error while building libtensorflow.so on raspberry pi
  • perl one-liner + add condition before edit file
  • ASP.NET Identity Confirmation Email Is Plain Text Instead of HTML
  • Getting connection returns false even when the device has connection
  • Pytest: How to make sure a certain fixture is called first
  • Understanding the implementation of memcpy()
  • How to print a large JPanel in several page
  • preg_split is case insensitive with special characters
  • Bash convert string to timestamp
  • Why is Google indexing Friendly URL mixed with hyphens and %20?
  • How to rollback commits in Behat functional tests with Symfony2 and Doctrine?
  • How can I combine values of checkboxes with values of text in php?
  • Rspec + puppet: nested fixtures?
  • Get Current NT Header Data of running Process with C/C++
  • Current x86 privilege level on a custom OS
  • Miller Rabin Primality test in C++ bug?
  • How to get Email address from outlook contacts for the names listed in a column?
  • Creating certificate using makecert without pvk file
  • c# Resize parent control when a child pictureBox changes the Image
  • How to search and isolate attributes of FASTA formatted text in R
  • Prepopulate database with fixtures or with script?
  • Formatting of number from contacts
  • Sitecore 8: Number of Recipients in List Manager not calculating properly
  • Cannot convert a char value to money. The char value has incorrect syntax
  • What is the equivalent of Android permissions in iOS development? [duplicate]
  • Creating a C++ function that calls other Lua function
  • pythonanywhere - How do I use websockets to transmit messages as per the web2py messaging example?
  • Defined variables not working in javascript files when I use getScript
  • Can't remove headers after they are sent
  • Cannot get the UserManager class
  • How to synchronize jQuery dialog box to act like alert() of Javascript
  • Unable to decode certificate at client new X509Certificate2()
  • Needing to do .toArray() to get output of mongodb .find() on key name not value
  • MailKit: The IMAP server replied to the 'EXAMINE' command with a 'BAD' response
  • Recording logins for password protected directories
  • Splitting given String into two variables - php
  • Check if a string to interpolate provides expected placeholders
  • Symfony2: How to get request parameter
  • ORA-29908: missing primary invocation for ancillary operator
  • How can I use `wmic` in a Windows PE script?