Raycast problem - placing items on a mesh
Posted: Thu Jan 19, 2012 4:04 pm
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);
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);