XNA 3D Skybox仿真问题,涉及相机位置和模型平移

时间:2019-04-16 08:50:22

标签: 3d xna

我已经在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值就停止。

1 个答案:

答案 0 :(得分:0)

您正在移动相机,以使其始终沿XZ平面在半径为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);
}
`