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
Add velocity to position
Add acceleration to velocity
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.