• Create Account

## Ray to Plane Collision

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1lesshardtofind  Members

104
Like
0Likes
Like

Posted 07 January 2013 - 11:26 PM

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;
}


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

### #2SuperVGA  Members

1132
Like
2Likes
Like

Posted 08 January 2013 - 12:45 AM

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, 08 January 2013 - 04:28 AM.

### #3DT....  Members

487
Like
0Likes
Like

Posted 08 January 2013 - 05:25 AM

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.

### #4lesshardtofind  Members

104
Like
0Likes
Like

Posted 09 January 2013 - 12:59 AM

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)

### #5Mercile55  Members

136
Like
0Likes
Like

Posted 21 January 2013 - 06:50 PM

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, 21 January 2013 - 06:53 PM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.