Camera change...
This commit is contained in:
@@ -15,8 +15,8 @@
|
||||
#include <cmath>
|
||||
|
||||
PlayerControllerSystem::PlayerControllerSystem(flecs::world &world,
|
||||
Ogre::SceneManager *sceneMgr,
|
||||
EditorApp *editorApp)
|
||||
Ogre::SceneManager *sceneMgr,
|
||||
EditorApp *editorApp)
|
||||
: m_world(world)
|
||||
, m_sceneMgr(sceneMgr)
|
||||
, m_editorApp(editorApp)
|
||||
@@ -78,6 +78,10 @@ void PlayerControllerSystem::initController(flecs::entity controllerEntity,
|
||||
m_sceneMgr->getRootSceneNode()->createChildSceneNode();
|
||||
}
|
||||
|
||||
// Initialize camera to start behind character (180 degrees yaw)
|
||||
state.yaw = 180.0f;
|
||||
state.pitch = 0.0f;
|
||||
|
||||
state.initialized = true;
|
||||
state.faceHidden = false;
|
||||
}
|
||||
@@ -86,7 +90,8 @@ void PlayerControllerSystem::update(float deltaTime)
|
||||
{
|
||||
if (!m_editorApp ||
|
||||
m_editorApp->getGameMode() != EditorApp::GameMode::Game ||
|
||||
m_editorApp->getGamePlayState() != EditorApp::GamePlayState::Playing)
|
||||
m_editorApp->getGamePlayState() !=
|
||||
EditorApp::GamePlayState::Playing)
|
||||
return;
|
||||
|
||||
m_world.query<PlayerControllerComponent>().each(
|
||||
@@ -153,8 +158,7 @@ void PlayerControllerSystem::updateTPSCamera(PlayerControllerComponent &pc,
|
||||
CharacterSlotSystem *css =
|
||||
m_editorApp->getCharacterSlotSystem();
|
||||
if (css) {
|
||||
css->setSlotVisible(state.targetEntity, "face",
|
||||
true);
|
||||
css->setSlotVisible(state.targetEntity, "face", true);
|
||||
}
|
||||
state.faceHidden = false;
|
||||
}
|
||||
@@ -176,20 +180,19 @@ void PlayerControllerSystem::updateTPSCamera(PlayerControllerComponent &pc,
|
||||
state.pivotNode->setPosition(pivotPos);
|
||||
|
||||
// Compute goal position based on yaw/pitch/distance
|
||||
Ogre::Quaternion yawRot(Ogre::Degree(state.yaw),
|
||||
Ogre::Vector3::UNIT_Y);
|
||||
Ogre::Quaternion yawRot(Ogre::Degree(state.yaw), Ogre::Vector3::UNIT_Y);
|
||||
Ogre::Quaternion pitchRot(Ogre::Degree(state.pitch),
|
||||
Ogre::Vector3::UNIT_X);
|
||||
Ogre::Vector3 offset = yawRot * pitchRot *
|
||||
Ogre::Vector3(0, 0, pc.tpsDistance);
|
||||
Ogre::Vector3 offset =
|
||||
yawRot * pitchRot * Ogre::Vector3(0, 0, pc.tpsDistance);
|
||||
Ogre::Vector3 goalPos = pivotPos + offset;
|
||||
|
||||
state.goalNode->setPosition(goalPos);
|
||||
|
||||
// Smoothly interpolate camera to goal
|
||||
Ogre::Vector3 currentPos = camNode->getPosition();
|
||||
Ogre::Vector3 newPos = Ogre::Math::lerp(currentPos, goalPos,
|
||||
deltaTime * 9.0f);
|
||||
Ogre::Vector3 newPos =
|
||||
Ogre::Math::lerp(currentPos, goalPos, deltaTime * 9.0f);
|
||||
camNode->setPosition(newPos);
|
||||
camNode->lookAt(pivotPos, Ogre::Node::TS_WORLD);
|
||||
}
|
||||
@@ -211,8 +214,7 @@ void PlayerControllerSystem::updateFPSCamera(PlayerControllerComponent &pc,
|
||||
return;
|
||||
|
||||
// Find animated entity for bone access
|
||||
AnimationTreeSystem *ats =
|
||||
m_editorApp->getAnimationTreeSystem();
|
||||
AnimationTreeSystem *ats = m_editorApp->getAnimationTreeSystem();
|
||||
if (!ats)
|
||||
return;
|
||||
|
||||
@@ -229,8 +231,8 @@ void PlayerControllerSystem::updateFPSCamera(PlayerControllerComponent &pc,
|
||||
bone = skel->getBone(pc.fpsBoneName);
|
||||
} catch (...) {
|
||||
// Try common alternatives
|
||||
const char *alternatives[] = { "Head", "head", "Neck", "neck",
|
||||
"Camera", "camera" };
|
||||
const char *alternatives[] = { "Head", "head", "Neck",
|
||||
"neck", "Camera", "camera" };
|
||||
for (const char *name : alternatives) {
|
||||
try {
|
||||
bone = skel->getBone(name);
|
||||
@@ -253,25 +255,21 @@ void PlayerControllerSystem::updateFPSCamera(PlayerControllerComponent &pc,
|
||||
CharacterSlotSystem *css =
|
||||
m_editorApp->getCharacterSlotSystem();
|
||||
if (css) {
|
||||
css->setSlotVisible(state.targetEntity, "face",
|
||||
false);
|
||||
css->setSlotVisible(state.targetEntity, "face", false);
|
||||
}
|
||||
state.faceHidden = true;
|
||||
}
|
||||
|
||||
// Get character scene node
|
||||
auto &transform =
|
||||
state.targetEntity.get<TransformComponent>();
|
||||
auto &transform = state.targetEntity.get<TransformComponent>();
|
||||
Ogre::SceneNode *charNode = transform.node;
|
||||
|
||||
// Compute bone world transform
|
||||
Ogre::Vector3 boneWorldPos =
|
||||
charNode->_getDerivedOrientation() *
|
||||
bone->_getDerivedPosition() +
|
||||
charNode->_getDerivedPosition();
|
||||
Ogre::Quaternion boneWorldRot =
|
||||
charNode->_getDerivedOrientation() *
|
||||
bone->_getDerivedOrientation();
|
||||
Ogre::Vector3 boneWorldPos = charNode->_getDerivedOrientation() *
|
||||
bone->_getDerivedPosition() +
|
||||
charNode->_getDerivedPosition();
|
||||
Ogre::Quaternion boneWorldRot = charNode->_getDerivedOrientation() *
|
||||
bone->_getDerivedOrientation();
|
||||
|
||||
// Offset slightly forward
|
||||
Ogre::Vector3 offset = boneWorldRot * Ogre::Vector3(0, 0, 0.15f);
|
||||
@@ -289,16 +287,15 @@ void PlayerControllerSystem::updateFPSCamera(PlayerControllerComponent &pc,
|
||||
state.pitch = -89.0f;
|
||||
}
|
||||
|
||||
Ogre::Quaternion yawRot(Ogre::Degree(state.yaw),
|
||||
Ogre::Vector3::UNIT_Y);
|
||||
Ogre::Quaternion yawRot(Ogre::Degree(state.yaw), Ogre::Vector3::UNIT_Y);
|
||||
Ogre::Quaternion pitchRot(Ogre::Degree(state.pitch),
|
||||
Ogre::Vector3::UNIT_X);
|
||||
camNode->setOrientation(yawRot * pitchRot);
|
||||
}
|
||||
|
||||
void PlayerControllerSystem::updateLocomotion(PlayerControllerComponent &pc,
|
||||
ControllerState &state,
|
||||
float deltaTime)
|
||||
ControllerState &state,
|
||||
float deltaTime)
|
||||
{
|
||||
(void)deltaTime;
|
||||
if (!state.targetEntity.has<CharacterComponent>())
|
||||
@@ -308,8 +305,7 @@ void PlayerControllerSystem::updateLocomotion(PlayerControllerComponent &pc,
|
||||
auto &cc = state.targetEntity.get_mut<CharacterComponent>();
|
||||
|
||||
// Get camera yaw for relative movement
|
||||
Ogre::Quaternion yawRot(Ogre::Degree(state.yaw),
|
||||
Ogre::Vector3::UNIT_Y);
|
||||
Ogre::Quaternion yawRot(Ogre::Degree(state.yaw), Ogre::Vector3::UNIT_Y);
|
||||
Ogre::Vector3 forward = yawRot * Ogre::Vector3::NEGATIVE_UNIT_Z;
|
||||
Ogre::Vector3 right = yawRot * Ogre::Vector3::UNIT_X;
|
||||
|
||||
@@ -346,14 +342,15 @@ void PlayerControllerSystem::updateLocomotion(PlayerControllerComponent &pc,
|
||||
flatForward.y = 0;
|
||||
if (flatForward.squaredLength() > 0.0001f) {
|
||||
flatForward.normalise();
|
||||
Ogre::Quaternion targetRot = Ogre::Vector3::NEGATIVE_UNIT_Z.getRotationTo(
|
||||
flatForward);
|
||||
Ogre::Quaternion currentRot = transform.node->getOrientation();
|
||||
Ogre::Quaternion targetRot =
|
||||
Ogre::Vector3::NEGATIVE_UNIT_Z
|
||||
.getRotationTo(flatForward);
|
||||
Ogre::Quaternion currentRot =
|
||||
transform.node->getOrientation();
|
||||
transform.node->setOrientation(
|
||||
Ogre::Quaternion::Slerp(deltaTime * 10.0f,
|
||||
currentRot,
|
||||
targetRot,
|
||||
true));
|
||||
Ogre::Quaternion::Slerp(
|
||||
deltaTime * 10.0f, currentRot,
|
||||
targetRot, true));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -361,8 +358,7 @@ void PlayerControllerSystem::updateLocomotion(PlayerControllerComponent &pc,
|
||||
}
|
||||
|
||||
// Update animation state
|
||||
AnimationTreeSystem *ats =
|
||||
m_editorApp->getAnimationTreeSystem();
|
||||
AnimationTreeSystem *ats = m_editorApp->getAnimationTreeSystem();
|
||||
if (!ats)
|
||||
return;
|
||||
|
||||
@@ -376,7 +372,7 @@ void PlayerControllerSystem::updateLocomotion(PlayerControllerComponent &pc,
|
||||
}
|
||||
|
||||
if (!animState.empty()) {
|
||||
ats->setState(state.targetEntity,
|
||||
pc.locomotionStateMachine, animState);
|
||||
ats->setState(state.targetEntity, pc.locomotionStateMachine,
|
||||
animState);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user