# How to resolve a sphere-mesh collision?

I'm making a rolling-ball type game, and I can't seem to get my sphere-mesh collision code working properly.

Currently what I have for the main loop (in regards to collision) is the following:

Calculate player acceleration based on input
Clamp velocity to avoid clipping
For each solid object:
Check for collision for this object and resolve (how I do this is below)

If there were any collisions:
Normalize accumulated normal vector (explained below)
Slow down ball to account for friction


I tried two main methods for resolving collisions, each with their own problems.

The first method:

For each triangle:
If there is a collision:
Correct player velocity along normal based on distance
Accumulate normal for friction calculations later
If velocity perpendicular to surface is above threshold:
Rebound ball off surface with damping factor
Otherwise, if moving toward surface instead of away:
Clear perpendicular velocity


The problem with this method is, when I hit a wall, the player will begin to bounce erratically. The collision I'm using to test this looks like this (player to scale):

The second method is similar to the first:

Set collision distance accumulator to arbitrarily high value
Set triangle index to -1 (to denote that there's no collisions)

For each triangle:
If there is a collision:
Calculate collision distance
If this distance is smaller than the accumulator value:
Set triangle index to current triangle
Set collision distance accumulator to current distance value

If the index is not -1 (there was a collision):
Correct player velocity along normal based on distance in accumulator
Accumulate normal for friction calculations later
If velocity perpendicular to surface is above threshold:
Rebound ball off surface with damping factor
Otherwise, if moving toward surface instead of away:
Clear perpendicular velocity


This fixes the bounciness issue, but now I can go partially through surfaces which are supposed to be solid.

I have no idea where to turn at this point. Hopefully someone here can provide some insight as to what's going on.

[EDIT] I can add some video if that'd help anyone.

[EDIT 2] Here's a link of a video using the first method: https://drive.google.com/open?id=1n1MxTYQkzKA2eixTkEOD3E7eOd0Ryl4f

[EDIT 3] I redid the code somewhat, here's some related code snippets: https://pastebin.com/GQd9bwgB

It still bounces, like so: https://drive.google.com/open?id=1JPJ4lkIB0wBhjnlT5mawxKwq0dC6DlNr

I think I know the cause, but I don't know how to best fix the issue.

• Please add a video to showcase the first method – ColdSteel May 17 at 2:04
• @ColdSteel Added! Let me know if that is not sufficient. – Yoshimaster May 17 at 2:15
• Seems like the problem is that it bouncing way too much than it should ? Probably it calculates the bounce for each triangle - so the bounce back force adds up like that, now I see why you came up with the second method you want to bounce back of only one surface (that is the closest) right ? – ColdSteel May 17 at 2:16
• I set the rebound factor to multiply the velocity by 0.7, so I'm not sure why this would occur. Even it it detected multiple collisions, that would only decrease the velocity even further. However, even a gentle touch is enough to cause a huge bounce. – Yoshimaster May 17 at 2:18
• what are you doing with accumulated normal ? What happened if you break the for each loop after the first collision occurred ? Does it still bounces like that ? – ColdSteel May 17 at 2:23