Game Development Community

FindObject on client

by Derk Adams · in Torque Game Engine · 07/27/2005 (3:26 pm) · 4 replies

Greetings,

I have two GUIs that use the terrain to generate their output (a map and an altimeter). I need to find the terrain object and use the following code in the onRender routine:
// Must have a connection and player control object
   GameConnection* conn = GameConnection::getServerConnection();
   if (!conn)
      return;
   ShapeBase* control = conn->getControlObject();
   if (!control) return;

   Point3F shapePos;
   shapePos = control->getPosition();

    F32 altitude;
    altitude = shapePos.z;
    Point3F terrPos = shapePos;
    TerrainBlock * terrain = dynamic_cast<TerrainBlock*>(Sim::findObject("Terrain"));
    if(terrain != NULL) {
      terrain->getWorldTransform().mulP(terrPos);
      terrPos.convolveInverse(terrain->getScale());
      F32 height;
      if (terrain->getHeight(Point2F(terrPos.x, terrPos.y), &height) == true) {
        terrPos.z = height;
        terrPos.convolve(terrain->getScale());
        terrain->getTransform().mulP(terrPos);
      }
      altitude = shapePos.z - terrPos.z;
      Con::printf("Altitude %f %f %f",shapePos.z,terrPos.z,altitude);
   }
This works fine on the server and the client hosting the server, but separated clients get NULL for the terrain object.

How do I find the ghosted terrain to find the height?

Thanks.

#1
07/27/2005 (4:27 pm)
Lookup the waterblock code. it looks up the terrian object for the client.
#2
07/27/2005 (5:01 pm)
For an altimeter, do you really want the distance from the ground, or the distance from sea level?

One would think that sea level is more appropriate. If you have a flight ceiling in use, sea level is critical so you can tell how close you are flying to it. If you aren't flying, then your altitude is always 0 anyway. If you are "jumping" (noticing your project is mecha), sea level is still appropriate since your altitude probably won't exceed 50m at the most anyway.

Just a few thoughts.
#3
07/27/2005 (5:13 pm)
The simpler thing might be to cast a ray down using Waterblock and Terrain as the typemask so that it'll tell you the position of the first water/terrain it hits. Then your altimeter won't have to do any fancy shenanigans - just measure distance between the point of collision and the position of the object in question.
#4
07/28/2005 (9:09 am)
Everyone,

Thanks for the help.

Michael, the waterblock code didn't have what I was looking for (or atleast I couldn't transfer the code from an object to a gui).

Bryce, actually it is an AGL (above ground level) display. Thanks for doing a little homework before answering (about my project). I have a jump cap at 60m above ground. When going over mountains, thas can end up with an altitude of a couple hundred meters.

Ben, while that would work for the altimeter, the map is still an issue, but I found the solution.

TerrainBlock * mTerrain = NULL;
// Go through all ghosted objects on connection in order to find the terrain
for (SimSetIterator itr(conn); *itr && !mTerrain; ++itr)
   if ((*itr)->getType() & TerrainObjectType ) mTerrain = static_cast<TerrainBlock*>(*itr);

It was in the map resource and works great for the AGL, but there is still a bug in it when drawing the map, but that will be an easier fix.

Thanks.