XNA 3D Skybox模拟问题,包括相机位置和模型转换

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了XNA 3D Skybox模拟问题,包括相机位置和模型转换相关的知识,希望对你有一定的参考价值。

我在VS 2015社区中创建了一个XNA 4.0 3D项目。它有一个带有子弹模型的Skybox,我试图模拟轨迹。我似乎无法将相机放在正确的位置,并且转换似乎在错误的轴上工作。

我试过改变模型,相机甚至透视的位置。

protected override void Update(GameTime gameTime)
{
    // Allows the game to exit
    if (GamePad.GetState(PlayerIndex.One).Buttons.Back == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
        this.Exit();

    mDeltaTime = gameTime;
    mFlightTime += 0.1f;


    //if (mAngleInput != 0)
    //{
    //    mVelocity.X = (float)(mVelocityInput * Math.Cos(DegreeToRadian(mAngleInput)));
    //    mVelocity.Y = (float)(mVelocityInput * Math.Sin(DegreeToRadian(mAngleInput)));
    //}

    position = (mStartingPosition + mVelocity * mFlightTime) - (0.5f * mAcceleration * (float)Math.Pow(mFlightTime, 2)) / 5;


    // This updates the world matrix, so that it reflects the changes to the position
    // and angle.  Remember that this matrix determines where the model will be located
    // at in the 3D world.

    //   camTarget += new Vector3(0.1f,0,0);

    //           world = Matrix.CreateRotationY(angle) * Matrix.CreateTranslation(position);
    //            view = Matrix.CreateLookAt(camTarget, position,  Vector3.UnitY);

    cameraPosition =  (distance * new Vector3((float)Math.Sin(angle), 0, (float)Math.Cos(angle)));
    Vector3 cameraTarget = position;
    //original Vector3 cameraTarget = new Vector3(0, 0, 0);

    viewVector = Vector3.Transform(cameraTarget - cameraPosition, Matrix.CreateRotationY(0));
    // viewVector.Normalize();

    angle += 0.002f;

    world = Matrix.CreateScale(0.5f) * Matrix.CreateRotationY(angle) * Matrix.CreateTranslation(position);
    view = Matrix.CreateLookAt(cameraTarget, cameraPosition, Vector3.UnitY);
    //original view = Matrix.CreateLookAt(cameraPosition, new Vector3(0, 0, 0), Vector3.UnitY);

    base.Update(gameTime);
}

我希望模型将在某个位置开始并移动水平X轴并根据我正在使用的轨迹公式下降垂直Y轴。一旦达到某个Y值就停止。

答案

您正在移动相机,以便它在半径为distance的圆形环绕XZ平面上的世界原点旋转,同时始终查看子弹。

要查看子弹运动,摄像机必须处于固定位置并旋转。假设初始子弹位置为(0,0,0),您的相机需要在Z轴上偏移,以防止LookAt矢量退化为NaN

您可以移动相机以跟随X轴上的子弹,但它会从屏幕中间开始直接向下移动。

Protected override void Update(GameTime gameTime)
{

    //mDeltaTime = gameTime;  
    mFlightTime += 0.1f;

    //position = (mStartingPosition + mVelocity * mFlightTime) - (0.5f * mAcceleration * (float)Math.Pow(mFlightTime, 2)) / 5;
    // The previous line can be simplified, assuming you do not need to jump to a specific time:
    position += mVelocity;
    mVelocity +=mAcceleration;
    mAcceleration += mDrag;
    mDrag = -bulletCoefficient * airDensity * mBulletCrossSection * (float)Math.Pow(mVelocity, 2) / 2; 

    cameraTarget = new Vector3(0,0,0);
    cameraPosition = new Vector3(0,0,-1);   

    world = Matrix.CreateScale(0.5f) * Matrix.CreateRotationY(angle) * Matrix.CreateTranslation(position);
    view = Matrix.CreateLookAt(cameraTarget, cameraPosition, Vector3.UnitY);    

    base.Update(gameTime);
}
`

以上是关于XNA 3D Skybox模拟问题,包括相机位置和模型转换的主要内容,如果未能解决你的问题,请参考以下文章

XNA (MonoGame) 世界空间和物体运动

OpenGL/GLSL/GLM - Skybox 像第三人称一样旋转

根据 3D 对象的位置旋转相机

C#XNA鼠标的位置投射到3D平面

Unity3D中Lens Flare 如何添加在skybox中的太阳旁边?

Unity3D相机