1

3D ワールド内のオブジェクトの背後にカメラを移動するのに苦労しています。2 つのビュー モードを作成します。

1: fps (一人称)。2nd:キャラクターの後ろの外観(二人称)。

ネットでいくつかの例を検索しましたが、私のプロジェクトでは機能しません。

F2が押された場合にビューを変更するために使用される私のコードは次のとおりです

 //Camera
        double X1 = this.camera.PositionX;
        double X2 = this.player.Position.X;
        double Z1 = this.camera.PositionZ;
        double Z2 = this.player.Position.Z;


        //Verify that the user must not let the press F2
        if (!this.camera.IsF2TurnedInBoucle)
        {
            // If the view mode is the second person
            if (this.camera.ViewCamera_type == CameraSimples.ChangeView.SecondPerson)
            {
                this.camera.ViewCamera_type = CameraSimples.ChangeView.firstPerson;

                //Calcul position - ?? Here my problem
                double direction = Math.Atan2(X2 - X1, Z2 - Z1) * 180.0 / 3.14159265;
                //Calcul angle - ?? Here my problem

                this.camera.position = ..
                this.camera.rotation = ..

                this.camera.MouseRadian_LeftrightRot = (float)direction;
            }
            //IF mode view is first person
            else
            {
                    //....
4

2 に答える 2

1

これは、Xna の非常に基本的な 3 人称カメラ (2 人称の意味) です。プレイヤーのワールド マトリックスが保存されていて、それにアクセスできることを前提としています。

Vector3 _3rdPersonCamPosition = playerWorldMatrix.Translation + (playerWorldMatrix.Backward * trailingDistance) + (playerWorldMatrix.Up * heightOffset);// add a right or left offset if desired too
Vector3 _3rdPersonCamTarget = playerWorldMatrix.Translation;//you can offset this similarly too if desired
view = Matrix.CreateLookAt(_3rdPersonCamPosition, _3rdPersonCamTarget , Vector3.Up);

FPS カメラが適切に機能していて、基本的にプレイヤーと同じ位置と向きであると想定している場合は、上記の playerWorldMatrix の代わりにビュー マトリックスを次のように置き換えることができます。

Matrix FPSCamWorld = Matrix.Invert(yourWorkingFPSviewMatrixHere);

今私が書いたところならどこでも代わりにplayerWorldMatrix使うことができますFPSCamWorld

于 2012-12-08T18:39:35.243 に答える
1

私があなただったら、あなたの現在動作している FPS カメラ (適切に動く、位置マトリックスなどがあると思いますか?) を取り、それに別の Translation Transform を追加して、プレーヤーの後ろに「移動」させます。

別の言い方をすれば:

FPS ビューの「変換/ビュー マトリックス」が次のような場合:

(申し訳ありませんが、しばらく XNA で遊んでいないので、適切なクラス名を覚えていません)

var camTranslateMatrix = [matrix representing player position];
var camDirectionMatrix = [matrix representing player direction, etc];
var camViewMatrix = camTranslateMatrix * camDirectionMatrix;

次に、次のように変更できます。

var camTranslateMatrix = [matrix representing player position];
var camDirectionMatrix = [matrix representing player direction, etc];

// If not in 3rd person, this will be identity == no effect
var camThirdPersonMatrix =
       IsInThirdPersonMode ?
             new TranslateMatrix(back a bit and up a bit) :
             IdentityMatrix();

var camViewMatrix = 
      camTranslateMatrix * 
      camDirectionMatrix * 
      camThirdPersonMatrix;

わかる?そうすれば、毎回面倒な計算をせずに 2 つのビューを簡単に切り替えることができます。

于 2012-12-07T22:02:28.413 に答える