Wednesday, November 13, 2019

2d - Out of screen position to inner screen position


I don't know how I could solve this. I'm trying to accomplish this:


So if a object is out of my screen I would like to calculate the position it would be inside of the screen. Searched already but did not found any help for this problem. You can see this in games like cod where it shows you from which direction you got hit by a bullet.



Thanks in advance.



Answer



The easiest would be to clamp the x and y to within range. That is, something like


apparentX = max(screenXMin, min(thingX, screenXMax));
apparentY = max(screenYMin, min(thingY, screenYMax));

This would let you see exactly what height or position it is if directly left, right, up, or down from the screen. Anything off in a diagonal would show up in a corner here.


Alternatively, consider a line segment from screen center to thingPosition. See if it intersects any of the four edges of your screen. (If it doesn't intersect one of them, it's already visible.)


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...