3 This file is part of the MSP Märklin suite
4 Copyright © 2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
9 #include <msp/input/keys.h>
10 #include "cameracontroller.h"
15 CameraController::CameraController(Graphics::Window &w, GL::Camera &c):
24 window.signal_key_press.connect(sigc::mem_fun(this, &CameraController::key_press));
25 window.signal_key_release.connect(sigc::mem_fun(this, &CameraController::key_release));
28 void CameraController::tick(float dt)
30 if(!move_x && !move_y && !zoom && !rotate && !pitch)
33 GL::Vector3 pos = camera.get_position();
34 GL::Vector3 look = camera.get_look_direction();
35 float xy_len = sqrt(look.x*look.x+look.y*look.y);
36 GL::Vector3 look_xy(look.x/xy_len, look.y/xy_len, 0);
37 GL::Vector3 ground(pos.x-look.x*pos.z/look.z, pos.y-look.y*pos.z/look.z, 0);
41 float speed = -3*dt*xy_len*pos.z/look.z;
42 pos.x += (look_xy.y*rotate - look_xy.x*look.z*pitch)*speed;
43 pos.y += (-look_xy.x*rotate - look_xy.y*look.z*pitch)*speed;
44 pos.z += xy_len*pitch*speed;
45 camera.set_position(pos);
46 camera.look_at(ground);
49 if(move_x || move_y || zoom)
51 float zoom_speed = -zoom*pos.z/look.z;
52 pos.x += (look_xy.x*move_y + look_xy.y*move_x + look.x*zoom_speed)*dt;
53 pos.y += (look_xy.y*move_y - look_xy.x*move_x + look.y*zoom_speed)*dt;
54 pos.z += look.z*dt*zoom_speed;
55 camera.set_position(pos);
59 void CameraController::key_press(unsigned code, unsigned, wchar_t)
61 unsigned key = Msp::Input::key_from_sys(code);
63 if(key==Msp::Input::KEY_RIGHT)
65 else if(key==Msp::Input::KEY_LEFT)
67 else if(key==Msp::Input::KEY_UP)
69 else if(key==Msp::Input::KEY_DOWN)
71 else if(key==Msp::Input::KEY_INSERT)
73 else if(key==Msp::Input::KEY_PGUP)
75 else if(key==Msp::Input::KEY_HOME)
77 else if(key==Msp::Input::KEY_END)
79 else if(key==Msp::Input::KEY_DELETE)
81 else if(key==Msp::Input::KEY_PGDN)
85 void CameraController::key_release(unsigned code, unsigned)
87 unsigned key = Msp::Input::key_from_sys(code);
89 if(key==Msp::Input::KEY_RIGHT || key==Msp::Input::KEY_LEFT)
91 else if(key==Msp::Input::KEY_UP || key==Msp::Input::KEY_DOWN)
93 else if(key==Msp::Input::KEY_INSERT || key==Msp::Input::KEY_PGUP)
95 else if(key==Msp::Input::KEY_HOME || key==Msp::Input::KEY_END)
97 else if(key==Msp::Input::KEY_DELETE || key==Msp::Input::KEY_PGDN)