2
\$\begingroup\$

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): Diagram of level collision from side, 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.

\$\endgroup\$
  • \$\begingroup\$ Please add a video to showcase the first method \$\endgroup\$ – ColdSteel May 17 at 2:04
  • 1
    \$\begingroup\$ @ColdSteel Added! Let me know if that is not sufficient. \$\endgroup\$ – Yoshimaster May 17 at 2:15
  • \$\begingroup\$ 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 ? \$\endgroup\$ – ColdSteel May 17 at 2:16
  • \$\begingroup\$ 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. \$\endgroup\$ – Yoshimaster May 17 at 2:18
  • \$\begingroup\$ 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 ? \$\endgroup\$ – ColdSteel May 17 at 2:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.