With previous help from people on this forum I managed to find the distance between a sphere and a plane when they collide which I believe follows these steps.
1. Find two vectors of the quad/triangle plane by subtracting 2 sets of adjacent points.
2. Find the cross product of these 2 vectors to find the plane normal.
3. Normalize the result.
4. Take the dot product of the sphere position and the plane normal to get the final result.
Assuming all of that is correct I thought you just had to check to see if that result was less than the radius, and if it was then subtract the result from the radius add it on to the y position to push the sphere back up.
However when I do this on a test slope it sends my position much higher than it should so I am guessing the response is meant to be more complicated than simply adding it to the y axis, so any hints in the right direction would be great.
What exactly do you mean by distance between a sphere and a plane when they collide?
The minimum distance of a point on the plane to sphere's center?
Also i do not see how your algorithm may calculate this distance. You are using only the plane normal at the fourth step (which does not necessarily point towards the sphere's center). The normal holds no information about the position of the plane in 3d-space, so from my understanding there is no way to calculate the distance between the plane and sphere using a plane normal and the sphere's center.
I'm not really into the topic of collision detection, but what might work is placing a ray at the sphere's center which is pointing in the same direction as the plane normal (which points away from the sphere) and find the intersection point of the ray and the plane.
This should be the point on the plane which is closest to the sphere's center.
I seen have examples of sphere-plane collision using ray intersection and without it, so I am really not sure if it's needed or not, but you may be correct. (Which just means another thing which I will probably get stuck on..~).
ne555, I was actually thinking it had to be moved in the direction of the normal last night (I don't know why I didn't realize it before) but for some reason I can't seem to figure out exactly how to move it in that specific direction (although I am sure it's a very simple equation).
Is the sphere moving ? If so where is the velocity ? What you are trying to do now is snap the sphere onto the plane (a plane that is "centered" at the origin; most likely not representing "surface"). You need another component for the plane to describe it's position relative to the origin.
And also why did you split Pos up ? Could have just done:
Pos += (radius - Distance) * surface[ i ].Norm; // not solution to problem, just "better" way of writing than what you had
The recipe you give is correct provided the plane passes through the origin.
If the plane does not pass through the origin you can shift the whole system so that it does by subtracting a point in the plane from all the position vectors (points in plane and sphere position)
This subtraction will not affect the plane normal, so that calculation can stay the same, but you will have to subtract from the sphere position before doing the dot product.