1

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();
        }
    }
iSteffi
  • 178
  • 2
  • 11
  • Is it `effect.View = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up);` this that is causing the problem. Shouldn't be `modelPosition` and not `cameraPosition`? – Neil Knight Jul 19 '11 at 17:38
  • hmm.. no i don' t think so: first Vector should be the camera position and the second one the target position.. (see the reference) – iSteffi Jul 19 '11 at 17:42
  • And I tried it out. The result is, that the pizza vanishes – iSteffi Jul 19 '11 at 17:43
  • Do you mean my whole project?? – iSteffi Jul 20 '11 at 09:47
  • http://home.htw-berlin.de/~s0524591/Game.rar – iSteffi Jul 20 '11 at 10:35
  • I did some changes: Matrix.CreateLookAt(cameraPosition, Vector3.Forward, Vector3.Up); But it didnt work anyway :( – iSteffi Jul 20 '11 at 10:36
  • Because you are using 2 cameras, in effect, it's not going to be easy to sort out. I have got the pizza in position, but its relative to screenspace and not world space.`Matrix world = Matrix.CreateScale(2f) * Matrix.CreateTranslation(modelPosition); effect.World = transforms[mesh.ParentBone.Index] * world; effect.View = Matrix.CreateLookAt(cameraPosition, Vector3.Forward, Vector3.Up);` – Neil Knight Jul 20 '11 at 11:52
  • Did you use that code only in Pizza?? Because the only effect is, that the pizza becomes scaled. But it is still moving relative to the player – iSteffi Jul 21 '11 at 09:17
  • I only used that on the `Pizza` code. – Neil Knight Jul 21 '11 at 10:16
  • And that worked for you?? Because.. in my project.. my Pizza is still moving with the player.. hmm.. wait I will update the link okay.. (with my newer version of the game) – iSteffi Jul 21 '11 at 10:36
  • I uploaded the newest version.. You'll see that my Pizza ist still moving with the player – iSteffi Jul 21 '11 at 10:45
  • yes it is: home.htw-berlin.de/~s0524591/Game.rar – iSteffi Jul 21 '11 at 11:04

0 Answers0