dannypb
04-11-2012 02:10:26
hi ![Smile :)]({SMILIES_PATH}/icon_smile.gif)
Still working on my space shooter. I'm trying to get the nearest object in front of the ship I'm flying, and I'm having all kinds of frustration. I'm am hoping that someone can help.
1. The world is made up of a number of entities, such as the planet, space stations, jumpgates, ect. All of these entities are set up as static geometry.
2. There are also other player ships in the sector.
3. The code to check for the nearest object in front is called from the frame listener.
4. And here is the function that checks for the nearest entity hit.
The only hits I get on it are when I face the middle of the planet. If I line up on a ship or station or a player, I get no hits. Has anyone dealt with this and figured it out?
Thanks in advance, even if you couldn't help.
Dan
![Smile :)]({SMILIES_PATH}/icon_smile.gif)
Still working on my space shooter. I'm trying to get the nearest object in front of the ship I'm flying, and I'm having all kinds of frustration. I'm am hoping that someone can help.
1. The world is made up of a number of entities, such as the planet, space stations, jumpgates, ect. All of these entities are set up as static geometry.
2. There are also other player ships in the sector.
3. The code to check for the nearest object in front is called from the frame listener.
Mogre.Vector3 tempVector = new Mogre.Vector3(0, 0, -1000);
gCamNode.Translate(tempVector, Mogre.Node.TransformSpace.TS_LOCAL);
Mogre.Vector3 chkVector = gCamNode.Position;
tempVector = new Mogre.Vector3(0, 0, 1000);
gCamNode.Translate(tempVector, Mogre.Node.TransformSpace.TS_LOCAL);
frontDistance = prRayCast(ref frontTarget, gCamNode.Position, chkVector);
4. And here is the function that checks for the nearest entity hit.
protected float prRayCast(ref string obName, Mogre.Vector3 origPos, Mogre.Vector3 normPos)
{
Mogre.Vector3 rayHitPoint;
Single rayDistance = 0.0f;
Single lowDistance = 0.0f;
string lowName = "";
String rayHitPointName = "";
Mogre.Vector3 rayDir = normPos - origPos;
rayDir.Normalise();
Ray myRay = new Ray(origPos, rayDir);
//tarNode.Position = normPos;
RaySceneQuery raySQuery = gSceneMgr.CreateRayQuery(myRay);
RaySceneQueryResult raySQResult = raySQuery.Execute();
RaySceneQueryResultEntry raySQREntry;
for (Int16 i = 0; i < raySQResult.Count; i++) // for all hits
{
raySQREntry = raySQResult;
rayDistance = raySQREntry.distance;
rayHitPoint = myRay.Origin + (myRay.Direction * rayDistance); // calculate hit point
rayHitPointName = "";
// save names of objects (or world fragment) that was hit
if (raySQREntry.worldFragment != null)
rayHitPointName = raySQREntry.worldFragment.ToString();
if (raySQREntry.movable != null)
rayHitPointName = raySQREntry.movable.Name;
// Also bounding boxes of terrains can be hit.
// Skip them to prevent hits on strange places ("just in the air").
// There is no terrain in this case however
if (rayHitPointName.StartsWith("tile[")) // e.g. "tile[1][1,1]"
continue;
if (!rayHitPointName.ToLower().Equals("camera"))
{
if (lowDistance == 0.0f)
lowDistance = rayDistance;
if (raySQREntry.worldFragment != null)
{
if (rayDistance < lowDistance)
{
lowDistance = rayDistance;
lowName = rayHitPointName.Substring(0,5);
}
}
if (raySQREntry.movable != null)
{
if (rayDistance < lowDistance)
{
lowDistance = rayDistance;
lowName = rayHitPointName.Substring(0, 5);
}
}
}
}
if (raySQResult.IsEmpty)
{
rayHitPoint = Mogre.Vector3.ZERO;
lowDistance = 0.0f;
lowName = "Empty";
}
gSceneMgr.DestroyQuery(raySQuery);
raySQuery.Dispose();
raySQuery = null;
obName = lowName;
return lowDistance;
}
The only hits I get on it are when I face the middle of the planet. If I line up on a ship or station or a player, I get no hits. Has anyone dealt with this and figured it out?
Thanks in advance, even if you couldn't help.
Dan