3D Cartesian coordinates to 2D screen space coordinates

Malonn

New member
Joined
May 7, 2016
Messages
2
I'm trying to map cartesian coordinates to a 2D screen. But it's for a game, and I only have limited resources to find maths related to the screen. I've googled for help, but most require a camera position, and there is no function to determine that in the game. I can return the camera's X, Y and Z world rotation anges. I can return an angle between the camera and a certain object. Using only thjose two things, what would be the formula to map a 3D position to a 2D screen?

I've found and used a simple formula:

screenX = cartX + screen_width/2
screenY = screen_height/2 - cartY

But that dopesn't factor the camera into the equation. But all I can return is what I've stated above. Please help.

Thanks.
 
I'm trying to map cartesian coordinates to a 2D screen. But it's for a game, and I only have limited resources to find maths related to the screen. I've googled for help, but most require a camera position, and there is no function to determine that in the game. I can return the camera's X, Y and Z world rotation anges. I can return an angle between the camera and a certain object. Using only thjose two things, what would be the formula to map a 3D position to a 2D screen?

I've found and used a simple formula:

screenX = cartX + screen_width/2
screenY = screen_height/2 - cartY

But that dopesn't factor the camera into the equation. But all I can return is what I've stated above. Please help.

Thanks.
You might look at
https://en.wikipedia.org/wiki/3D_projection

What kind of projection do you want? What you have appears to be an orthographic projection parallel to the z axis with both scale factors equal to one (and a reversed y).
 
Okay, after a little digging, I found a formula that *sort of* works. It's accurate, but a little loose. Here's the formulas for the X and Y screen coordinates:

X coordinate:

Code:
Screen Position X = (( Screen Resolution Width / 2 ) + ( <Angle between Player and NPC> / <x> ) * ( Screen Resolution Width / 2 )) - ( Screen Resolution Width / 2 )

<x> = Your Filed of View if <Angle between Player and NPC> is positive, else it equals 1

Y Coordinate:

Screen Position Y = (( Screen Resolution Height / 2 ) + ( <Vertical Angle> / <x> ) * ( Screen Resolution Height / 2 )) - ( Screen Resolution Height / 2 )

<Vertical Angle> = The angle of <Distance between the player and the NPC> and <The players Z position subtracted from the NPC's Z position plus 30>
Take those calculations and subtract 90 and then subtract the player's X angle
<x> = 60 if the result of the <verticle angle> calculation is positive and 1 if the result is negative.

That formula works pretty well, but it can be loose. By "loose" I mean the resulting X and Y coordinates for the screen kind of jump around from the NPC's position a little. I'd like them to be dead-locked on to the NPC position.

Can any of you math geniuses find a way to refine the formula to make it more accurate??

Thanks.
 
Top