Help with 3d camera, first person

Just started learing 3d and having trouble with the camera.
I’m trying to make it first person, so i assume you would rotate the lookat pos around the eye pos.
I have tried a formula i use in 2d, but the distance between the two decreases the more i rotate.
What is the proper way to do this?
Any help would be appreciated

``````function setup()
lookAt = vec3(0,0,0) --camera direction
parameter.watch("lookAt")
parameter.watch("lookAt:dist(vec3(0,0,500))")
parameter.number("ang",-90,95,85)
parameter.integer("spriteZ",-100,1000,620)
end

function draw()
background()perspective()pushMatrix()

dist = 500
lookAt.x = math.cos(math.rad(ang))*dist
lookAt.z = math.sin(math.rad(ang))*dist

camera(0, 0, 500, lookAt.x, lookAt.y, lookAt.z)

translate(-100,0,0) sprite("Planet Cute:Character Cat Girl",0,0)
translate(500,0,spriteZ) sprite("Planet Cute:Character Horn Girl",0,0)

popMatrix()ortho()viewMatrix(matrix())
strokeWidth(2)fill(127, 127, 127, 145)translate(WIDTH/2,HEIGHT/2)
ellipse(0,0,40)
ellipse(lookAt.x/10,-lookAt.z/10,20)
popMatrix()
end

``````

@Jaybob - Have a look at this

https://coolcodea.wordpress.com/2015/02/12/198-looking-at-objects-in-3d/

It may help you to read the previous posts on dot products and cross products, it will make it easier for you to understand how it works.

I have many other 3D posts on that blog, and I have some ebooks, including one on 3D, you’ll find a link at the top of the index page of the blog

@Jaybob Look at the discussion ‘starter game 16’ that I recently posted. That uses simple math to move the camera ‘look at’ position. That might give you a start.

Thanks for the help guys, some really interesting stuff there, if a bit daunting ^:)^
I realised what the problem was. I set camEye.z at 500 and then multiplied the sin/cos angles by 500, that doubled the distance between the two and set the point of rotation in the middle.

``````function setup()
parameter.number("ang",-360,360,-90)
parameter.number("y",-2000,2000,500)
parameter.watch("cam")
parameter.watch("eye")
cam = vec3(0, 0, 0)
eye = vec3(0, 0, 0)
end

function draw()
background()
rectMode(CENTER)
perspective()
pushMatrix()
eye.x = math.cos(math.rad(ang))*500
eye.z = math.sin(math.rad(ang))*500
cam.y = y
camera(cam.x, cam.y, cam.z, eye.x, eye.y, eye.z)
fill(87, 255, 0, 185)
for i = 0,7 do
local x, z = math.cos(math.rad(i*45))*1000, math.sin(math.rad(i*45))*1000
pushMatrix()
translate(x,0,z)
rotate(i*-45+90,0,1,0)
rect(0,0,100,100)
popMatrix()
end
translate(0,0,0)
rotate(90,1,0,0)
fill(95, 0, 255, 96)
ellipse(0,0,2000)
end
``````