# Calculating the impact of a collision in box2d for Processing

**URL:** https://discourse.processing.org/t/calculating-the-impact-of-a-collision-in-box2d-for-processing/17835
**Category:** Libraries
**Created:** [February 15, 2020, 8:28am UTC](https://discourse.processing.org/t/calculating-the-impact-of-a-collision-in-box2d-for-processing/17835 "2020-02-15T08:28:13Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ilinx](https://avatars.discourse-cdn.com/v4/letter/i/7cd45c/32.png) [@ilinx](https://discourse.processing.org/u/ilinx)
#### Post date: [February 15, 2020, 8:28am UTC](https://discourse.processing.org/t/calculating-the-impact-of-a-collision-in-box2d-for-processing/17835/1 "2020-02-15T08:28:13Z")

</div>

Hi all,

I’ve been searching around the internet for a while now, but couldn’t find a solution to the following problem:

In box2d for Processing, I’d like to calculate the **impact of a contact** in order to modify certain parameters of a sound (e.g. its level). I’ve realized that Fisica has a class named FContactResult – is there anything comparable, ready-to-go, in box2d for Processing?

The thing is, I would like both angular velocity and linear velocity to be taken into account when calculating the impact force.

I’ve posted some of my code below (it’s based on – what else – Daniel Shiffman’s fantastic tutorials). I’ve also marked the spot where I’d guess the essential lines have to go. As you’ll see, I’ve even managed to get the “manifold”, though I don’t know how to go on from there.

Any tips and/or hints to useful resources are appreciated.

Many thanks in advance &  
all the best to everybody  
ilinx

```auto
void beginContact(Contact cp){
  
  Fixture f1 = cp.getFixtureA();
  Fixture f2 = cp.getFixtureB();
  Body b1 = f1.getBody();
  Body b2 = f2.getBody();
  
  Object o1 = b1.getUserData();
  Object o2 = b2.getUserData();
  
  if(o1.getClass() == Boundary.class && o2.getClass() == Box.class){
    collisionCount++;
    println("hello world! " + collisionCount);
    
    Box b = (Box)o2;
    
    Manifold manifold = cp.getManifold();
    

  }
}

```

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [February 18, 2020, 8:42pm UTC](https://discourse.processing.org/t/calculating-the-impact-of-a-collision-in-box2d-for-processing/17835/2 "2020-02-18T20:42:49Z")

</div>

If you are using the Processing Box2D library, it uses JBox2D. Specifically, it looks like you are wondering about the available methods on a Contact object. Here they are:

- [https://github.com/jbox2d/jbox2d/blob/master/jbox2d-library/src/main/java/org/jbox2d/dynamics/contacts/Contact.java](https://github.com/jbox2d/jbox2d/blob/master/jbox2d-library/src/main/java/org/jbox2d/dynamics/contacts/Contact.java)

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [February 19, 2020, 2:43am UTC](https://discourse.processing.org/t/calculating-the-impact-of-a-collision-in-box2d-for-processing/17835/3 "2020-02-19T02:43:49Z")

</div>

You might also find this Box2D tutorial relevant, particularly the part on " Collision points and the normal".

[https://www.iforce2d.net/b2dtut/collision-anatomy](https://www.iforce2d.net/b2dtut/collision-anatomy)

It is for C++, not Java, but it might be helpful for explaining the concepts of what a Box2D Contact is and how it can be used. Maybe that will be useful for understanding the Java API.

---

<div class="post-metadata">

### Author: ![ilinx](https://avatars.discourse-cdn.com/v4/letter/i/7cd45c/32.png) [@ilinx](https://discourse.processing.org/u/ilinx)
#### Post date: [February 23, 2020, 6:10am UTC](https://discourse.processing.org/t/calculating-the-impact-of-a-collision-in-box2d-for-processing/17835/4 "2020-02-23T06:10:24Z")

</div>

Hi Jeremy,

thanks for your reply! I had indeed read the iforce2d article already and found it very helpful. Also, the idea of looking into the Contact class was a very good point.

For my project of creating virtual sound-machines, however, I have in the meantime turned to Fisica, which seems to wrap JBox2D just perfectly for my needs. It makes it very simple to calculate a whole lot of details from contacts and map them to Beads-objects and midi-outputs.

However, I have now stumbled upon a new question concerning the FGearJoint class, but I guess I’ll open a new thread for that …

Thanks & all the best,  
ilinx
