Game Development Community

UnProject behind viewpoint

by Phil Carlisle · in Torque Game Engine Advanced · 07/30/2006 (12:03 pm) · 9 replies

I'm looking at making some GUI controls that display the positions of objects in a hud.

Now I'm messing around with it and I notice that objects behind the viewpoint (basically in the back half-space from your point of view) dont get unprojected properly if you use unProject.

So I'm wondering. Is there a method that will handle it?

I've tried projecting the objects position into view space and negating the Z, but that still has its own issues.

I really need to project a direction vector to the edge of the screen when the object is either off screen or behind me.

Just wondering if my maths is wrong somewhere.

I'm multiplying the world space direction vector by the worldtoObject space matrix (with the position set at 0,0,0) and that in theory is giving me screen space position with either +ve or -ve Z values.

I then 0 the z and normalize the vector. I then multiply half the screen width * 2 and then clip to the bounds rectangle of the gui control.

I guess what I'm actually getting, is a compressed half-circle rather than a real direction.

#1
07/30/2006 (4:59 pm)
Not sure if i understand you correct phil, but
also, not an expert :)

However, to find the distance between 2 vector points "a" & "b" , don't we say:

(v)[0] = (a)[0] - (b)[0], (v)[1] = (a)[1] - (b)[1], (v)[2] = (a)[2] - (b)[2]

then distance = sqrt (v[0] * v[0] + v[1] * v[1] + v[2] * v[2])

or am I completely off field here?

Hewster
#2
07/30/2006 (5:17 pm)
Yep :)

This is projecting the points onto a plane..
#3
07/31/2006 (12:43 pm)
Have you tried just doing a dot product to test if the object is in front of, or behind camera? If the object is small enough, that might be enough to determine that it's not visible and does not need an unProject.
#4
07/31/2006 (1:34 pm)
The problem is, I want to display objects behind me. But as a direction indicator on the edge of the screen.

So basically things fall into 3 categories:

1) Objects in the view.. these are projected onto the screen and thier distance etc is displayed
2) Objects in front of me but not in the view - these are projected into screen space and then clipped into the screen bounds and a direction arrow is drawn for each one (basically, the direction to look in to see the object)

3) Objects behind me - what I need, is to do the same as 2 above. Display a direction arrow. Unfortunately for some reason I'm getting completely the wrong direction, so that I get weird flips and stuff when the indicator arrows are drawn.

I'm likely doing something stupid here though :)
#5
08/01/2006 (5:42 am)
Maybe you can "convert" the position of the objects behind the camera to front of the camera before projecting to 2D.
#6
08/01/2006 (6:12 am)
Yeah, I tried that. But it didnt seem to feel right.

Apparently the project function still works even if the object is behind the camera anyway.

I think its maybe the way that objects behind the camera but within the view are projected onto the edge of the screen. What you really need, is a direction vector from the current view in the direction to look at in order to see the object. So really its sort of a spherical direction vector. What the projection does I think, is warp that vector in a way that doesnt quite work.
#7
08/02/2006 (8:26 pm)
Phil, this is for a flight sim?

It makes it a lot harder because of roll. I thought about this exact problem when I was thinking about how to display incomming missiles on this one thing I was playing around with (never got around to finishing the hud, or much else for that matter)

If you take the vector from you to the object, and can somehow re-express that as a component of your x-axis and z-axis vectors (ignore y-axis, since distance behind you doesn't matter.... or at least I don't think it should matter), you should be able to do arctan (or something like that) and find the direction, right?

No projection needed then...

Now the tricky thing is which matrix transforms get you there. Maybe you're better at that than me, I'd have to play with it for a while to get it working.
#8
08/02/2006 (8:48 pm)
Unit vectors represented by the 3 axis of your plane's position vector in world space, and not its transformation matrix as a whole, use those for the 2 or 3 dot product checks, depending if you track above/below or not, keep the current results from the previous frame and displace the 2d GUI direction finders in a set list of positions according to frame by frame variations of the various dot products checks : by using the position vector only, you won't get weird directional finders jumps due to roll and pitch change, only their effect on position will be influential.
#9
08/04/2006 (2:40 am)
Not following you there Nic.

If its an arrow direction relative to the screen plane, then surely the vectors need to be in object space and not world space.

I guess I agree with you about keeping a list of previous positions and not allowing them to jump, but surely if they were projected correctly, it wouldnt jump anyway.

Its jumping because the projection code is buggy somewhere.

This is for a direction arrow to indicate the direction to move the center of your view in order to align it with an enemy. What I really need, is to have the direction vector formed on the sphere between the current view center and the direction formed between the player and the enemy as it hits the sphere. That would give me a spherical direction. I'd then need to project that into the view plane in 2D and then clip to the view rectangle.

Sadly, I'm not sure how to do it :)