• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
ChristianFrantz

Creating and moving a third person camera

5 posts in this topic

I have a player class and camera class. I want my model to move and the camera to follow it. Simple enough, but when I move my model it decides to fly off the screen and then stops somewhere far in the distance, and then moves up and down on the Y axis. I'm not exactly sure where I went wrong with this one. The camera also doesn't move either, but that's not as much of an issue right now.

class Player
    {
        Model playerModel;
        public Matrix playerWorld;
        Matrix[] playerTransforms;

        public float speed = 2f;
        public float rotationRate = 0.3f;

        public Vector3 Position { get; set; }
        public float RotationX { get; set; }
        public float RotationY { get; set; }
        public float Scale { get; set; }

        public Player()
        {
            playerWorld = Matrix.CreateScale(.1f);
            this.Position = new Vector3(0, 1f, 0);
        }

        public void LoadContent(ContentManager content)
        {
            playerModel = content.Load<Model>("p1_wedge");
            playerTransforms = new Matrix[playerModel.Bones.Count];
            playerModel.CopyAbsoluteBoneTransformsTo(playerTransforms);
        }

        public void Update(float dt)
        {
            KeyboardState kbs = Keyboard.GetState();

            if (kbs.IsKeyDown(Keys.W))
            {
                playerWorld += Matrix.CreateTranslation(Vector3.Forward * speed  * dt);
            }

            if (kbs.IsKeyDown(Keys.S))
            {
                playerWorld += Matrix.CreateTranslation(playerWorld.Backward * speed * dt);
            }

            if (kbs.IsKeyDown(Keys.A))
            {
                Vector3 temp = playerWorld.Translation;
                playerWorld.Translation = Vector3.Zero;
                playerWorld += Matrix.CreateRotationY(rotationRate * dt);
                playerWorld.Translation = temp;
            }

            if (kbs.IsKeyDown(Keys.D))
            {
                Vector3 temp = playerWorld.Translation;
                playerWorld.Translation = Vector3.Zero;
                playerWorld += Matrix.CreateRotationY(-rotationRate * dt);
                playerWorld.Translation = temp;
            }
        }

        public void Render(ThirdPersonCam cam)
        {
            foreach (ModelMesh mesh in playerModel.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.View = cam.view;
                    effect.Projection = cam.proj;
                    effect.World = playerTransforms[mesh.ParentBone.Index] * playerWorld;
                }
                mesh.Draw();
            }
        }
    }

That's the player class. The error lies within the draw or update method, but I'm not sure what I'm looking for. When I press "w" it seems like the model moves behind me at first. 

public class ThirdPersonCam
    {
        public Matrix view;
        public Matrix proj;

        public ThirdPersonCam()
        {
            proj = Matrix.CreatePerspectiveFieldOfView(0.78f, 1.7777f, 1f, 10000f);
        }

        public void CameraUpdate(Matrix objectToFollow)
        {
            Vector3 camPosition = objectToFollow.Translation + (objectToFollow.Backward * 10f) + (objectToFollow.Up * 15f) + (objectToFollow.Left * 1f);
            Vector3 camTarget = objectToFollow.Translation;

            view = Matrix.CreateLookAt(camPosition, camTarget, Vector3.Up);
        }

There's the camera class. This doesn't work either, as I have called the CameraUpdate method in my main game class and the camera doesn't move at all. The camera is following the player.playerWorld matrix when I call it.

1

Share this post


Link to post
Share on other sites

Hey,

  The code only makes some vague sense in that I can see what you're trying to do but it doesn't work like that. Unless the Vector3 and Matrix classes you're using has some implementation unknown to me.(which is completely possible!).

 

You appear to be adding matrices together? I would guess you meant to concatenate them (ie. multiply?)

 

Rather than try to concatenate in this way, create your position as a vector separately from the orientation then combine them together at the end by multiplying the translation matrix (created from your position vector) and your rotation matrix. Your object should maintain the position vector and orientation (in whichever format you choose to represent it) as member variables.

 

I am not sure what your Forward/Backward/Left properties are, it looks like they're vectors extracted from your matrix.This wont work if the matrix you're extracting them from is the concatenated matrix we see in your Player.Update method. However it should be fine if you use the rotation as I mention above.

 

A couple of things to think about after you have the basic camera working:

 

With your current implementation, you will have a *very* rigid camera and you will want to add some kind of 'tightness' control.

You will want to have some kind of adjustment for the target point on the object being looked at, looking directly at the object will make it difficult to see where you are going.

 

n!

[edit] The forum lost half my original post, but I think I remembered the bits that disappeared.

Edited by nfactorial
1

Share this post


Link to post
Share on other sites

One way to fix your issue is that you can set the position and orientation of the camera to the object you want it to follow with an offset. on how far away you want it be from the player.

0

Share this post


Link to post
Share on other sites

Alrighty so I figured out the problem. I actually had two cameras going at once somehow. So now I'm only using my 3rd person cam. The rotation with a and d work fine, when you can get the model in view...This usually involves some guess work and moving back and forth with w and s. My values for the camera have also changed.

 

I'm still having a hard time actually finding the model and chasing it. I've been messing around with the values a bit but nothing seems to work right


Vector3 camPosition = objectToFollow.Translation + (objectToFollow.Backward * 3) + (objectToFollow.Up * 3);
            Vector3 camTarget = objectToFollow.Translation;

view = Matrix.CreateLookAt(camPosition, camTarget, Vector3.Up);

Edited by burnt_casadilla
0

Share this post


Link to post
Share on other sites

Why is your up vector always the same. I mean if you camera orientation is based on the player then you camera up vector will depend on your look vector.

0

Share this post


Link to post
Share on other sites
When I get to coding the camera with the mouse, I'll end up changing the vector. I want a camera like WoW or guild wars. But for now, what I have works
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0