# 3D Camera Movement Question

How would I make the camera move forward in the direction it is facing.

Keep in mind the camera direction will change.

Thanks in advance for any help!

-Toxyn

normalize the difference between the camera position, and the position it’s looking at, times the normalized vector by speed, add that to your camera position.

Can you show me? I don’t understand.

So, you have a vec3 describing camera position(lets call it c) and a vec3 describing the position the camera is looking in(lets call it l). Then, you would do this:

``````normalizedVector=(c-l):normalize()
normalizedVector=normalizedVector*speed
c=c+normalizedVector
``````

I understand vectors can be confusing, but really they are one of the most useful things in Codea. Basically, you can write your own normalize function like:

``````function normalize(v1,v2)
local dif=v1-v2
dif=dif/math.sqrt(dif.x^2+dif.y^2)
return dif
end
``````

Essentially, you find the difference between the two vectors, then find the length of the hypotenuse of the triangle formed by dif.x and dif.y as two sides of a triangle, then divide the dif vector by the length of the hypotenuse so the hypotenuse is one, basically simplest form for vectors.

@TheSolderKing - better is to use the built in function normalized

``````--camPos is camera position, vec3 global var
--look is the direction the camera is looking, vec3 global var
--dist is the distance to move, in pixels
function MoveForward(dist)
camPos=camPos+(look-camPos):normalize()*dist
end
``````

Demo code - use the parameter to look left or right, touch the screen to move forward

``````function setup()
camPos=vec3(0,0,500) --camera position
look=vec3(0,0,0) --camera direction
parameter.integer("X",-100,100,0)
img1=readImage("Planet Cute:Character Boy")
img2=readImage("Planet Cute:Character Princess Girl")
end

function draw()
background(0)
perspective()
look.x=X --user can change X value of camera direction
camera(camPos.x,camPos.y,camPos.z,look.x,look.y,look.z)
sprite(img2,0,0)
translate(-100,0,0) sprite(img1,0,0)
translate(200,0,0) sprite(img1,0,0)
end

function MoveForward(dist)
camPos=camPos+(look-camPos):normalize()*dist
end

function touched(t)
if t.state==ENDED then MoveForward(10) end
end
``````

@Ignatz I pm’d you

I know @Ignatz, sorry for the misunderstanding. The reason I wrote my own is because at first, I had no idea there even was a normalize function built in :P. I also wrote it because I know that, when beginning with a topic, it is easier to know how something works rather than just using it. Well, perhaps not easier, but more beneficial in the end, I suppose. Also, I had a bit of a question, I have gotten the joystick to work so that the player moves forwards whenever you press the joystick, but right now the joystick might as well be a button. I am attempting to help @Toxyn and I have made it so the player can move forwards, but I am not sure how to combine the input from the joystick with the normalized vector between the camera position and the direction of looking. Any idea how to do this? Thank you!

-TheSolderKing

The easiest may be to calculate the direction from the centre of the joystick to the current touch position, and normalise it as above. This will give a value between -1 and +1 for each of x and y.

Then the x value can be used to turn left and right, and the y value to move forward or backward.

``````--assume centre of joystick is at vec2 called joy
--and radius of joystick is r
function touch(t)
local tch=vec2(t.x,t.y)
if tch:dist(joy)<=r then
move=(tch-joy):normalize()
end
end

--and in draw..
if move then
angle=angle+move.x*2 --turn up to 2 degrees per frame
speed=move.y*3 --move up to 3 pixels per frame
move=nil
end
``````

Thanks @Ignatz, I appreciate it, but what I should rephrase my question: say I have two vectors, one called v1 and represents the normalized vector between the camera position and look position, and another, v2, which represents the normalized vector between the center of the joystick and the current touch. How could I combine those two to have relative movement to the direction the player is looking in? In other words, I would like it so that if you pull back on the joystick, the player moves backwards relative to the direction they are looking. Thank you in advance,

TheSolderKing

If you want to move forward or backwards in the current direction, use my MoveForward function, so you would simply add a line to the end of my code above

``````MoveForward(speed)
``````

if you pull back on the joystick, speed is negative and you move backwards