gpt4 book ai didi

c++ - 除非旋转为 0 度或 180 度,否则物体不会朝它所面对的方向移动

转载 作者:行者123 更新时间:2023-11-28 08:08:49 26 4
gpt4 key购买 nike

正如标题所说,当面向任何非 0 度或 180 度的方向时,对象不会正确移动。这是在 3D 空间中,但旋转仅在 1 个轴 (UP) 上,因此对象左右转动。这是一个有助于形象化问题的绘画图:http://dl.dropbox.com/u/60309894/rotationissue.png在按住鼠标右键的同时,对象会根据鼠标的 x 增量位置进行旋转。这是代码:

// Check & calculate rotation.
if (mouse->ButtonIsDown(NiInputMouse::NIM_RIGHT))
{
int iDeltaX = 0, iDeltaY = 0, iDeltaZ = 0;
mouse->GetPositionDelta(iDeltaX,iDeltaY,iDeltaZ);
if (iDeltaX != 0)
{
NiMatrix3 mMat;
mMat.MakeRotation(iDeltaX / 100.0f,NiPoint3::UNIT_Z);
SetRotate( GetRotate() * mMat );
}
}

// Check & calculate movement.
m_vVelocity = NiPoint3::ZERO;
if ( keyboard->KeyIsDown(NiInputKeyboard::KEY_W) == true)
m_vVelocity.y++;
if ( keyboard->KeyIsDown(NiInputKeyboard::KEY_S) == true)
m_vVelocity.y-- ;
if ( keyboard->KeyIsDown(NiInputKeyboard::KEY_A) == true)
m_vVelocity.x--;
if ( keyboard->KeyIsDown(NiInputKeyboard::KEY_D) == true)
m_vVelocity.x++;
m_vVelocity.Unitize();

// Move the object.
m_spNode->SetTranslate(GetTranslate() + m_vVelocity * GetRotate() * m_fSpeed * dt );

最佳答案

假设 X 在示例图像中是左和右,它看起来只是你的 x 速度被否定了。如果这是真的,那么交换左右方向,或者取反 x 速度,应该可以解决这个问题:

if ( keyboard->KeyIsDown(NiInputKeyboard::KEY_A) == true)
m_vVelocity.x++;
if ( keyboard->KeyIsDown(NiInputKeyboard::KEY_D) == true)
m_vVelocity.x--;

假设 z 在监视器之外,这将对应于左手坐标系,而不是原始代码中的右手坐标系。

关于c++ - 除非旋转为 0 度或 180 度,否则物体不会朝它所面对的方向移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9613259/

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