Raycasts and Terrain

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
Jimmy
Posts: 4
Joined: Fri Jun 20, 2008 10:30 am

Raycasts and Terrain

Post by Jimmy »

Hi

I am trying to use raycasts to check if terrain is between my camera and my player (or the camera went thru the terrain ect.), and from what I can tell your Toolbox.raycast can only test against entities. It gives me an error saying terrain cannot be converted to entity when I try to raycast. Is there any easy way to do this that I am missing? Thanks for they physics engine btw, it has been great so far.

-Jimmy
User avatar
Zukarakox
Not a Site Admin
Posts: 426
Joined: Mon Jul 10, 2006 4:28 am

Re: Raycasts and Terrain

Post by Zukarakox »

You can either use the terrain itself and do terrain.raycast, or space.raycast, but that will test agaisnt everything inside space, so you might have to sort through your results.

It will return entities -- but your terrain is presumably made up of a bunch of triangles from StaticTriangleGroup -- so goodluck sir.
i has multiple toes
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Raycasts and Terrain

Post by Norbo »

Just a quick addendum to what Zukarakox said- if you try to access the entities returned by raycasts against a Terrain (or StaticTriangleGroup for that matter), they will be null. This is because the terrain doesn't necessarily have triangle entities at every point on the terrain at all times, instead creating entities on demand.
User avatar
Zukarakox
Not a Site Admin
Posts: 426
Joined: Mon Jul 10, 2006 4:28 am

Re: Raycasts and Terrain

Post by Zukarakox »

Can I ever get it right ?: (
i has multiple toes
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Raycasts and Terrain

Post by Norbo »

Well what you said was basically right, I just figured the null thing could be confusing for someone who didn't already know about it :D
Jimmy
Posts: 4
Joined: Fri Jun 20, 2008 10:30 am

Re: Raycasts and Terrain

Post by Jimmy »

Duh... thanks a lot, I will give the terrain.raycast a try.
Jimmy
Posts: 4
Joined: Fri Jun 20, 2008 10:30 am

Re: Raycasts and Terrain

Post by Jimmy »

So I tried using the terrain.raycast and it works most of the time, but I can get the camera to go thru the terrain and once it is thru it wont detect hits anymore. The other thing is every once an a while it will throw an index out of range exception in the terrain.raycast. I suspect it is something to do with the way I am setting up the parameters for the raycast, but I cant see what I am doing wrong. Also I am not getting what time of impact is used for, why use it when the raycast gives you the hit location?

Code: Select all

                world.CollectRayCastTerrainForCamera(cameraPosition, player.Position, ref terrainRaycastList);

                //get the raycast origin, direction and distance
                rayCastOrigin = cameraTarget;

                Vector3.Subtract(ref cameraPosition, ref rayCastOrigin, out rayCastDirection);
                Vector3.Normalize(ref rayCastDirection, out rayCastDirection);

                Vector3.Distance(ref rayCastOrigin, ref cameraPosition, out rayCastDistance);

                //cast the rays to check if the terrain is in the way
                foreach (Terrain t in terrainRaycastList)
                {
                    t.rayCast(rayCastOrigin, rayCastDirection, rayCastDistance, out notUsedHitEntity, out rayCastHitLocation,
                                  out notUsedHitNormal, out notUsedTimeOfImpact);

                    //if the hit location is closer to the camera target than the current camera position move to that location
                    if (Vector3.Distance(cameraTarget, rayCastHitLocation) < Vector3.Distance(cameraTarget, cameraPosition))
                    {
                        cameraPosition = rayCastHitLocation;

                        //reset the camera physics
                        velocity = Vector3.Zero;
                        acceleration = Vector3.Zero;
                    }
Thanks
-Jimmy
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Raycasts and Terrain

Post by Norbo »

The camera going through the terrain is due to the triangles being very thin. Due to just general imprecision, it would be possible to push your way through. The solution to this is to not move the camera all the way up to the contact point, but instead give it a little room. That way, it will at least stay on one side of the terrain.

For the exception problem, it seems to be related to the terrain.raycast that returns only one hit and the way it indexes into the terrain geometry near the edges. There's an index in there that differs from the multi-hit version; it will stop happening if you use the multi-hit version. A fix for this will be in the next version. There's also a minor issue of the ray-triangle intersection sometimes missing between two adjacent triangles- I'll see if I can improve that behavior as well.

The time of impact is just a convenience that comes with the calculation. Sometimes it's easier to deal with the time than a location.
Jimmy
Posts: 4
Joined: Fri Jun 20, 2008 10:30 am

Re: Raycasts and Terrain

Post by Jimmy »

Didnt see your edit, ill try using the multi hit version.

Thanks for your help

The exception happens when the camera is in constant collision with the terrain (using the raycasts) and then i move the player around for about 10-15 sec

System.IndexOutOfRangeException was unhandled
Message="Index was outside the bounds of the array."
Source="BEPUphysics"
StackTrace:
at BEPUphysics.Terrain.rayCast(Vector3 origin, Vector3 direction, Single maximumLength, Entity& hitEntity, Vector3& hitLocation, Vector3& hitNormal, Single& toi)
at ShatteredScrolls.Camera.CalculateNextPosition(Single timeDelta) in C:\ShatteredScrolls\ShatteredScrolls_V0.2\ShatteredScrolls_V0.2\GameObjects\Camera.cs:line 459
at ShatteredScrolls.Camera.Update(GameTime gameTime) in C:\ShatteredScrolls\ShatteredScrolls_V0.2\ShatteredScrolls_V0.2\GameObjects\Camera.cs:line 405
at Microsoft.Xna.Framework.Game.Update(GameTime gameTime)
at ShatteredScrolls.ShatteredScrolls.Update(GameTime gameTime) in C:\ShatteredScrolls\ShatteredScrolls_V0.2\ShatteredScrolls_V0.2\ShatteredScrolls.cs:line 270
at Microsoft.Xna.Framework.Game.Tick()
.....
Post Reply