c# - Converting FPS ground camera to FPS free flying camera -
i have implemented camera in opengl(opentk) project
//move(0f, 0.1f, 0f); } forward //move(-0.1f, 0f, 0f); } left //move(0f, -0.1f, 0f); } //move(0.1f, 0f, 0f); } right //move(0f, 0f, 0.1f); } //move(0f, 0f, -0.1f); } down public static void move(float x, float y, float z) { vector3 offset = new vector3(); vector3 forward = new vector3((float)math.sin((float)orientation.x), 0, (float)math.cos((float)orientation.x)); vector3 right = new vector3(-forward.z,0,forward.x); offset += x * right; offset += y * forward; offset.y += z; offset.normalizefast(); offset = vector3.multiply(offset, movespeed); position += offset; }
where "orientation" x,y of direction camera facing. "position" position of camera in world, , "movespeed" float.
this camera works great. ground based. mean x value of camera orientation affects movement direction. y value not. want make free flying camera if , press forward camera fly air.
i tried changing forward declation to:
vector3 forward = new vector3((float)math.sin((float)orientation.x), (float)math.sin((float)orientation.y), (float)math.cos((float)orientation.x));
this partially works, camera can fly air. not right, camera moving same forward amount no matter how far "up" tilt it. not replacing of forward, being added onto it.
i hope explanation makes sense.
thanks
you can this: first forward
vector orientation
(you can use camera.lookat well)
public vector3 getforward(vector2 orientation) { vector3 forward = new vector3(0, 1, 0); //x axis rotation forward.z = (float)math.sin((float)orientation.y); forward.y = (float)math.cos((float)orientation.y); //z axis rotation forward.x = forward.y*(float)math.sin((float)orientation.x); forward.y = forward.y*(float)math.cos((float)orientation.x); return forward; }
and move camera with:
public void move(float horizontal, float strafe) { vector3 forward=getforward(orientation); //forward vector projected on xoy plane , rotated 90 degrees vector3 leftxoy = new vector3(-forward.y ,forward.x,0); vector3 movedirection = vector3.multiply(forward,horizontal)+vector3.multiply(leftxoy,strafe); movedirection.normalize(); position += vector3.multiply(movedirection,movespeed); }
so if call move(1,-1);
move camera forward (along forward
vector) , strafe right.
Comments
Post a Comment