Hey!
I've been trying, for a couple days, to get my Camera to smoothly "walk" across any loaded heightmap with any size triangles. It was suggested to me to use a ray to plane collision to get this done, but I'm having some issues figuring out where I slipped up. Maybe someone here can shed some light on my ignorance.
This function should return a Vector3, which is a struct of 3 float, that represents the point at which collision would occur.
Here is the code:
Vector3 IntersectionPoint(Triangle ColTri, Vector3 CameraPos){
float a = DotProduct(Normal(ColTri), Vector3(0, -1, 0)); // Vector {0, -1, 0} is the up vector, Normal(ColTri) returns the normal of the triangle passed
if(a == 0)
return CameraPos;
Vector3 Temp; //this is used to calculate the distance from the plane
Temp.X = CameraPos.X - ColTri.VertexA.X;
Temp.Y = CameraPos.Y - ColTri.VertexA.Y;
Temp.Z = CameraPos.Z - ColTri.VertexA.Z;
float X = DotProduct(Normal(ColTri), Temp);
float Mag = Magnitude(Normal(ColTri));
float DistanceToPlane = X/Mag; // end of the distance to plane formula
Vector3 ToReturn; // the vector to return
float Multiplier = DistanceToPlane/a;
ToReturn.X = CameraPos.X;
ToReturn.Y = CameraPos.Y-((-1)*Multiplier);
ToReturn.Z = CameraPos.Z;
return ToReturn;
}
I know there may be a couple extra data types as I was trying to dumb down the logic for myself to understand it. I'm very sketchy on calculus still. Been reading on plane math and planes equations, but still haven't found anything yet.
Thanks for your time!
- John






