paella/Code/src/SFMLTools/JoyCam.cpp

111 lines
3.3 KiB
C++

////////////////////////////////////////////////////////////////////////////////
//
// Paella
// Copyright (C) 2015 - Thomas FORGIONE, Emilie JALRAS, Marion LENFANT, Thierry MALON, Amandine PAILLOUX
// Authors :
// Thomas FORGIONE
// Emilie JALRAS
// Marion LENFANT
// Thierry MALON
// Amandine PAILLOUX
//
// This file is part of the project Paella
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <cmath>
#include <GL/glu.h>
#include <boost/algorithm/clamp.hpp>
#include "SFMLTools/VectorFunctions.hpp"
#include "SFMLTools/JoyCam.hpp"
namespace sft
{
JoyCam::JoyCam(int i) : speed(0.001), sensitivity(0.0005), theta(0), phi(0), joy(i)
{
vectorsFromAngles();
}
void JoyCam::nextStep(int width, int height)
{
// Gestion de la souris
sf::Vector2i mouse_position =
sf::Vector2i{
static_cast<int>(-sf::Joystick::getAxisPosition(joy, sf::Joystick::Z)),
static_cast<int>(-sf::Joystick::getAxisPosition(joy, sf::Joystick::R))
};
theta += sensitivity * (mouse_position.x);
phi += sensitivity * (mouse_position.y);
vectorsFromAngles();
// Gestion du clavier
sf::Vector3f move;
move -= speed * sf::Joystick::getAxisPosition(joy, sf::Joystick::Y) * forward;
move -= speed * sf::Joystick::getAxisPosition(joy, sf::Joystick::X) * left;
// if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
// {
// move += forward * speed;
// }
// if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
// {
// move -= forward * speed;
// }
// if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q))
// {
// move += left * speed;
// }
// if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
// {
// move -= left * speed;
// }
position += move;
target += move;
}
void JoyCam::vectorsFromAngles()
{
static constexpr auto PI_2_MINUS_EPSILON = M_PI_2 - 0.1;
phi = boost::algorithm::clamp(phi,-PI_2_MINUS_EPSILON,PI_2_MINUS_EPSILON);
// Passage du sphérique au cartésien
float r_temp = std::cos(phi);
forward.z = std::sin(phi);
forward.x = r_temp * std::cos(theta);
forward.y = r_temp * std::sin(theta);
left = crossProduct(sf::Vector3<float>(0,0,1), forward);
normalize(left);
target = position + forward;
}
void JoyCam::look() const
{
gluLookAt(position.x, position.y, position.z,
target.x, target.y, target.z,
0,0,1);
}
} // namespace sft