我正在尝试创造的是一个火箭,它将沿直线方向拥抱轨道。ie) 火箭沿直线方向行进,并且可以根据其局部 x 轴进行定向。这样它就可以上/下坡道而永远不会撞到地面。
目前我正在使用 PhysX opengl 和 C++。
这是我现在正在尝试的方法: 1. 从导弹前方投射光线(向下投射光线) 2. 如果光线投射小于预期的光线投射长度,那么我必须向上定向。3. 如果光线投射大于预期的光线投射长度,那么我必须向下定向。
现在我遇到的问题是我的导弹以任意角度定向(我给它 1 度。)虽然我认为这是一种不好的方法,因为游戏中的帧数没有我想的那么多认为会有。所以火箭会撞上坡道。
我的主要问题是:有没有更好的方法来解决这个问题以及如何解决?
NxVec3 frontRayLoc = m_rocketConfig->getValueForKey<NxVec3>("r_frontRayCastLocation");
float threshhold = m_rocketConfig->getValueForKey<float>("r_angleThreshhold");
float predRayCastHeight = m_rocketConfig->getValueForKey<float>("r_predRayCastHeight");
NxVec3 rayGlobalPos_1 = m_actor->getGlobalPosition() + m_actor->getGlobalOrientation() * frontRayLoc;
NxVec3 dir = m_actor->getGlobalOrientation() * NxVec3(0,-1.0,0);
NxReal dist1 = castRay(rayGlobalPos_1, dir);
// Get the percentage difference
float actualFrontHeight = abs(1 - (dist1/predRayCastHeight));
// See if the percentage difference is greater then threshold
// Also check if we are being shot off track
if ((actualFrontHeight > threshhold) && (dist1 != m_rayMaxDist)){
// Dip Down
if (dist1 > predRayCastHeight){
printf("DOWN - Distance 1: %f\n", dist1);
// Get axis of rotation
NxVec3 newAxis = m_actor->getGlobalOrientation() * NxVec3(1.0,0,0.0);
// Rotate based on that axis
m_orientateAngle = -1.0 * m_orientateAngle; // For rotating clockwise
NxQuat newOrientation(m_orientateAngle, newAxis);
NxMat33 orientation(newOrientation);
m_orientation = m_orientation * orientation;
// Orientate the linear velocity to keep speed of rocket and direct away from road
NxVec3 linVel = m_actor->getLinearVelocity();
m_actor->setLinearVelocity(m_orientation * linVel);
}
// Go Up
else if (dist1 < predRayCastHeight){
printf("UP - Distance 1: %f\n", dist1);
// Get axis of rotation
NxVec3 newAxis = m_actor->getGlobalOrientation() * NxVec3(1.0,0,0.0);
// Rotate around axis
NxQuat newOrientation(m_orientateAngle, newAxis);
m_actor->setGlobalOrientationQuat(newOrientation);
NxMat33 orientation(newOrientation);
m_orientation = m_orientation * orientation;
// Orientate the linear velocity to keep speed of rocket and direct away from road
NxVec3 linVel = m_actor->getLinearVelocity();
m_actor->setLinearVelocity(m_orientation*linVel);
}
m_actor->setGlobalOrientation(m_orientation);
}
感谢您的支持 :)