Sign in to follow this  

"Constraining" a vector on-screen

Recommended Posts

Xavura    100
Sorry for the awkward title, I have no idea how to word this question.

Let's say the screen is 1024x768, my camera is centered over an entity (the player). There are numerous other entities, arbitrarily positioned, all off-screen.

Imagine lines drew from the player/center of the camera, to each entity. For each of these lines, at the point where it crosses the camera AABB (that is, at the point where the line leaves the screen) I want to get that point (so I can draw some kind of marker).

I would like the markers to be e.g.: 10px from the top of the screen if the entity is above; 10 pixels from the right if the entity is to the right; etc.

I've tried a couple things but nothing has really worked.

First I tried working out the angles from the center of a 1024x768 rectangle, to each corner. I figured that I could check the angle from the player to each planet, and compare it to these angles to see which "quadrant" it lays in. By quadrant, I mean... say if I drew lines from the player in the direction of each corner... but made these lines infinitely long, they would slice up the screen into triangular quadrants.

So for each planet, I subtract the player/entity position to get the distance then normalize that to get the direction, then multiply either i or j by [around] half of the camera width/height depending on which "quadrant" it lays in. It was something to that effect, anyway.

That didn't really work, I may have been doing something wrong but that (and other things I've tried) seems really clumsy. Surely there's a nice way to achieve this?

Oh and, keep in mind that the size of the screen can change.

Share this post

Link to post
Share on other sites
haegarr    7372
One way would be this: Construct a bounding volume with planes parallel to the view volume but replaced somewhat to the inside (in other words: a shrunk version of the view volume). Compute a ray from the player (as the origin) to the target entity. Compute where the ray hits the bounding volume. Map this point to the screen by projection, as usual. Do al the former computations in either world space or camera space. A feature of this way is that screen resolution has no effect on the relative position of the marker w.r.t. the screen borders. E.g. the marker appears ever, say, 5% of the screen size away from the borders, independent how many pixels are used for display. (Other ways with small variations of the above exists, too.)

Another way is to construct a bounding box in screen co-ordinates (i.e. in 2D). Compute the ray as above, but map the ray to screen co-ordinates before computing the hit point. A feature of this way is that the hit point will ever be a constant amount of pixels away from the screen borders. This does not mean "resolution independence", and may be a feature you don't want to have.

You will see the difference if the rendered representation of the targeted object approaches the hit point. The 1st way above will work smooth, wile the 2nd way above will show a small jerk in some situations.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this