火箭跟随轨道高度。不是寻的导弹

发布于 2024-08-24 14:12:06 字数 2625 浏览 5 评论 0原文

我想要创造的是一个能够沿着直线方向拥抱轨道的火箭。 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);
}

感谢您的支持:)

What I am trying to create is a rocket that will hug the track in a straight direction.
ie) The rocket travels in a straight direction and can orientate based on its local x axis. This is so it can go up/down ramps and never hit the ground.

Currently I am using PhysX opengl and C++.

This is the method I'm trying right now:
1. Ray cast from ahead of the missile (ray casting downwards)
2. If the ray cast is less then the expected ray cast length, then I have to orientate up.
3. If the ray cast is more then the expected ray cast length, then I have to orientate down.

Now the problem, I am having is that my missile is orientating at an arbitary angle (I'm giving it 1 degrees.) Though I think this is a bad approach because the amount of frames in the game is not as much as I would think there would be. So the rocket would run into a ramp.

My main question is: is there a better way of approaching this and how?

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

Thanks for the support :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽清梦 2024-08-31 14:12:06

如果您的光线追踪可以确定前方某个点的地形高度,那么为什么不能直接确定火箭当前水平坐标处的地形高度,并在高于该高度的固定高度处渲染火箭呢?

也就是说,您似乎正在尝试为火箭发明一个制导系统,而听起来您真正需要的只是弄清楚在哪里绘制它。

实际上,您可能可以通过使其与下方地形的坡度相匹配来获得火箭的方向,这样它就不会始终显得水平。如果在明显的斜坡上行驶时保持水平,看起来会有点奇怪。

If your ray trace can determine the height of the terrain at some point out ahead, why couldn't you just determine the height of the terrain at the current horizontal coordinates of the rocket, and render the rocket at a fixed height above that?

I.e., you seem to be trying to invent a guidance system for the rocket, when it sounds like all you really need is to figure out where to draw it.

Actually, you probably could get the orientation for the rocket by making it match the slope of the terrain underneath it, so that it doesn't appear dead level all the time. It would look sort of strange if it were level while tracking over noticeable slopes.

泪痕残 2024-08-31 14:12:06

不如像军队那样进行地形跟踪:

向前看距离,找到飞行器和 之间的最高地形。将所需的离地高度加上该值,就得到了火箭应达到的高度。如果低于此爬升,如果高于此下降。

选择以获得火箭所需的行为。它可能非常小。

很有可能预先计算高度,从而减少到简单的数组查找。 (即使您拥有接近无限分辨率的地形,您也不需要高度数据的完美粒度。)

How about doing it the way the military does with terrain following:

Look ahead distance and find the highest terrain between the craft and . Add the desired height above ground to this value and you have the altitude the rocket should be at. If it's below this climb, if it's above this descend.

Choose to get the desired behavior of the rocket. It can probably be pretty small.

There's a very good chance the heights can be precalculated and thus this reduces to a simple array lookup. (Even if you have near-infinite resolution terrain you don't need perfect granularity in the height data.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文