• Advertisement
Sign in to follow this  

Ray to Plane Collision

This topic is 1828 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts



   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

Share this post

Link to post
Share on other sites
I'm sorry I don't have time to dive into your code right now, but I suggest, if you have further trouble making it work,
That you base the y value on the result of a concept called "Barycentric Coordinates". If you're using a grid of quads,
it's easy to compute. (Given that the area of a right-angled triangle is 0.5h*b)

It revolves around weighted coords of the triangle (A, B, C) that currently surround your camera (P).
Basically, the weight of A (wA) is
area(B.xz, P.xz, C.xz) / area(B.xz, A.xz, C.xz)
(the surrounding triangle) / (the opposing triangle to the triangle-point in question (here: A))

So you can add together
A.y*wA + B.y * wB + C.y * wC
to get the resulting P.y. Edited by SuperVGA

Share this post

Link to post
Share on other sites

Also, in order to get your camera's "position" and "look at" vector to travel smoothly, research on "linear interpolation" might prove useful (quadratic/cubic bezier curves too but for later) in order to prevent the player, especially the programmer, from getting sick.

Share this post

Link to post
Share on other sites

Thank you for the responses.  I adjusted my Vector3 structure to defined operator overloads which made the entire equation alot easier.  It now works to a degree but not how I expected.  It has me floating to the right point rather than just staying on top of the terrain.  It made me realize I'm going to need working structs and formulas in a library before I really delve into this so I'm going to jump into some calculus websites for a few days and program a math library for me to use.


I will look into the Linear Interpolation, Barycentric Coordinates, and quadratic/cubic bezier curves.  (thanks for that sometimes I just need to find the right phrase to google)

Share this post

Link to post
Share on other sites
If it's a heightmap, you should already know the A,B,C of any given triangle of the terrain (you must know at least two points on the plane to calculate third), and then use SuperVGA's formula to calculate it. Calculating collision in real-time against a terrain is a waste of time (unless it's morphing infinite terrain), and you want that time to be spent on collision detection with other things, i.e. buildings, moving objects, etc.

EDIT: Also, is your Temp vector normalized, when calling DotProduct, or do you normalize inside? Edited by Mercile55

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement