Skip to content

Instantly share code, notes, and snippets.

@xentec
Last active August 29, 2015 14:27
Show Gist options
  • Save xentec/8b05f107efee521ec660 to your computer and use it in GitHub Desktop.
Save xentec/8b05f107efee521ec660 to your computer and use it in GitHub Desktop.
Quaternion camera
const vec3 Camera::axisV = {0.f, 1.f, 0.f};
const vec3 Camera::axisF = {0.f, 0.f, 1.f};
mat4 Camera::getView(const vec3& pos, const quat& rot) const
{
vec3 up = rot * axisV;
vec3 forward = rot * axisF;
return glm::lookAt(pos, pos + forward, up);
}
void PlayerControl::update(EntityManager& es, EventManager& evm, f32 dt)
{
vec3 dir {};
f32 mul = 1.0f;
if(keys[GLFW_KEY_W]) dir += vec3(0,0,1);
if(keys[GLFW_KEY_S]) dir += vec3(0,0,-1);
if(keys[GLFW_KEY_D]) dir += vec3(-1,0,0);
if(keys[GLFW_KEY_A]) dir += vec3(1,0,0);
if(keys[GLFW_KEY_C]) dir += vec3(0,-1,0);
if(keys[GLFW_KEY_SPACE]) dir += vec3(0,1,0);
if(mod & GLFW_MOD_SHIFT) mul *= 2.0;
vec3 rot {};
if(keys[GLFW_KEY_UP]) rot.x += -1;
if(keys[GLFW_KEY_DOWN]) rot.x += 1;
if(keys[GLFW_KEY_LEFT]) rot.y += 1;
if(keys[GLFW_KEY_RIGHT]) rot.y += -1;
if(keys[GLFW_KEY_DELETE]) rot.z += -1;
if(keys[GLFW_KEY_PAGE_DOWN]) rot.z += 1;
ComponentHandle<Controllable> ctrl;
ComponentHandle<Position> pos;
ComponentHandle<Motion> mov;
for(const Entity& e: es.entities_with_components(ctrl, pos))
{
quat rota {};
rota *= glm::angleAxis(glm::radians(rot.x), glm::vec3(1,0,0));
rota *= glm::angleAxis(glm::radians(rot.y), glm::vec3(0,1,0));
rota *= glm::angleAxis(glm::radians(rot.z), glm::vec3(0,0,1));
pos->dir *= rota;
}
for(const Entity& e: es.entities_with_components(ctrl, pos, mov))
{
mov->v = pos->dir * dir * mul;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment