gpt4 book ai didi

java - 修复了 3D 相机在它面对的方向上移动的问题?

转载 作者:搜寻专家 更新时间:2023-10-30 20:54:16 25 4
gpt4 key购买 nike

简短版本 (TL;DR)

我有一个 Camera附在 SceneNode只要 SceneNode,运动就可以正常工作的旋转/轴与世界对齐。然而,当一个对象旋转到不同方向“看”并被告知“向前”移动时,它不会沿着新的“向前”方向移动。相反,它会继续沿应用旋转之前所面向的相同方向移动。

详细信息和示例

我有一个场景图来管理 3D 场景。该图是SceneNode的树对象,它们知道它们相对于它们的父对象和世界的变换。

根据 TL; DR;片段,假设您有一个 cameraNode零旋转(例如面向北),然后旋转 cameraNode围绕 +Y“向上”轴向左 90 度,即使其向西看。目前一切正常。如果您现在尝试移动 cameraNode “前进”,即现在向西,cameraNode而是移动,好像“前进”仍然面向北方。

简而言之,它的移动就好像它从来没有旋转过一样。

下面的代码显示了我最近尝试的内容以及我(当前)在缩小最有可能与问题相关的领域的最佳猜测。

相关SceneNode成员(member)
SceneNode implementation 具有以下字段(仅显示与此问题相关的字段):

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没有自己的位置/旋转信息,客户端只需处理 SceneNode Camera已附加,仅此而已。

除了这个问题中提到的问题,其他一切似乎都按预期工作。
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 矩阵对象中。请注意,在测试过程中,您可以看到场景围绕相机旋转,因此确实应用了旋转,这让我更加困惑这个问题。

方向 vector

方向 vector 只是取自派生的 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 s 可以考虑其父级的转换。

其他/相关问题

在发布此问题之前的最后大约 3 周内,我已经在 SO 内外进行了多个答案(例如 herehereherehere 等)。显然,虽然相关,但它们对我的情况确实没有帮助。

对评论中问题的回答

Are you sure that when computing derivedTransform your parent's derivedTransform is already computed?



是的,父级 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;
}

这一段调用了上一节中的私有(private)方法。

最佳答案

这并不是直接的答案,而是应 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

Camera.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 );
}

CameraRender使用旧的 OpenGL API 调用的函数首先加载到 Modelview 矩阵中,然后加载单位矩阵;然后我们最终使用 glu 的 gluLookAt(...) 方法来设置所需 vector 的位置。

场景.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类以及场景的场景图层次结构,我们使用 Camera在现场的 Render称呼。这里我们设置了 Camera通过获取 Player的当前位置,以及 Player's LookCenter方向。

编辑 - 为移动计算添加玩家类和相关代码
enum Action {
NO_ACTION = -1,
MOVING_FORWARD = 0,
MOVING_BACK,
MOVING_LEFT,
MOVING_RIGHT,
LOOKING_LEFT,
LOOKING_RIGHT,
LOOKING_UP,
LOOKING_DOWN,
}; // Action

Player.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;
}

场景.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

关于java - 修复了 3D 相机在它面对的方向上移动的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38825659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com