Jump to content
• Advertisement

# "Constraining" a vector on-screen

This topic is 2803 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

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

##### Share on other sites
Advertisement
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

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
21
3. 3
JoeJ
18
4. 4
5. 5
• Advertisement

• 14
• 39
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631717
• Total Posts
3001877
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!