camera movement

This commit is contained in:
floralrainfall 2023-07-19 00:37:36 -04:00
parent a6bbc2d683
commit 935b547a16
7 changed files with 64 additions and 10 deletions

View File

@ -11,6 +11,8 @@ namespace RNR
void QtInputManager::keyEvent(QKeyEvent* e)
{
if(e->isAutoRepeat())
return;
if(e->type() == QEvent::KeyPress)
keyDown(e->key());
else if(e->type() == QEvent::KeyRelease)

View File

@ -14,6 +14,7 @@ namespace RNR
class IInputManager
{
World* m_world;
std::vector<int> scancodes_down;
protected:
MouseState state;
public:
@ -21,8 +22,11 @@ namespace RNR
virtual void resetMouse() {};
void setWorld(World* world) { m_world = world; }
void setWorld(World* world) { m_world = world; world->setInputManager(this); }
void frame();
bool isKeyDown(int scancode);
void keyDown(int scancode);
void keyUp(int scancode);

View File

@ -15,14 +15,11 @@ namespace RNR
CoordinateFrame m_focus;
virtual void deserializeProperty(char* prop_name, pugi::xml_node prop);
virtual void addProperties(std::vector<ReflectionProperty>& properties);
float m_cf_yaw;
float m_cf_pitch;
public:
Camera();
~Camera();
void cameraFrame(float xd, float yd);
void cameraFrame(float xd, float yd, bool movement_disable = true);
virtual std::string getClassName() { return "Camera"; }
CoordinateFrame& getCFrame() { return m_cframe; };

View File

@ -13,6 +13,8 @@
namespace RNR
{
class IInputManager;
struct WorldUndeserialized
{
Instance* instance;
@ -31,6 +33,7 @@ namespace RNR
Ogre::SceneManager* m_ogreSceneManager;
TopMenuBar* m_tmb;
InstanceFactory* m_instanceFactory;
IInputManager* m_inputManager;
float m_lastDelta;
void xmlAddItem(pugi::xml_node node, Instance* parent);
@ -48,6 +51,8 @@ namespace RNR
float getLastDelta() { return m_lastDelta; }
DataModel* getDatamodel() { return m_datamodel; }
void setInputManager(IInputManager* inputManager) { m_inputManager = inputManager; }
IInputManager* getInputManager() { return m_inputManager; }
void setDatamodel(DataModel* instance) { m_datamodel = instance; }
Workspace* getWorkspace() { return m_workspace; }
void setWorkspace(Workspace* workspace) { m_workspace = workspace; }

View File

@ -14,12 +14,24 @@ namespace RNR
void IInputManager::keyDown(int scancode)
{
auto it = std::find(scancodes_down.begin(), scancodes_down.end(), scancode);
if(it == scancodes_down.end())
scancodes_down.push_back(scancode);
}
void IInputManager::keyUp(int scancode)
{
auto it = std::find(scancodes_down.begin(), scancodes_down.end(), scancode);
if(it != scancodes_down.end())
scancodes_down.erase(it);
}
bool IInputManager::isKeyDown(int scancode)
{
auto it = std::find(scancodes_down.begin(), scancodes_down.end(), scancode);
if(it != scancodes_down.end())
return true;
return false;
}
void IInputManager::mouseMoveAbsolute(float x, float y)
@ -45,6 +57,14 @@ namespace RNR
}
}
void IInputManager::frame()
{
Workspace* workspace = m_world->getWorkspace();
Camera* camera = workspace->getCurrentCamera();
if(camera)
camera->cameraFrame(0, 0, false); // update camera position
}
void IInputManager::mousePrimaryState(bool down)
{
state.mouse_primary = down;

View File

@ -1,12 +1,12 @@
#include <App/V8/DataModel/Camera.hpp>
#include <App/V8/World/World.hpp>
#include <App/InputManager.hpp>
#include <Helpers/XML.hpp>
namespace RNR
{
Camera::Camera()
{
m_cf_yaw = 0.f;
m_cf_pitch = 0.f;
setName("Camera");
}
@ -27,7 +27,7 @@ namespace RNR
}
}
void Camera::cameraFrame(float xd, float yd)
void Camera::cameraFrame(float xd, float yd, bool movement_disable)
{
Ogre::Radian pitch = Ogre::Radian(yd);
Ogre::Radian yaw = Ogre::Radian(xd);
@ -44,6 +44,28 @@ namespace RNR
Ogre::Matrix3 rotation;
rotation.FromEulerAnglesYXZ(yaw, pitch, Ogre::Radian(0));
getCFrame().setRotation(rotation);
if(!movement_disable)
{
float speed = 50;
Ogre::Vector3 position = getCFrame().getPosition();
Ogre::Vector3 movement = Ogre::Vector3(0, 0, 0);
IInputManager* input = world->getInputManager();
if(input->isKeyDown('W'))
movement.z = -speed;
else if(input->isKeyDown('S'))
movement.z = speed;
if(input->isKeyDown('A'))
movement.x = -speed;
else if(input->isKeyDown('D'))
movement.x = speed;
movement = rotation * movement;
position += movement * world->getLastDelta();
getCFrame().setPosition(position);
}
}
void Camera::addProperties(std::vector<ReflectionProperty>& properties)

View File

@ -3,6 +3,7 @@
#include <App/V8/DataModel/PartInstance.hpp>
#include <App/GUI/SelectionBox.hpp>
#include <App/Humanoid/Humanoid.hpp>
#include <App/InputManager.hpp>
#include <stdexcept>
#include <pugixml.hpp>
@ -12,6 +13,8 @@ namespace RNR
{
Instance::setWorld(this);
m_inputManager = 0;
m_instanceFactory = new InstanceFactory();
m_instanceFactory->registerInstance("Camera", InstanceFactory::instanceBuilder<Camera>);
@ -124,7 +127,8 @@ namespace RNR
void World::preStep()
{
//
if(m_inputManager)
m_inputManager->frame();
}
double World::step(float timestep)