ricodued

How exactly would I go about implementing a free-moving camera I looked at the How To: Rotate and Move a Camera in the docs, but it doesn't seem to build for a 360 project.

Any help or a point in the right direction


Re: XNA Game Studio Express Free-moving camera?

Derek Nedelman

You should check out the FlyingCamera class in my utility library project. It uses the convention that the world z-axis points toward the sky, so if you use a different convention you may need to make a few tweaks





Re: XNA Game Studio Express Free-moving camera?

ricodued

Ah, thanks a ton. Should work flawlessly :)




Re: XNA Game Studio Express Free-moving camera?

ricodued

Spoke too soon. I'm having trouble figuring out where exactly to hook up the camera. I've got it declared and initialized:

public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
ContentManager content;
Model myModel;
Matrix world;
ThreeSixBox.FlyingCamera cam;

public Game1()
{
graphics = new GraphicsDeviceManager(this);
content = new ContentManager(Services);

world = new Matrix();
cam = new ThreeSixBox.FlyingCamera(world);
cam.MoveSpeed = 0.3f;
cam.TurnSpeed = 0.3f;
}

And I've got it updating (I think) in my Update() method:

protected override void Update(GameTime gameTime)
{
...

cam.Update(gameTime.ElapsedGameTime);

base.Update(gameTime);
}

However, the camera doesn't seem to want to move. Draw code:

protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

//Copy any parent transforms
Matrix[] transforms = new Matrix[myModel.Bones.Count];
myModel.CopyAbsoluteBoneTransformsTo(transforms);

//Draw the model, a model can have multiple meshes, so loop
foreach (ModelMesh mesh in myModel.Meshes)
{
//This is where the mesh orientation is set, as well as our camera and projection
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);
}
//Draw the mesh, will use the effects set above.
mesh.Draw();
}

The project builds and deploys to the 360 fine, and I can see my model, the camera just won't budge :(

Any help




Re: XNA Game Studio Express Free-moving camera?

Derek Nedelman

You need to call InputHelper.Update() to update the global gamepad information before calling Update() on the flying camera. I guess I'll update the documentation to clarify that.





Re: XNA Game Studio Express Free-moving camera?

ricodued

Thanks, however this still doesn't change anything on the screen.

I'm going to guess it's my drawing code. I'll work on that.




Re: XNA Game Studio Express Free-moving camera?

ricodued

I'm sorry but I'm completely stumped (this is so vastly different from Windows Forms ;)).

How should I be setting up my matrices with the FlyingCamera

i.e.:

protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

foreach (ModelMesh mesh in worldMesh.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.World =
effect.View =
effect.Projection =
}

mesh.Draw();
}

base.Draw(gameTime);
}




Re: XNA Game Studio Express Free-moving camera?

Derek Nedelman

There's no magic to it really.

effect.World = whatever you model's local->world transformation matrix is
effect.View = Matrix.Invert(flyingCamera.WorldMatrix);
effect.Projection = Matrix.CreatePerspectiveFieldOfView(cameraFov, (float)viewWidth/(float)viewHeight, near, far);

You can take a look at this project to see the FlyingCamera in action





Re: XNA Game Studio Express Free-moving camera?

ricodued

Ah, that will help a lot. Probably should've just looked for that project :P

Thanks a lot, it's greatly appreciated.




Re: XNA Game Studio Express Free-moving camera?

Derek Nedelman

Also, I just noticed that the project uses a much older version of the FlyingCamera (the code uses the name 'Matrix' instead of 'WorldMatrix'), but the usage is the same.

The most current version of the FlyingCamera will always be in the utilities library I posted a link to earlier.





Re: XNA Game Studio Express Free-moving camera?

ricodued

Yeah, I noticed. I did get the older one working, except for some odd rotations with the right thumbstick. Hopefully that's fixed by the new FlyingCamera, but if not I'll iron it out.

EDIT: Weird rotations still existed, but that was easily fixed by changing

            if (turn != 0.0f)
            {
                ...

                Matrix.CreateRotationZ(turnSpeed * turn, out rotation);

               ...
            }

to Matrix.CreateRotationY(turnSpeed * turn, out rotation);

Now it functions exactly like a typical FPS camera except it's not tethered to the ground :)

Great work. Thanks a ton.




Re: XNA Game Studio Express Free-moving camera?

Derek Nedelman

I'm glad you got it to work.

Actually the "weird" behavior isn't weird at all. Your world "up" axis is the Y axis, whereas the my implementation is for the Z axis - so the change makes sense.

Also, I hope you're using the newer implementation of the flying camera. The one I included with the balls demo was thrown together from some other old code I had and is kind of ugly.





Re: XNA Game Studio Express Free-moving camera?

ricodued

Yeah, I'm using the new implementation. It works great and exactly as I expect it to.

I'm going to need to change my world's up-axis to the Z-axis, since that's what I use in 3ds max. Thanks again for your help :) Great library.