# How to use contact data from simultaneous collisions?

I have a ball that bounces the opposite way to a contact point (jumpPoint) when I press Jump.

The problem I am having is that if the ball has 2 contacts (either on the same object or on two separate objects), it only jumps away from the first contact point. Is there a way to calculate all contacts points with the ball from every object and work out the correct jump angle.

I have tried the 2 following methods: (all variables are vector2’s. Most of the math here is done in Update rather than collision, I’ve just included it here to better show whats going on)

1:

``````void OnCollisionStay2D(Collision2D other)
{
ballPos = transform.position;
contact1 = other.contacts[0].point;
output = conact1 - ballPos;
jumpPoint = -output.normalized;
}
``````

2:

``````void OnCollisionEnter2D(Collision2D other)
{
foreach (ContactPoint2D contact in other.contacts)
{
ballPos = transform.position;
contact1 = contact.point;
output = dir2 - dir1;
jumpPoint = -output.normalized;
}
``````

What do you do to add force? Because if you use physics, and just add all of it in fixedupdate, basically that should be it.

Else, maybe you can supply a little more info on the game and why you chose the setup you have?

TO: Fritsl

The site has been bugged for me for days I’m afraid, I can’t reply to you directly Fritsl, but seem to be able to reply to the question as a whole, so I hope you see this.

The way I add force works fine:

``````myRigidBody.AddForce(jumpPoint * force, ForceMode2D.Impulse);
``````

It’s the calculation of the jump direction, jumpPoint, thats the problem.
Is there a way to get the contact data of each instance of Collision2D other, because right now if the ball in contact with more than one instances of “other” it will choose one of them at random as the object to bounce off.

In addition to this, there are cases where the ball can have more than one contact point with a single object. I know this produces an array, but I am unsure how to use this array correctly. I feel like I need something like the following, but am unsure how to implement it correctly:

``````If (contact.point.amount = 1)
{
contact1 = other.contacts[0].point;
}
else if (contact.point.amount = 2)
{
contact1 = other.contacts[0].point + other.contacts[1].point;
}
``````

vProject: Would you mind posting the solution you came up with please? This forum does not allow users to directly message each other (which I think is stupid, especially for the cases where the problem got solved but was not shared)