Game Development Community

Show enemy off screen

by Andy Hawkins · in Torque Game Engine · 05/06/2008 (10:01 pm) · 5 replies

When I have a target I want the GUI to show where I need to turn to bring the enemy on screen. Does anybody know how to do this?

#1
05/07/2008 (1:28 am)
Hey Andy,

You should add this to your Target Reticule code.
When you see the following:
if (!parent->project(shapePos, &projPnt))
   continue;

Continue with the draw method (Mine is drawTarget(Point3F offset, F32 opacity, F32 Size))
So remove the if Statement and just have it as:
parent->project(shapePos, &projPnt);

Now when the Object is offscreen you draw the Offscreen bitmap. Here's a snippet of my code:
Point2I GuiExtent = getExtent();

	if (offset.x < 0 || offset.y  < 0 || offset.x > GuiExtent.x || offset.y  > GuiExtent.y)
	{

		//Check if texture Exists before we do all the calculations
      if(!mTextureOffscreenObject)
         return;

		if (offset.x < 0)
			offset.x = 0;

		if (offset.y < 0)
			offset.y = 0;

		if (offset.x > GuiExtent.x)
			offset.x =  GuiExtent.x - mTextureOffscreenObject->getWidth() ;

		if (offset.y > GuiExtent.y)
			offset.y = GuiExtent.y - mTextureOffscreenObject->getHeight();
	
		GFX->drawBitmap(mTextureOffscreenObject,Point2I(offset.x,offset.y));

	}
	else
	{
.... (Draw Normal onscreen Target Reticlue)

Hope that helps. If not drop me a mail and I can send you my TargetReticule Code.
#2
05/07/2008 (3:06 am)
I don't want to burst anyone's bubble, but the above will stop working as soon as the target gets behind you.
It works somewhat if the target is infront of you but not in your view, but after that you'll get jumping target indicators and project () will return garbage.

UnProject behind viewpoint

The correct way to do it is to draw a line out of the center of the screen, on shapeDir (check guiShapeNameHud) and intersect it with the screen bounds.

If you doubt this is correct, use drawLine () to draw a line from center to target, and you'll see that the projected coordinates earlier are bad. I went trough a painful week with this, just hoping it will help.
#3
05/07/2008 (5:12 am)
Yeah... I have noticed this, but it's a start I guess.

Interesting link, but no real solution (or code), do you have any working code? Cause I wouldnt know how to do that.
#4
05/07/2008 (5:52 am)
The solution is:

* Get your forward vector.
* Get the shape direction in which you have to face to see your target.
* Get the difference between these two, and normalize.
* Use this difference to get a new position (in 3D space, still) and project it onto your screen (to 2D).
* Project the new position, and the center of your screen. When they are projected, calculate the difference and normalize.
* Now, use this difference to offset the center coords, until they go out of your bounds. When they are out of bounds, do a line intersection test (for code, check google.. there are some free ones) from the center and find the point where your line intersects with the bounds line on the side you want to test against.
* Then pretty much just clip it into your screen, like you've done in your code snippit.

I found it helpful to use drawLine () between your offscreen position, and your center. It behaves correctly, and you can see where your indicator should turn up. When they line up, you know you've done the right thing in the code.
#5
05/07/2008 (8:32 am)
Great - thanks guys. This will really help.