Game Development Community

dev|Pro Game Development Curriculum

guiRangeFinderHud

by Tim Dix (Raverix) · 06/01/2009 (8:44 pm) · 27 comments

static.garagegames.com/static/upload/blogs/17407.pngThis code came about from this post, so thank you deepscratch for getting this started, and Ron Nelson for the original snippet.

I chose to simplify and do a very basic GUI based off guiTextCtrl. If you're looking for something a bit more flashy, it should be really easy to adapt this to your needs. I tried to make it well commented so that you can follow along.

Instructions: Just save the following file to your source folder, compile, and then place the GUI using the GUI Editor.

T3D/fps/guiRangeFinderHud.cpp
#include "gui/controls/guiTextCtrl.h"

#include "console/consoleTypes.h"
#include "console/console.h"
#include "T3D/gameConnection.h"
#include "T3D/gamebase.h"

class GuiRangeFinderHud : public GuiTextCtrl
{
   typedef GuiTextCtrl Parent;

	F32	mMaxRange;     // Max Range for our finder

public:
	GuiRangeFinderHud();

	void onRender( Point2I, const RectI &);
	static void initPersistFields();
	DECLARE_CONOBJECT( GuiRangeFinderHud );
};


//-----------------------------------------------------------------------------

IMPLEMENT_CONOBJECT( GuiRangeFinderHud );

GuiRangeFinderHud::GuiRangeFinderHud()
{
	mMaxRange = 1000;	//Max range we search for a collision
}

void GuiRangeFinderHud::initPersistFields()
{
	Parent::initPersistFields();

	addGroup("RangeFinder");
	addField("maxRange",	TypeF32,		Offset(mMaxRange,		GuiRangeFinderHud));
	endGroup("RangeFinder");
}

void GuiRangeFinderHud::onRender(Point2I offset, const RectI &updateRect)
{
  // Must have a connection and game base control object
   GameConnection* conn = GameConnection::getConnectionToServer();
   if (!conn)
      return;

   GameBase* control = dynamic_cast<GameBase*>(conn->getControlObject());
   if (!control)
      return;

   MatrixF cam;
   Point3F dir;

   Point3F startPos;
   Point3F endPos;

   // Get the camera's tranform,
   conn->getControlCameraTransform(0,&cam);
   cam.getColumn(3, &startPos); //Camera Position
   cam.getColumn(1, &dir);		//Camera forward vector
   
   //Calculate the point mMaxRange in front of camera.
   endPos = startPos + dir * mMaxRange;

   //Store the state of the collision
   bool collisionEnabled = control->isCollisionEnabled();

   //Disable collision so we don't collide with ourself.
   if(collisionEnabled)
      control->disableCollision();

   //These are the items that we will consider to collide with
   static U32 mask = TerrainObjectType |
			   InteriorObjectType |
			   WaterObjectType |
			   StaticShapeObjectType |
			   StaticTSObjectType |
			   PlayerObjectType |
			   ItemObjectType |
			   VehicleObjectType;

	//Get out container, copied from camera.cpp, I think I need it :p
	RayInfo collision;
	Container* pContainer = control->isServerObject() ? &gServerContainer : &gClientContainer;

	//Cast our ray and see what we hit
	if (pContainer->castRay(startPos, endPos, mask, &collision))
	{
		//If our ray hits, set our text
		char text[32];
		dSprintf(text, 32,"%.0f", (collision.point - startPos).len());
		setText(text);
	}
	else 
	{
		//Otherwise, set the range to maxRange+, ie: 1000+
		char text[32];
		dSprintf(text, 32,"%.0f+", mMaxRange);
		setText(text);
	}

	//Enable collision if we need to
	if(collisionEnabled)
		control->enableCollision();

	//Let the parent take care of rendering
	Parent::onRender(offset,updateRect);
}


This was coded for T3D, but it should be extremely easy to port it back to any of the engines. The only thing I think that would be needed to change would be the headers.

Let me know if you've got any problems and I'll do my best to help.
tp.cantanogames.com/counter.php?guiRangeFinderHud
Page«First 1 2 Next»
#21
06/03/2009 (3:49 pm)
@Ron - I've updated the top of the resource to cite you as the original source of that snippet. I'm more than happy to give credit where credit is due. When I cited the source of where I got the snippet, I thought the poster of the code wrote it.

The part I took very seriously was the accusation that I took code and passed it off as my own when I didn't, so I'm glad that we cleared that up.
#22
06/03/2009 (4:19 pm)
Sounds good to me. Thanks.
#23
08/23/2009 (3:40 pm)
I'm having issues saving the range finder on the HUD. I add the guiRangeFinderHud in the GUI editor and then save the GUI but every time I load the game again its gone.
#24
05/20/2010 (12:19 pm)
This works great for Tge 1.5.2.....

To port this just change this

#include "gui/controls/guiTextCtrl.h"

#include "console/consoleTypes.h"
#include "console/console.h"
#include "T3D/gameConnection.h"
#include "T3D/gamebase.h"

To

#include "gui/controls/guiTextCtrl.h"

#include "console/consoleTypes.h"
#include "console/console.h"
#include "game/gameConnection.h"
#include "game/gamebase.h"

Then save the file as guiRangeFinderHud.cc in engine/game/fps. Add to project and recompile. Start game, press F10, and add your new guiRangeFinderHud to the game and save.........Happy RangeFInding

#25
05/20/2010 (1:21 pm)
For those who might wanna change the font color, in your playgui.gui

new GuiRangeFinderHud(RangeFinder) {
      canSaveDynamicFields = "0";
      Profile = "RFPrintProfile";
      HorizSizing = "right";
      VertSizing = "bottom";
      Position = "526 332";
      Extent = "50 50";
      MinExtent = "8 2";
      canSave = "1";
      Visible = "1";
      hovertime = "1000";
      text = "19";
      maxLength = "1024";
      maxRange = "1000";
   };

Profile = what every you wanna name it, just as long as its the name thats in your defaultGameProfiles.cs file. Here's mine for example;

// RangeFinder Number Color
new GuiControlProfile ("RFPrintProfile")
{
   fontType = "Ellis";
   fontSize = 26;
   opaque = true;
   fillColor = "128 128 128 150";
   fontColor = "0 255 0 150"; //Green Numbers 
   border = true;
   borderColor = "0 255 0 150";
};

Now I have Green RangeFinder Numbers.....
#26
05/25/2011 (12:12 am)
Note for 1.1 Preview usage, and presumably Final as well:
Includes for gameConnection.h and gameBase.h should reflect a move to the gameBase directory.
#include "T3D/gameConnection.h"  
#include "T3D/gamebase.h"
becomes:
#include "T3D/gameBase/gameConnection.h"  
#include "T3D/gameBase/gamebase.h"
And the container query (line 85):
Container* pContainer = control->isServerObject() ? &gServerContainer : &gClientContainer;
becomes:
SceneContainer* pContainer = control->isServerObject() ? &gServerContainer : &gClientContainer;
#27
01/26/2014 (8:40 pm)
one more fix to t3d 3.5.....

change this
//These are the items that we will consider to collide with
   static U32 mask = TerrainObjectType |
			   InteriorObjectType |
			   WaterObjectType |
			   StaticShapeObjectType |
			   StaticTSObjectType |
			   PlayerObjectType |
			   ItemObjectType |
			   VehicleObjectType;

to this
//These are the items that we will consider to collide with
   static U32 mask = TerrainObjectType |
			   WaterObjectType |
			   StaticShapeObjectType |
			   PlayerObjectType |
			   ItemObjectType |
			   VehicleObjectType;
Page«First 1 2 Next»