旋转一个对象面对另一个C
Rotating one object to face another C++
我正在尝试旋转一个对象(汽车)以面对一个对象,但是我对此有很多困难。
我在此代码方面取得了不同的成功,它知道角度何时正确,并且何时会面对正确的方式,但大多数情况下它会由于符号值而迅速陷入困境1> -1或从-1> 1每个框架,从而导致所有旋转停止,并使汽车不受控制地抽搐,从不真正纠正自己。
老实说,我不确定我的符号计算是否在使用X/Z轴上移动时使用正确的值,但在Y轴上旋转以向左/右转
我正在使用c 和directx11
任何建议/帮助将不胜感激,谢谢!
bool ParticleModel::FaceTarget(XMFLOAT3 target)
{
XMFLOAT3 toNormalize;
toNormalize.x = transform->GetPosition().x - target.x;
toNormalize.y = transform->GetPosition().y - target.y;
toNormalize.z = transform->GetPosition().z - target.z;
XMFLOAT3 toTarget = NormalizeVector(toNormalize);
//Determine the angle between the heading vector and the target.
XMFLOAT3 carHeading;
carHeading.x = sin(transform->GetRotation().y);
carHeading.y = sin(transform->GetRotation().x);
carHeading.z = cos(transform->GetRotation().y);
carHeading = NormalizeVector(carHeading);
double angle = acos(DotProduct(carHeading, toTarget));
//Return true if the player is facing the target.
if (angle < 0.01000)
{
return true;
}
/*if (signTimer < signCooldown)
{
signTimer++;
}
else if (signTimer >= signCooldown)
{
signTimer = 0;
sign = GetSign(carHeading, toTarget);
}*/
sign = GetSign(carHeading, toTarget);
transform->SetYRot(transform->GetRotation().y + (0.030f*sign));
//bool thing = false;
//RotateHeadingByRadian(angle, mHeading.Sign(toTarget));
return true;
}
float ParticleModel::DotProduct(XMFLOAT3 a, XMFLOAT3 b)
{
float dot;
dot = (a.x * b.x) + (a.y * b.y) + (a.z * b.z);
return dot;
}
int ParticleModel::GetSign(XMFLOAT3 v1, XMFLOAT3 v2)
{
if (v1.y*v2.z > v1.z*v2.y)
{
return -1;
}
else
{
return 1;
}
}
没关系,我发现了如何做,从本质上讲,这是代码
XMFLOAT3 distance;
distance.x = transform->GetPosition().x - target.x;
distance.y = transform->GetPosition().y - target.y;
distance.z = transform->GetPosition().z - target.z;
XMFLOAT3 carHeading;
carHeading.x = sin(transform->GetRotation().y);
carHeading.y = sin(transform->GetRotation().x);
carHeading.z = cos(transform->GetRotation().y);
if (VectorLength(distance) < 0.5)
{
return true;
}
//Don't actually need to call normalize for directionA - just doing it to indicate
//that this vector must be normalized.
XMFLOAT3 directionA = carHeading;
XMFLOAT3 directionB = NormalizeVector(distance);
float rotationAngle = (float)acos(DotProduct(directionA, directionB));
if (abs(rotationAngle) < 0.5)
{
return true;
}
XMFLOAT3 rotationAxis = CrossProduct(directionA, directionB);
rotationAxis = NormalizeVector(rotationAxis);
transform->SetYRot(transform->GetRotation().y + (rotationAngle*rotationAxis.y));
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 运行同一解决方案的另一个项目的项目
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- C++从另一个类访问公共静态向量的正确方法是什么
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 使用std::transform将一个范围的元素添加到另一个范围中
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 我可以将一个用clang c++11编译的对象与另一个用c++17编译的对象链接起来吗
- 修改函数中的指针(将另一个指针作为参数传递)
- 为什么我不能将一个对象push_back到属于另一个类的对象向量中?
- C++试图读取一个文件并输出到另一个文本文件
- 如何将指针从一个void函数传递到另一个C++
- 如何从另一个文件继承私有成员变量和公共函数
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 在他自己的方法中,有可能将一个对象取消引用到另一个对象吗
- C++-我可以创建另一个类的成员并在构造函数中使用它吗
- 旋转一个对象面对另一个C
- SFML 2.1如何使一个精灵面对另一个精灵