Game Development Community

Trying to castray directly beneath the camera

by Steve Lamperti · in Torque Game Engine · 03/22/2004 (1:24 pm) · 5 replies

I'm trying to write a simple routine to do a castray directly below a point, and see if there is a collision. I am most likely doing something very silly, and I was hoping someone could look at the code below and point it out.
It may be something as simple as it not being correct to assume that z-.5 is directly below z, if x and y don't change.
Thanks,

Note: Reposted here from the public forums.


//
// JSL - check below the camera to see where the ground is
//
double BumpIntoTheGround(Point3F& start)
{
   Point3F end;
   bool   result;
   RayInfo rinfo;
     end.x = start.x;
     end.y = start.y;
     end.z = start.z - .5;
   result = gServerContainer.castRay(start, end, TerrainObjectType, &rinfo);
   
   if (result)
      return(rinfo.point.z);
   
   return(0.0);
}

#1
03/22/2004 (2:20 pm)
Make the end.z something larger than that. Cast ray will return the nearest collision. Give it something like 10.0 or 100.0 and you should be fine. You might also look into more than just TerrainObjectType...

- Brett
#2
03/22/2004 (3:00 pm)
I should have described what I am trying to do better. I am calling this each time the free floating camera tries to move. I want to restrict the camera from moving through the ground, so I want to check for something directly below the camera, but I need to check close, so I can stop when I get right next to the ground.

It almost seems to be working, except it works when I am moving along almost parallel to the ground, and doesn't when I head directly towards the ground. I have no idea why.
#3
03/23/2004 (10:07 am)
You may want to cast a couple of rays, try one in the direction of travel and one below. The one below maintains "height" above the ground. And the one in the direction of travel keeps it from bumping into objects "ahead" of it. You could then use the point of impact below the camera to the point of impact ahead of the camera to create a slope to move "up".

Before casting rays (which isn't all that bad except it can pass through many zones and many objects) you might try AABB/AABB intersection tests, or sphere intersection tests. Although according to the book Real-Time Rendering AABB/AABB is a good early out/early rejection test.

Like I said, however, you can start the ray at the point of the camera. Don't worry about how far down you go with the end because it will find the point of nearest intersection that matches the mask supplied.

I've been spending some time in the collision code (which is essentially what you're looking to do) and there are many tests implemented by the engine. Maybe get the normal of the surface that the "ahead" ray intersects, and the point at which it intersects, and then move that point away from the surface, along the normal, by a set amount. However, you might get caught on very steep slopes.

- Brett
#4
03/23/2004 (10:20 am)
@ Brett
Thanks for your information. My ground in my test case, (and for the most part in my project,) is completely flat, so if my xyz stuff is ok, and z-.5 is in fact directly below the object, then I think that just the z test will be enough. What I was thinking, was that maybe the camera has already moved far enough below the ground, so that from the camera location to the ground doesn't intersect the ground, because from the camera is already below the ground. That was worded awkwardly, but I bet it's correct. What I will try is casting from above the current camera point, to my -.5 point, and see if that hits the ground.
#5
03/24/2004 (8:32 am)
I was correct. The problem was that my cast from the location of the camera, to directly below it, was starting below the ground if the camera had moved more then the .5 distance. When I cast from above the location to the .5 below it seems to be working fine.