Ray Casting

Posts: 2
Joined: 2009.01
Post: #1
Ray Casting Anyone? I have been messing about with openGL, and I made an ocean simulator, but now I want the camera to bob above the waves as you move about, but how can I know how far it is from the camera to the waves below it (i tried a comples method of figuring stuff out but it sucked) so because I am a lingo programmer at heart, I want to raycast down from the camera, find out the distance to teh first hit, then offset the camera, is there an easy, preeschooler maths way to do this, after learning some openGL I am sure there isn't a simple modelsUnderRay(position, direction, number of models to report) command, but I would be very happy if you proved me wrong!
Quote this message in a reply
Post: #2
Opengl is very low level. Thus has no knowledge of models, cameras or whatever that would be necessary for that kind of routine.

So you'll have to write your own.

BUT I am certain you don't need to cast rays to do this. I'd imagine you have some way of determining the height at any given point on the ocean. Combine this with the location of the ocean plane will get you a three-space coordinate of any point on the ocean waves. Seems to me this is all you need to have the camera bob with the ocean. Jus tplace the camera at that point. Then maybe offset the camera by some factor times the normal at that point to get it above the waves.

If you really want to cast a ray to check where the camera is relative to the ocean:
1. translate the point of the camera to the ocean's local coordinates. This will be the orgin of the ray.

2. The direction for the ray will be the negation of the up normal of the ocean plane: The normal pointing down towards the ocean. Normalize this for convenience. Now you got a ray of the form: R = orgin + dir * time.

3. I presume your ocean is made of triangles. So for each triangle in the ocean check if it collides with the ray, and find the minimum time of intersection. This minimum time corresponds to the distance of the camera to the ocean.

4. Modulate your camera's height above the ocean by this distance.

Unfortunately, checking a ray-triangle intersection with anything reasonable complex is ridiculously slow.
Quote this message in a reply
Posts: 365
Joined: 2002.04
Post: #3
What shape is your water object? If it's a regular grid of vertices connected by quads or triangles, you can greatly simplify the maths for finding the altitude of a point on its surface. Given four vertices around the camera's location (this diagram shows the horizontal XZ plane):


You can use linear interpolation between points A and B based upon the camera's X coordinate to find a new point E, and do the same between points C and D to find point F. You can then find the location of the point on the surface below the camera by doing another linear interpolation between the new points E and F based upon the camera's Z coordinate. If you add the offset you want to that point's Y coordinate, you're done.

There was another discussion a while ago which talks about linear, bilinear and cubic interpolation for use in landscape generation, which more or less relates to the method I'm trying to describe. You can read it here.

Neil Carter
Nether - Mac games and comic art
Quote this message in a reply
Post Reply