Advertisement Jump to content
Sign in to follow this  

How do I get a 3D position from a Camera Vector

This topic is 1022 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

Ok... I am quite the novice when it comes to game related math, especially in the 3D realm.  So I shall try to convey my problem as clearly as possible, since I do not know much of the terminology.


I have a Camera in 3D space, this camera can be located at any x,y,z and it can face any direction valid for a FPS view.

I have a target, the target can be at any location in 3D space along the x,y,z however the target will be STATIC and non movable.


I would like to be able to have my camera position itself in front of the target so that the target will appear a certain size and location on the 2D render screen.


I found online a function, when when supplied with variables of x,y (location of target to be displayed on 2D screen) and height (the height the target should display on the 2D screen) the function will return a Vector3 stating where the target should be placed in 3D space, so that it will render at the x,y at desired height on the 2D render screen.


The problem I am having is converting that Vector3 into something that I can use to position my Camera (since my target is stable) in the correct position to get the same result.


This is what I am doing so far... the code is in Lua


-- This line is calling the function I found online, I am requesting a Vector3 position so the target will be shown on the 2D render at position screenW/2, screenH/2 (so that should be in center of screen) and I want the object to appear to be .5 the size of my screen's height so thats the second screenH/2.  The last argument is the unit size of the object (2 units high)


local pos = ScreenSpace.ScreenToWorldByHeight(screenW/2,screenH/2,screenH/2, 2)


Now the result is ... pos = (0,0,-2)   -- Meaning i need to have the target positioned -2 away from my camera on the Z axis to display as needed.


This is the same result no matter which way my camera is facing, so I know the result is based off of my camera's .. facing vector?


So I tried this to get my new camera position...


newCamPos = TargetPosition - pos


This however, causes my camera to be positioned at the targets location offset by +2 on the Z axis, when in reality, I need it to be +2 on the Z axis in relation to how the Target is facing, the targets facing vector.


So I am lost... is there an easier way to do this?  What terms should I search for?  Or if you have any tips or tricks to share that would be great.





Share this post

Link to post
Share on other sites

Not sure if it will be needed by anyone, but after experimenting I did find a solution.


--first i set up some vectors

local g_up =,1,0)  --global up

local v_forward = target.lookVector

local v_right = -g_up:Cross(v_forward)

local v_up = -v_forward:Cross(v_right)

local p = target.position


--get a new position, based off of moving down the vectors (also the 'loc' variable is what was returned by the function in the above post)

p = (v_forward*(-loc.Z))+p

p = (v_right*loc.X)+p

p = (v_up*loc.Y)+p


local cam = Camera

cam.position = p

cam.lookAt = p+(target.lookVector * -2)) 


I dont know if its the best way to do this, but it works, so... unless it gives me issues, ill use this



Share this post

Link to post
Share on other sites

Getting your camera's position is reasonable easy, I am a little confused as to why you are doing the cross product stuff at the start, isn't target.lookVector already exactly what you need?


// I am assuming both target.lookVector and cam.lookAt are unit vectors

cam.position = target.position+target.lookVector*distance

cam.lookAt = -target.lookVector


The tricky bit will be working out distance, I don't see your code for that, is that what 'loc' is? in that case you could use the magnitude of it as distance.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!