Direct3D9游戏:宇宙飞船相机

问题描述:

我正在研究Direct3D9空间模拟器游戏,其中我需要创建一个包含玩家飞船位置和角度的相机。目前,我限制我的代码只是为了前后移动,上下移动以及扫射。下面是我的代码,它有一个问题。除了LocalUp(D3DXVECTOR3(0.0f,1.0f,0.0f))和LocalAhead(D3DXVECTOR3(0.0f,0.0f,0.0f)),构造函数中的所有矢量都被初始化为D3DXVECTOR3(0.0f, ,0.0f,1.0f)),浮点数设为0.0f;Direct3D9游戏:宇宙飞船相机

D3DXVECTOR3 Position, LookAt ,PosDelta, PosDeltaWorld, WorldAhead, WorldUp, LocalUp, 
LocalAhead, Velocity; 
D3DXMATRIX View, CameraRotation; 
float SpeedX, SpeedY, SpeedZ; 

void Update(float ElapsedTime) 
{ 
    SpeedX = 0.0f; 
    SpeedY = 0.0f; 

    if(IsKeyDown('A')) 
    { 
     SpeedX = -0.02f; 
     Velocity.x -= SpeedX; 
    } 
    if(IsKeyDown('D')) 
    { 
     SpeedX = 0.02f; 
     Velocity.x += SpeedX; 
    } 
    if(IsKeyDown('X')) 
    { 
     SpeedZ += 0.01f; 
     Velocity.z += SpeedZ; 
    } 
    if(IsKeyDown('Z')) 
    { 
     SpeedZ -= 0.01f; 
     Velocity.z -= SpeedZ; 
    } 
    if(IsKeyDown('W')) 
    { 
     SpeedY = 0.02f; 
     Velocity.y += SpeedY; 
    } 
    if(IsKeyDown('S')) 
    { 
     SpeedY = -0.02f; 
     Velocity.y -= SpeedY; 
    } 

    D3DXVec3Normalize(&Velocity, &Velocity); 

    PosDelta.x = Velocity.x * SpeedX; 
    PosDelta.y = Velocity.y * SpeedY; 
    PosDelta.z = Velocity.z * SpeedZ; 

    D3DXMatrixRotationYawPitchRoll(&CameraRotation, 0, 0, 0); 
    D3DXVec3TransformCoord(&WorldUp, &LocalUp, &CameraRotation); 
    D3DXVec3TransformCoord(&WorldAhead, &LocalAhead, &CameraRotation); 
    D3DXVec3TransformCoord(&PosDeltaWorld, &PosDelta, &CameraRotation); 

    Position += PosDeltaWorld; 
    LookAt = Position + WorldAhead; 

    D3DXMatrixLookAtLH(&View, &Position, &LookAt, &WorldUp); 
} 

的 “D3DXMatrixPerspectiveFovLH” 和 “的IDirect3DDevice9 ::一个setTransform” 功能被称为应用程序的另一部分。由于他们工作正常,我不再谈论他们。

问题是,无论Z轴的速度是否相当大,并且我分别或同时横向移动和横向移动,相机的Z轴速度都会降低。此外,在速度几乎为0之后,我按下增加速度的键,矢量的感觉反转,然后恢复正常。当以相当高的速度改变向量的感觉时(例如,按X然后立即按'Z'),也会发生这种情况。有人可以解释我为什么会发生这种情况,我该如何解决这个问题?

我还会问另一个问题:如果没有按键被按下,我怎样才能缓慢减少strafe和Y轴的速度?我想在游戏中实现惯性效果。

如果有人能够帮助我,请回复!

编辑:NEW CODE:

void NewFrontiers3DEntityPlayer::OnFrameUpdate(float ElapsedTime) 
{ 
State.SpeedX = 0.0f; 
State.SpeedY = 0.0f; 
if(IsKeyDown(State.Keys[CAM_STRAFE_LEFT])) 
    State.SpeedX = -0.02f; 
if(IsKeyDown(State.Keys[CAM_STRAFE_RIGHT])) 
    State.SpeedX = 0.02f; 
if(IsKeyDown(State.Keys[CAM_MOVE_FORWARD])) 
{ 
    State.SpeedZ += 0.01f; 
} 
if(IsKeyDown(State.Keys[CAM_MOVE_BACKWARD])) 
{ 
    State.SpeedZ -= 0.01f; 
} 
if(IsKeyDown(State.Keys[CAM_MOVE_UP])) 
    State.SpeedY = 0.02f; 
if(IsKeyDown(State.Keys[CAM_MOVE_DOWN])) 
    State.SpeedY = -0.02f; 

State.Velocity.x = State.SpeedX; 
State.Velocity.y = State.SpeedY; 
State.Velocity.z = State.SpeedZ; 

D3DXVec3Normalize(&State.Velocity, &State.Velocity); 

State.PosDelta.x = State.Velocity.x * ElapsedTime; 
State.PosDelta.y = State.Velocity.y * ElapsedTime; 
State.PosDelta.z = State.Velocity.z * ElapsedTime; 

D3DXMatrixRotationYawPitchRoll(&State.CameraRotation, 0, 0, 0); 
D3DXVec3TransformCoord(&State.WorldUp, &State.LocalUp, &State.CameraRotation); 
D3DXVec3TransformCoord(&State.WorldAhead, &State.LocalAhead, &State.CameraRotation); 
D3DXVec3TransformCoord(&State.PosDeltaWorld, &State.PosDelta, &State.CameraRotation); 

State.Position += State.PosDeltaWorld; 
State.LookAt = State.Position + State.WorldAhead; 

D3DXMatrixLookAtLH(&State.View, &State.Position, &State.LookAt, &State.WorldUp); 

return; 
} 

“国家” 是保存所有关于相机信息的结构。

+0

由于您使用C++我会推荐你使用类而不是全局变量 – Quest 2014-09-13 16:41:16

+0

我正在使用类,我使用全局变量,因为在这里发布会更容易。 – 2014-09-13 16:52:08

我想你的速度会随着你一次向多个方向移动而改变,因为你正常化了你的速度。

例如,在Z移动:

Velocity = (0, 0, 0.01) 
Speed = (0,0, 0.01) 
Normalized Velocity = (0, 0, 1) 
PosDelta = (0, 0, 0.01) 

和X + Z移动:

Velocity = (0.02, 0, 0.01) 
Speed = (0.02, 0, 0.01) 
Normalized Velocity = (0.897, 0, 0.435) 
PosDelta = (0.018, 0, 0.0044)  

关于你的方向反转我猜测它可部分地与您的相对使用速度/速度的奇怪方法(见下面更多内容),也可能是由于浮点数的精度所致。关于最后一点您怎么看下面的代码输出(忽略潜在的编译器优化):

float test1 = 0f; 

test1 += 0.1f; 
test1 += 0.1f; 
test1 += 0.1f; 
test1 += 0.1f; 
test1 += 0.1f; 

test1 -= 0.1f; 
test1 -= 0.1f; 
test1 -= 0.1f; 
test1 -= 0.1f; 
test1 -= 0.1f; 

printf("%g\n", test1); 

它(?可能)不会输出,因为0.10答案显然不能完全以2为底的代表。它在我的系统上打印1.49012e-008。可能发生的情况是您可能接近0,但不完全可能导致坐标反演出现。你可以通过舍入速度来达到一定的精度。

您处理速度/速度/位置的整体方法有点奇怪,可能是您遇到困难的根源。例如,我认为Velocity是一个代表玩家速度的向量,而不是像你那样的归一化向量。我会做这样的事情:

SpeedX = 0.0f; 
SpeedY = 0.0f; 
SpeedZ = 0.0f 

if(IsKeyDown('A')) SpeedX += -0.02f; 
if(IsKeyDown('D')) SpeedX += 0.02f; 
... 

Velocity.x += SpeedX; 
Velocity.y += SpeedY; 
Velocity.z += SpeedZ; 

D3DXVECTOR3 NormVelocity; 
D3DXVec3Normalize(&NormVelocity, &Velocity); 

//Round velocity here if you need to 
Velocity.x = floor(Velocity.x * 10000)/10000.0f; 
... 

float FrameTime = 1; //Use last frame time here 

PosDelta.x = Velocity.x * FrameTime; 
PosDelta.y = Velocity.y * FrameTime; 
PosDelta.z = Velocity.z * FrameTime; 

这摆脱了你的速度变化时,在多个方向移动。如果将FrameTime设置为最后一帧的时间(或从中导出的值),它还可以让您正确地补偿更改的帧速率。当他们试图一次往两个相反的方向移动时,它也能正确地阻止玩家移动。

至于你最后一个关于Y速度衰减的问题,有几种方法可以做到这一点。你可以简单地做这样的事情:

Velocity.y *= 0.7f; 

每帧(调整常量,以满足您的需求)。更准确的模型会做这样的事情:

if (Velocity.y > 0) { 
    Velocity.y -= 0.001f; //Pick constant to suit 
    if (Velocity.y < 0) Velocity.y = 0; 
} 

一个更好的方法是使用最后一帧的时间来考虑不同的像帧速率:

Velocity.y -= FrameTime * 0.2f; //Pick constant to suit 
+0

感谢您的回复和帮助,但我仍然有问题。我现在有一段代码,我将很快通过编辑发布原始问题。问题是无论我按什么键(我在谈论X轴速度),可见速度都将保持不变。你可以帮帮我吗? – 2014-09-22 17:53:47

+0

没关系,我忘了在编写新代码时添加'+'符号...谢谢您解决我的问题! – 2014-09-29 06:46:18