The Short Version (TL;DR(
我有一个Camera
连接到SceneNode
,只要SceneNode
的旋转/轴与世界的旋转/轴对齐,运动就可以正常工作。然而,当一个物体旋转以"看"不同的方向并被告知"向前"移动时,它不会沿着新的"向前"方向移动。相反,它继续沿应用旋转之前面向的方向移动。
详细信息和示例
我有一个场景图来管理 3D 场景。该图是SceneNode
对象的树,这些对象知道它们相对于父对象和世界的转换。
根据 TL;DR; 代码段,假设您有一个旋转为零的cameraNode
(例如朝北(,然后将cameraNode
围绕 +Y"向上"轴向左旋转 90 度,即使其向西看。到目前为止,一切还不错。如果您现在尝试将cameraNode
"向前"移动,现在向西移动,则cameraNode
移动时,就好像"向前"仍然面向北方一样。
简而言之,它的移动就好像它从未被旋转过一样。
下面的代码显示了我最近尝试的内容,以及我(当前(在缩小最有可能与问题相关的区域方面的最佳猜测。
相关SceneNode
成员
SceneNode
实现具有以下字段(仅显示与此问题相关的字段(:
class GenericSceneNode implements SceneNode {
// this node's parent; always null for the root scene node in the graph
private SceneNode parentNode;
// transforms are relative to a parent scene node, if any
private Vector3 relativePosition = Vector3f.createZeroVector();
private Matrix3 relativeRotation = Matrix3f.createIdentityMatrix();
private Vector3 relativeScale = Vector3f.createFrom(1f, 1f, 1f);
// transforms are derived by combining transforms from all parents;
// these are relative to the world --in world space
private Vector3 derivedPosition = Vector3f.createZeroVector();
private Matrix3 derivedRotation = Matrix3f.createIdentityMatrix();
private Vector3 derivedScale = Vector3f.createFrom(1f, 1f, 1f);
// ...
}
向场景添加Camera
仅意味着它附加到图形中的SceneNode
。由于Camera
没有自己的位置/旋转信息,因此客户端只需处理Camera
所连接的SceneNode
,仅此而已。
除了这个问题中提到的问题外,其他一切似乎都在按预期工作。
SceneNode
翻译
在特定方向上转换节点的数学很简单,基本上可以归结为:
currentPosition = currentPosition + normalizedDirectionVector * offset;
SceneNode
实现如下:
@Override
public void moveForward(float offset) {
translate(getDerivedForwardAxis().mult(-offset));
}
@Override
public void moveBackward(float offset) {
translate(getDerivedForwardAxis().mult(offset));
}
@Override
public void moveLeft(float offset) {
translate(getDerivedRightAxis().mult(-offset));
}
@Override
public void moveRight(float offset) {
translate(getDerivedRightAxis().mult(offset));
}
@Override
public void moveUp(float offset) {
translate(getDerivedUpAxis().mult(offset));
}
@Override
public void moveDown(float offset) {
translate(getDerivedUpAxis().mult(-offset));
}
@Override
public void translate(Vector3 tv) {
relativePosition = relativePosition.add(tv);
isOutOfDate = true;
}
除了这个问题中提到的问题外,事情如预期的那样。
SceneNode
旋转
客户端应用程序按如下方式轮换cameraNode
:
final Angle rotationAngle = new Degreef(-90f);
// ...
cameraNode.yaw(rotationAngle);
SceneNode
实现也相当简单:
@Override
public void yaw(Angle angle) {
// FIXME?: rotate(angle, getDerivedUpAxis()) accumulates other rotations
rotate(angle, Vector3f.createUnitVectorY());
}
@Override
public void rotate(Angle angle, Vector3 axis) {
relativeRotation = relativeRotation.rotate(angle, axis);
isOutOfDate = true;
}
旋转的数学/代码封装在 3x3 矩阵对象中。请注意,在测试期间,您可以看到场景围绕相机旋转,因此确实应用了旋转,这使这个问题更加让我感到困惑。
方向矢量
方向向量只是取自派生的 3x3 旋转矩阵中的列,相对于世界:
@Override
public Vector3 getDerivedRightAxis() {
return derivedRotation.column(0);
}
@Override
public Vector3 getDerivedUpAxis() {
return derivedRotation.column(1);
}
@Override
public Vector3 getDerivedForwardAxis() {
return derivedRotation.column(2);
}
计算派生转换
如果相关,则parentNode
转换的组合方式如下,以计算this
实例的派生转换:
private void updateDerivedTransforms() {
if (parentNode != null) {
/**
* derivedRotation = parent.derivedRotation * relativeRotation
* derivedScale = parent.derivedScale * relativeScale
* derivedPosition = parent.derivedPosition + parent.derivedRotation * (parent.derivedScale * relativePosition)
*/
derivedRotation = parentNode.getDerivedRotation().mult(relativeRotation);
derivedScale = parentNode.getDerivedScale().mult(relativeScale);
Vector3 scaledPosition = parentNode.getDerivedScale().mult(relativePosition);
derivedPosition = parentNode.getDerivedPosition().add(parentNode.getDerivedRotation().mult(scaledPosition));
} else {
derivedPosition = relativePosition;
derivedRotation = relativeRotation;
derivedScale = relativeScale;
}
Matrix4 t, r, s;
t = Matrix4f.createTranslationFrom(relativePosition);
r = Matrix4f.createFrom(relativeRotation);
s = Matrix4f.createScalingFrom(relativeScale);
relativeTransform = t.mult(r).mult(s);
t = Matrix4f.createTranslationFrom(derivedPosition);
r = Matrix4f.createFrom(derivedRotation);
s = Matrix4f.createScalingFrom(derivedScale);
derivedTransform = t.mult(r).mult(s);
}
这用于通过场景图传播转换,以便子SceneNode
可以考虑其父级的转换。
其他/相关问题
在发布此问题之前的最近 ~3 周内,我已经在 SO 内部和外部浏览了几个答案(例如,这里、这里、这里和这里等(。显然,虽然相关,但它们对我的情况确实没有帮助。
评论中问题的答案
您确定在计算时
derivedTransform
已经计算了您父母的derivedTransform
吗?
是的,父SceneNode
始终在更新子级之前更新。update
逻辑是:
@Override
public void update(boolean updateChildren, boolean parentHasChanged) {
boolean updateRequired = parentHasChanged || isOutOfDate;
// update this node's transforms before updating children
if (updateRequired)
updateFromParent();
if (updateChildren)
for (Node n : childNodesMap.values())
n.update(updateChildren, updateRequired);
emitNodeUpdated(this);
}
@Override
public void updateFromParent() {
updateDerivedTransforms(); // implementation above
isOutOfDate = false;
}
本文调用上一节中的私有方法。
这不是一个直接的答案,而是作为OP要求的参考。
使用旧 API 调用的 OpenGL v1.0:在场景类的场景图之外的场景类中使用它时,实现相机类对象。这是用C++写的
相机.h
#ifndef CAMERA_H
#define CAMERA_H
#include "Core.h"
class Camera {
private:
Vector3 _v3EyePosition;
Vector3 _v3LookCenter;
Vector3 _v3Up;
public:
Camera();
~Camera();
void Get3rdPersonLocation( Vector3 &v3Position, float &fAngle );
void Set( Vector3 v3EyePosition, Vector3 v3LookCenter, Vector3 v3Up = Vector3( 0.0f, 1.0f, 0.0f ) );
void Render();
};
#endif
相机.cpp
#include "stdafx.h"
#include "Camera.h"
Camera::Camera() {
_v3EyePosition = Vector3( 0.0f, 0.0f, 0.0f );
_v3LookCenter = Vector3( 0.0f, 0.0f, -1.0f );
_v3Up = Vector3( 0.0f, 1.0f, 0.0f );
}
Camera::~Camera() {
}
void Camera::Get3rdPersonLocation( Vector3 &v3Position, float &fAngle ) {
v3Position._fX = _v3LookCenter._fX;
v3Position._fY = _v3EyePosition._fY;
v3Position._fZ = _v3LookCenter._fZ;
// Find Angle
float fX = _v3LookCenter._fX - _v3EyePosition._fX;
float fZ = _v3LookCenter._fZ - _v3EyePosition._fZ;
// Angle In Degrees
fAngle = Math::Radian2Degree( atan2( fX, fZ ) );
}
void Camera::Set( Vector3 v3EyePosition, Vector3 v3LookCenter, Vector3 v3Up ) {
_v3EyePosition = v3EyePosition;
_v3LookCenter = v3LookCenter;
_v3Up = v3Up;
}
void Camera::Render() {
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
gluLookAt( _v3EyePosition._fX, _v3EyePosition._fY, _v3EyePosition._fZ,
_v3LookCenter._fX, _v3LookCenter._fY, _v3LookCenter._fZ,
_v3Up._fX, _v3Up._fY, _v3Up._fZ );
}
在Camera
的Render
函数中使用旧的OpenGL API调用,我们首先加载Modelview矩阵,然后加载单位矩阵;最后我们使用glu的gluLookAt(...(方法来设置所需向量的位置。
Scene.h - 具有许多成员和函数;但对于Camera
对象,它有一个摄像机作为成员,而不是指向摄像机的指针。
场景.cpp - 渲染((
void Scene::Render() {
// Update Camera
_Camera.Set( _Player.GetPosition(), _Player.GetLookCenter() );
// Position Camera
_Camera.Render();
if ( UserSettings::Get()->_bQuit ) {
return;
}
if ( _vpNodes.size() < 1 ) {
// No SceneGraph To Render
return;
}
EnableLights();
// Send Items To Be Rendered
// Clear 2nd Render Pass Container
DeleteAllAlphaObjects();
// Render All Opaque Objects (1st Pass) & Store 2nd Pass Objects
_vpNodes[0]->RenderOGL( false, true );
// Render All Objects With Alpha Values (2nd Pass)
glEnable( GL_BLEND );
glMatrixMode( GL_MODELVIEW );
for ( std::vector<AlphaObject*>::iterator it = _vpAlphaObjects.begin(); it != _vpAlphaObjects.end(); ++it ) {
// Set Model View Matrix
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
glLoadMatrixf( &(*it)->f16Matrix[0] );
(*it)->pShape->RenderOGL( true, false );
glMatrixMode( GL_MODELVIEW );
glPopMatrix();
}
// Show Selected Weapon
_Player.RenderWeapon();
glDisable( GL_BLEND );
DisableLights();
return;
}
在这里,Camera
独立于Player
类以及场景的场景图层次结构,我们在场景的Render
调用中使用Camera
。在这里,我们通过获取Player
的当前位置和Player's
LookCenter
方向来设置Camera
。
编辑 - 添加用于移动计算的玩家类和相关代码
enum Action {
NO_ACTION = -1,
MOVING_FORWARD = 0,
MOVING_BACK,
MOVING_LEFT,
MOVING_RIGHT,
LOOKING_LEFT,
LOOKING_RIGHT,
LOOKING_UP,
LOOKING_DOWN,
}; // Action
玩家.h
#ifndef PLAYER_H
#define PLAYER_H
#include "Core.h"
class Weapon;
class NodeTransform;
class Player {
private:
enum MouseLook {
ML_NORMAL = 1,
ML_INVERT = -1,
} _MouseLookState; // MouseLook
Vector3 _v3Position;
Vector3 _v3LookCenter;
float _fLookDistance;
float _fMaxUp;
float _fMaxDown;
float _fLinearSpeed;
float _fAngularSpeed;
public:
Player( float fLookDistance );
~Player();
void SetSpeed( float fLinear, float fAngular );
void SetMouseY( bool bInvert );
void SetLocation( Vector3 v3Position, Vector3 v3Direction = Vector3( 0.0f, 0.0f, -1.0f ) );
void Move( Action action, float fDeltaTime );
bool Update();
inline void SetPosition( Vector3 v3Position );
inline Vector3 GetPosition();
inline Vector3 GetLookCenter();
inline Vector3 GetLookDirection();
};
inline void Player::SetPosition( Vector3 v3Position ) {
Vector3 v3LookDirection;
v3LookDirection = _v3LookCenter - _v3Position;
_v3Position = v3Position;
_v3LookCenter = v3Position + v3LookDirection;
}
inline Vector3 Player::GetPosition() {
return _v3Position;
}
inline Vector3 Player::GetLookCenter() {
return _v3LookCenter;
}
inline Vector3 Player::GetLookDirection() {
Vector3 v3LookDirection;
v3LookDirection = _v3LookCenter - _v3Position;
v3LookDirection.Normalize();
return v3LookDirection;
}
#endif
播放器.cpp
#include "stdafx.h"
#include "Player.h"
#include "UserSettings.h"
#include "NodeTransform.h"
Player::Player( float fLookDistance ) {
_fLookDistance = fLookDistance;
// Calculate Maximum Limits For Looking Up And Down
_fMaxUp = _fLookDistance * tan( Math::Degree2Radian( 50 ) );
_fMaxDown = _fLookDistance * tan( Math::Degree2Radian( 40 ) );
_v3Position = Vector3( 0.0f, 0.5f, 0.0f );
_v3LookCenter = Vector3( 0.0f, 0.5f, -fLookDistance );
_fLinearSpeed = 15.0f; // Units Per Second
_fAngularSpeed = 3.0f; // Radians Per Second
SetMouseY( UserSettings::Get()->GetMouseInvert() );
}
Player::~Player() {
} // ~Player
void Player::SetMouseY( bool bInvert ) {
if ( bInvert ) {
_MouseLookState = ML_INVERT;
} else {
_MouseLookState = ML_NORMAL;
}
}
void Player::SetLocation( Vector3 v3Position, Vector3 v3Direction ) {
_v3Position = v3Position;
_v3LookCenter = v3Position + _fLookDistance*v3Direction;
}
void Player::Move( Action action, float fDeltaTime ) {
Vector3 v3LookDirection;
v3LookDirection = _v3LookCenter - _v3Position;
switch ( action ) {
case MOVING_FORWARD: {
// Prevent Vertical Motion
v3LookDirection._fY = 0.0f;
_v3Position += v3LookDirection*fDeltaTime*_fLinearSpeed;
_v3LookCenter += v3LookDirection*fDeltaTime*_fLinearSpeed;
break;
}
case MOVING_BACK: {
// Prevent Vertical Motion
v3LookDirection._fY = 0.0f;
_v3Position -= v3LookDirection*fDeltaTime*_fLinearSpeed;
_v3LookCenter -= v3LookDirection*fDeltaTime*_fLinearSpeed;
break;
}
case MOVING_LEFT: {
// Get "Side" Direction & Prevent Vertical Motion
v3LookDirection._fY = v3LookDirection._fX;
v3LookDirection._fX = -v3LookDirection._fZ;
v3LookDirection._fZ = v3LookDirection._fY;
v3LookDirection._fY = 0.0f;
_v3Position -= v3LookDirection*fDeltaTime*_fLinearSpeed;
_v3LookCenter -= v3LookDirection*fDeltaTime*_fLinearSpeed;
break;
}
case MOVING_RIGHT: {
// Get "Side" Direction & Prevent Vertical Motion
v3LookDirection._fY = v3LookDirection._fX;
v3LookDirection._fX = -v3LookDirection._fZ;
v3LookDirection._fZ = v3LookDirection._fY;
v3LookDirection._fY = 0.0f;
_v3Position += v3LookDirection*fDeltaTime*_fLinearSpeed;
_v3LookCenter += v3LookDirection*fDeltaTime*_fLinearSpeed;
break;
}
case LOOKING_LEFT: {
/*float fSin = -sin( fDeltaTime*_fAngularSpeed );
float fCos = cos( fDeltaTime*_fAngularSpeed );
_v3LookCenter._fX = _v3Position._fX + (-fSin * v3LookDirection._fZ + fCos * v3LookDirection._fX );
_v3LookCenter._fZ = _v3Position._fZ + ( fCos * v3LookDirection._fZ + fSin * v3LookDirection._fX );
break;*/
// Third Person
float fSin = sin( fDeltaTime*_fAngularSpeed );
float fCos = -cos( fDeltaTime*_fAngularSpeed );
_v3Position._fX = _v3LookCenter._fX + (-fSin * v3LookDirection._fZ + fCos * v3LookDirection._fX );
_v3Position._fZ = _v3LookCenter._fZ + ( fCos * v3LookDirection._fZ + fSin * v3LookDirection._fX );
break;
}
case LOOKING_RIGHT: {
/*float fSin = sin( fDeltaTime*_fAngularSpeed );
float fCos = cos( fDeltaTime*_fAngularSpeed );
_v3LookCenter._fX = _v3Position._fX + (-fSin * v3LookDirection._fZ + fCos * v3LookDirection._fX );
_v3LookCenter._fZ = _v3Position._fZ + ( fCos * v3LookDirection._fZ + fSin * v3LookDirection._fX );
break;*/
// Third Person
float fSin = -sin( fDeltaTime*_fAngularSpeed );
float fCos = -cos( fDeltaTime*_fAngularSpeed );
_v3Position._fX = _v3LookCenter._fX + (-fSin * v3LookDirection._fZ + fCos * v3LookDirection._fX );
_v3Position._fZ = _v3LookCenter._fZ + ( fCos * v3LookDirection._fZ + fSin * v3LookDirection._fX );
break;
}
case LOOKING_UP: {
_v3LookCenter._fY -= fDeltaTime*_fAngularSpeed*_MouseLookState;
// Check Maximum Values
if ( _v3LookCenter._fY > (_v3Position._fY + _fMaxUp ) ) {
_v3LookCenter._fY = _v3Position._fY + _fMaxUp;
} else if ( _v3LookCenter._fY < (_v3Position._fY - _fMaxDown) ) {
_v3LookCenter._fY = _v3Position._fY - _fMaxDown;
}
break;
}
}
}
bool Player::Update() {
// Stripped Down This Deals With Player's Weapons
}
void Player::SetSpeed( float fLinear, float fAngular ) {
_fLinearSpeed = fLinear;
_fAngularSpeed = fAngular;
}
Scene.h - 此处与摄像机相同;有一个玩家对象,而不是指向玩家对象的指针。但是,有一个指向 playerTransform 的指针,它是一个 NodeTransform。由于玩家与场景的交互,这里有太多的功能无法列出,因为这是一个有效的 3D 游戏。我可以提供一些可能感兴趣的功能。
场景.cpp Scene::Update()
// -----------------------------------------------------------------------
// Update
// Animate Objects, Pickup Checks Etc. This All Happens At The
// Physics Refresh Rate
void Scene::Update() {
UserSettings* pUserSettings = UserSettings::Get();
AudioManager* pAudio = AudioManager::GetAudio();
bool bPlayerMoving = false;
// Movement
if ( pUserSettings->IsAction( MOVING_FORWARD ) ) {
_Player.Move( MOVING_FORWARD, GameOGL::GetPhysicsTimeStep() );
bPlayerMoving = true;
}
if ( pUserSettings->IsAction( MOVING_BACK ) ) {
_Player.Move( MOVING_BACK, GameOGL::GetPhysicsTimeStep() );
bPlayerMoving = true;
}
if ( pUserSettings->IsAction( MOVING_LEFT ) ) {
_Player.Move( MOVING_LEFT, GameOGL::GetPhysicsTimeStep() );
bPlayerMoving = true;
}
if ( pUserSettings->IsAction( MOVING_RIGHT ) ) {
_Player.Move( MOVING_RIGHT, GameOGL::GetPhysicsTimeStep() );
bPlayerMoving = true;
}
if ( bPlayerMoving && !_bPlayerWalking ) {
pAudio->SetLooping( AUDIO_FOOTSTEPS, true );
pAudio->Play( AUDIO_FOOTSTEPS );
_bPlayerWalking = true;
}
else if ( !bPlayerMoving && _bPlayerWalking ) {
pAudio->Stop( AUDIO_FOOTSTEPS );
_bPlayerWalking = false;
}
// ... Other Code Here
}
编辑 - 添加 NodeTransform::Render(( - 显示 MVP 的操作顺序
// Move Model View Matrix M = (T C R S C^)
void NodeTransform::RenderOGL( bool bSecondPass, bool bRenderNext ) {
if ( _pIn && _bVisible ) {
// Put Matrix Onto Stack For Later Retrieval
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
if ( _bHaveMatrix ) {
// Use Transformation Matrix
glMultMatrixf( &_f16Matrix[0] );
}
// Transalate
glTranslatef( _v3Translate._fX, _v3Translate._fY, _v3Translate._fZ );
// Move Back To Center
glTranslatef( _v3Center._fX, _v3Center._fY, _v3Center._fZ );
// Rotate
glRotatef( _fRotateAngle, _v3RotateAxis._fX, _v3RotateAxis._fY, _v3RotateAxis._fZ );
// Scale
glScalef( _v3Scale._fX, _v3Scale._fY, _v3Scale._fZ );
// Offset By -ve Center Value
glTranslatef( -_v3Center._fX, -_v3Center._fY, -_v3Center._fZ );
// Move Down The Tree
_pIn->RenderOGL( bSecondPass, true );
// Get Old Matrix
glMatrixMode( GL_MODELVIEW );
glPopMatrix();
}
if ( _pNext && bRenderNext ) {
_pNext->RenderOGL( bSecondPass, true );
}
} // RenderOGL