XNA:2.5D 游戏,如《马里奥兄弟 Wii》

发布于 2024-11-25 13:56:01 字数 3172 浏览 0 评论 0原文

我确实需要一些帮助!

我想要什么:

我正在尝试实现一款像 Mario Bros Wii 这样的 2.5D 游戏。 即:2D 背景从右到左滚动,反之亦然,带有 3D 播放器和 3D 对象。

我拥有的:

我已经有了一个用图块构建的 2D 环境(请参阅 xna 的平台游戏示例)。我在其中插入了一个 3D 模型,它可以移动并与世界交互。碰撞检测和一切......都很好!

我的问题是什么:

我的问题是相机随着播放器移动......这实际上没问题,也是我想要的。我确实是这样实现的:Camera.Position = modelPosition。 但我不想的是它会导致玩家相机和所有其他对象相机之间的关系。 结果是所有其他对象都随着玩家移动。但它们应该留在原地,并且只能被玩家通过(或收集)。

你知道我的意思吗?

我的假设是什么:

玩家沿着 2D 坐标系移动,相机位置将设置为玩家位置。

如果我也放入另一个对象(在草图中:o)(就像某个东西)收集)它将位于 3D 坐标系中,该 3D 坐标系位于该 2D 坐标系中。 (参见草图)因此该对象的相机随实际相机一起移动。但位置仍然是 (0, 0, 0)..

我能做什么?

示例 玩家&对象:

 o                    <>
/|\
 |
/ \

如果我移动玩家,该对象就会相对于它移动。

我的代码是什么:

玩家:

public void DrawPlayer()
    {
        Matrix[] bones = animationPlayer.GetSkinTransforms();

        if (playerDirection == Direction.Left)
            modelRotation = Math.Abs(modelRotation);
        else
            modelRotation = Math.Abs(modelRotation) * -1;

        Matrix view = Matrix.CreateRotationY(MathHelper.ToRadians(modelRotation)) *
                      Matrix.CreateRotationX(MathHelper.ToRadians(0)) *
                      Matrix.CreateTranslation(modelPosition) *
                      Matrix.CreateLookAt(cameraPosition, modelPosition, Vector3.Up);

        Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), aspectRatio, 1.0f, 10000.0f);
        foreach (ModelMesh mesh in player.Meshes)
        {
            foreach (Effect effect in mesh.Effects)
            {
                effect.Parameters["Bones"].SetValue(bones);
                effect.Parameters["View"].SetValue(view);
                effect.Parameters["Projection"].SetValue(projection);
            }
            mesh.Draw();
        }
    }

相机:

public void Update(GameTime gameTime)
    {
        position.X = -Player.ModelPosition.X;
        position.Y = Player.ModelPosition.Y;
    }

披萨(对象):

public void Draw(GameTime gameTime)
    {
        Matrix[] transforms = new Matrix[pizza.Bones.Count];
        pizza.CopyAbsoluteBoneTransformsTo(transforms);

        //float temp = Camera.Position.X / (1280 / 2);
        //CamTranslation = Matrix.CreateTranslation(new Vector3(xpos, 0, 0));

        foreach (ModelMesh mesh in pizza.Meshes)
        {
            foreach (BasicEffect effect in mesh.Effects)
            {
                effect.EnableDefaultLighting();

                effect.World = transforms[mesh.ParentBone.Index] *
                    Matrix.CreateRotationY(modelRotation)
                    * Matrix.CreateTranslation(modelPosition);

                effect.View = Matrix.CreateLookAt(cameraPosition,
                    Vector3.Zero, Vector3.Up);

                effect.Projection = Matrix.CreatePerspectiveFieldOfView(
                    MathHelper.ToRadians(45.0f), aspectRatio,
                    1.0f, 10000.0f);
            }
            mesh.Draw();
        }
    }

I definitely do need some help!!

What I want:

I am trying to implement a 2.5D game like Mario Bros Wii.
That is: A 2D Background scrolling from right to left and vice versa with a 3D player and 3D objects.

What I have:

I already have a 2D environment build up with tiles (see platformer example game of xna). In that I inserted a 3D Model, that moves and interacts with the world. Collision Detection and everything.. thats all fine!

What is my problem:

My problem is that the camera moves with the Player.. that is actually okay and what I wanted to. I did implemented it this way: Camera.Position = modelPosition.
But what I dont want to is that it results in a relationship between the player camera and all the other object cameras.
The result is that all the other objects move with the player. BUT they should stay in place and should only be passed by the player (or collected)..

Do you know what I mean??

What is my assumption:

The player moves along that 2D coordinate system and the camera position will be set to the player position..

If I put in another object (in sketch: o) as well (like something to collect) it will sit in a 3D coordinate system that sits in that 2D coordinate system. (see sketch) So the camera of that object moves with the actual camera. But never the less the position is (0, 0, 0)..

What can I do??

Example
Player & Object:

 o                    <>
/|\
 |
/ \

If I move the player the object moves relative to it.

What is my code:

Player:

public void DrawPlayer()
    {
        Matrix[] bones = animationPlayer.GetSkinTransforms();

        if (playerDirection == Direction.Left)
            modelRotation = Math.Abs(modelRotation);
        else
            modelRotation = Math.Abs(modelRotation) * -1;

        Matrix view = Matrix.CreateRotationY(MathHelper.ToRadians(modelRotation)) *
                      Matrix.CreateRotationX(MathHelper.ToRadians(0)) *
                      Matrix.CreateTranslation(modelPosition) *
                      Matrix.CreateLookAt(cameraPosition, modelPosition, Vector3.Up);

        Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), aspectRatio, 1.0f, 10000.0f);
        foreach (ModelMesh mesh in player.Meshes)
        {
            foreach (Effect effect in mesh.Effects)
            {
                effect.Parameters["Bones"].SetValue(bones);
                effect.Parameters["View"].SetValue(view);
                effect.Parameters["Projection"].SetValue(projection);
            }
            mesh.Draw();
        }
    }

Camera:

public void Update(GameTime gameTime)
    {
        position.X = -Player.ModelPosition.X;
        position.Y = Player.ModelPosition.Y;
    }

Pizza (Object):

public void Draw(GameTime gameTime)
    {
        Matrix[] transforms = new Matrix[pizza.Bones.Count];
        pizza.CopyAbsoluteBoneTransformsTo(transforms);

        //float temp = Camera.Position.X / (1280 / 2);
        //CamTranslation = Matrix.CreateTranslation(new Vector3(xpos, 0, 0));

        foreach (ModelMesh mesh in pizza.Meshes)
        {
            foreach (BasicEffect effect in mesh.Effects)
            {
                effect.EnableDefaultLighting();

                effect.World = transforms[mesh.ParentBone.Index] *
                    Matrix.CreateRotationY(modelRotation)
                    * Matrix.CreateTranslation(modelPosition);

                effect.View = Matrix.CreateLookAt(cameraPosition,
                    Vector3.Zero, Vector3.Up);

                effect.Projection = Matrix.CreatePerspectiveFieldOfView(
                    MathHelper.ToRadians(45.0f), aspectRatio,
                    1.0f, 10000.0f);
            }
            mesh.Draw();
        }
    }

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文