【问题标题】:XNA 3D Skybox simulation issue with camera position and model translationXNA 3D Skybox 模拟问题与相机位置和模型转换
【发布时间】:2019-04-16 08:50:21
【问题描述】:

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

【问题讨论】:

标签: 3d xna


【解决方案1】:

您正在移动相机,使其在 XZ 平面上绕着世界原点绕一圈,半径为distance,同时始终注视着子弹。

要查看子弹运动,相机必须处于固定位置并旋转。假设初始子弹位置为 (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);
}
`

【讨论】:

  • mVelocity、mAcceleration 和 mDrag 的变量类型应该是什么?向量还是浮点数?位置是 Vector3。如何将模型加载到特定位置?
  • @SSHK 他们是Vector3s。位置需要设置为起始位置。每个模型都将使用自己的世界变换矩阵。
  • 屏幕上看不到任何东西。
  • 对不起,我忘了说你必须将你的运动(重力和阻力)值缩放 1/60。您可能还需要将相机向后移动(取决于视野)。
猜你喜欢
  • 2018-08-23
  • 1970-01-01
  • 1970-01-01
  • 2014-01-14
  • 1970-01-01
  • 1970-01-01
  • 2016-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多