Raycast problem - placing items on a mesh

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
Dukajuke
Posts: 2
Joined: Thu Jan 19, 2012 3:45 pm

Raycast problem - placing items on a mesh

Post by Dukajuke »

Hi All,

I'm currently integrating BEPU into my Sunburn project and I'm havng some difficultes with finding the height at a given point on a terrain. What I'm trying to do is place items (models) on my terrain mesh by first suspending them above the terrain, and the using BEPU raycast to get the distance from my model's collision box to a point on my terrain. I am then subtracting that distance from the Y value for my model's vector. I've tried a balizzion combinations, but I can't seem to find the right formula for getting the correct distance. Here is my current function for getting the distance from my model to the terrain. It returns a T value but it's never correct. Does anyone have any suggestions? Thanks in advance!

public float GetDistanceToSupport(ISpaceObject spaceObj, Vector3 location, float BoxHeight)
{
Vector3 rayOriginOffset = new Vector3(0, -BoxHeight / 2, 0);
RayCastResult RayResult;
Ray testray = new Ray();
testray.Direction = Vector3.Down;
testray.Position = new Vector3(location.X, location.Y, location.Z) + rayOriginOffset;

//Fire a ray at the candidate and determine some details!
spaceObj.Space.RayCast(testray, out RayResult);
return RayResult.HitData.T;
}

I then set the object's position here: bbox.Position = new Vector3(bbox.Position.X, bbox.Position.Y - SupportDistance, bbox.Position.Z);
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Raycast problem - placing items on a mesh

Post by Norbo »

What T value is it giving you? Is it actually hitting the Terrain, or is it hitting something else first? If the goal is only to test the terrain for height, you could use the Terrain.RayCast method directly and avoid hitting anything else.

If you do want to include other objects, you could still exclude certain objects by using a filter delegate passed into the Space.RayCast method.

Another thing to watch out for is the collision shapes not matching the graphics. Tossing the BEPUphysicsDrawer from the main source download in for debug visualization can test for this.
Post Reply