Loading terrain in XNA incorrectly + First Person camera - c#

I have a project due very soon and I'm having lots of issues trying to load a model I made in 3D Studio Max. The model I made is what I want as my terrain in my XNA game. Here is what I've done so far:
Methods:
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Model terrainModel;
Vector3 terrainPosition = Vector3.Zero;
Vector3 cameraPosition = new Vector3(0.0f, 60.0f, 160.0f);
Vector3 cameraLookAt = new Vector3(0.0f, 60.0f, 160.0f);
Matrix cameraProjectionMatrix;
Matrix cameraViewMatrix;
LoadContent()
spriteBatch = new SpriteBatch(GraphicsDevice);
cameraViewMatrix = Matrix.CreateLookAt(
cameraPosition,
Vector3.Zero,
Vector3.Up);
cameraProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(80.0f),
graphics.GraphicsDevice.Viewport.AspectRatio,
1.0f,
1000.0f);
terrainModel = Content.Load<Model>("alphastreet");
Draw(GameTime gameTime)
GraphicsDevice.Clear(Color.CornflowerBlue);
DrawModel(terrainModel, terrainPosition);
// TODO: Add your drawing code here
base.Draw(gameTime);
And then I want to Draw:
void DrawModel(Model model, Vector3 modelPosition)
{
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.PreferPerPixelLighting = true;
effect.World = Matrix.CreateTranslation(modelPosition);
effect.Projection = cameraProjectionMatrix;
effect.View = cameraViewMatrix;
}
mesh.Draw();
}
}
Everything else is just as an XNA file should look. The model it's self looks like a fairly straightforward street. However, upon XNA loading the model, it's just a big block in the window that loads.
I don't know what I'm doing that's making the model load like this, but it's making me pull my hair out. Any help would be appreciated.
Also, would anybody be able to direct me to walk-through/guide or a class that creates a First Person Shooter camera, since that is the game I'm going. I probably shouldn't have picked that, but it's way too late to change now, so if you could help there too you would really be saving my skin!
Thanks, Jake!

Is there more than one mesh in the model, and if so, have you tried rendering each one separately?
Have you tried using identity matrices for world and view to make sure these aren't the issue?
I assume you haven't made any other state changes to xna/directx, if so try disabling these.
For a FPS camera, you can just track the x and y rotation in float variables and update these each frame based upon how far the mouse has moved in x and y. From these you can generate a camera view matrix; something like the following:
var mx = Matrix.CreateRotationX(xangle);
var my = Matrix.CreateRotationY(yangle);
var pos = Matrix.CreateTranslation(position);
var view = mx * my * pos; // These might be in the wrong order, not sure off the top of my head
view.Invert();
The camera is always inverted because moving left moves the world right, turning left turns the world right etc.

Related

Problems rotating a model in XNA

I'm new to 3D models so apologises if these seems basic. I've been searching the web and through my books but I'm lost.
I've made a model in 3D Studio Max 9. It consists of a variety of cubes, clyinders etc.
In Xna I've imported the model okay and it shows correctly. However, when I apply rotation, each component in the model rotates around it's own centre rather. I want the model to rotate as a singal unit rather than each component rotating on the spot.
I've linked the components in 3D Max and they rotate as I want in Max.
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
model = Content.Load<Model>("Models/Alien1");
}
protected override void Update(GameTime gameTime)
{
camera.Update(1f, new Vector3(), graphics.GraphicsDevice.Viewport.AspectRatio);
rotation += 0.1f;
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
Matrix[] transforms = new Matrix[model.Bones.Count];
model.CopyAbsoluteBoneTransformsTo(transforms);
Matrix worldMatrix = Matrix.Identity;
Matrix rotationYMatrix = Matrix.CreateRotationY(rotation);
Matrix translateMatrix = Matrix.CreateTranslation(location);
worldMatrix = rotationYMatrix * translateMatrix;
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.World = worldMatrix * transforms[mesh.ParentBone.Index];
effect.View = camera.viewMatrix;
effect.Projection = camera.projectionMatrix;
effect.EnableDefaultLighting();
effect.PreferPerPixelLighting = true;
}
mesh.Draw();
}
base.Draw(gameTime);
}
EDIT: Rotating the object via it's properties works fine so I'm guessing there's something up with the code rather than with the object itself.
Translating the object also causes the objects to get moved independently of each other rather than as a single model and each piece becomes spread around the scene.

Correcting camera movement - with demonstrational video

I have a sphere as FBX and a shader that enables me to have a texture inside of the sphere. Also inside of the sphere I have a camera. The goal is to have a simple panoramic viewer that is being driven by a headtracker.
The texture is being shown but the camera is reacting in a weird way:
When rotating the headtracker, the image is all over the place, jittery and not at all correlated to the actual movements. The headtracker input itself is correct, though.
See this video for further clarification. Each time I keep the tracker steady at first and then I move it. In the second half, the first three numbers are evaluated. The other three values are gyro.
I presume the problem to be in the following code, something about the Vectors(Vector3.whatever) could be off:
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.CornflowerBlue);
foreach (ModelMesh mesh in skyDome.Meshes)
{
foreach (BasicEffect ef in mesh.Effects)
{
float aspectRatio = (float)graphics.GraphicsDevice.PresentationParameters.BackBufferWidth / (float)graphics.GraphicsDevice.PresentationParameters.BackBufferHeight;
Vector3 camPosition = Vector3.Transform(Vector3.Up, Quaternion.CreateFromYawPitchRoll(drehung, neigung, rollen));
ef.View = Matrix.CreateLookAt(Vector3.Zero, camPosition,Vector3.Forward);
ef.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspectRatio, 0.1f, 10000.0f);
ef.TextureEnabled = true; //Textur zulassen
ef.Texture = panoramaTextur; //Textur auf Model darstellen
}
mesh.Draw();
}
base.Draw(gameTime);
}

Having trouble with Sprite look at mouse in XNA for game

I understand that this question has been answered before and I've done my research trust me.
I'll say this now, this is a project for school but what I want to do this for my own learning considering that I will be going in to game programmer.
This might get messy but ill do my best
so for this its started as a basic shoot em up, we have a
game.cs- contains all the textures vectors what not, and the
entity.cs- has already begun spritebatch for player to call a player.draw (spritebatch) in
game.cs
player.cs- contains the shooting of the bullet and the position
bullet.cs
enemy.cs
So my problem is that I have the code for updating the sprite to look at the mouse then I would use
player.Draw(
playerTexture,
position,
null,
Color.White,
rotation,
spriteOrigin,
1.0f,SpriteEffects.None,0f);
In game.cs draw function, but when I do this I get another sprite (same sprite as my player sprite) that does look at my mouse but I want my other one to look at mouse
But my proff. Wrote the code so that there is an already begun spritebatch in entity.cs
public CEntity(CGame myGame, Texture2D myTexture)
{
game = myGame;
texture = myTexture;
}
/// <summary>
/// Draws the player.
/// </summary>
/// <param name="begunSpriteBatch">Give this function a already begun SpriteBatch.</param>
public void Draw(SpriteBatch begunSpriteBatch)
{
begunSpriteBatch.Draw(texture, position, Color.White);
}
So in game.cs draw function player.draw is called to draw the player, what can I do to get that look at mouse effect for that sprite?
public CPlayer(CGame game, Texture2D playerTexture)
: base(game, playerTexture)
{
texture = playerTexture;
brotation = rotation;
position.X = 400f;
position.Y = 400f;
}
And also player is inherited from entity.
Code for updating the mouse as requested
IsMouseVisible = true;
MouseState mouse = Mouse.GetState();
distance.X = mouse.X - position.X;
distance.Y = mouse.Y - position.Y;
//Math.Atan2((double)mouseState.Y - position.Y, (double)mouseState.X - position.X); was using this //before found a different way
rotation = (float)Math.Atan2(distance.Y, distance.X);
spriteRectangle = new Rectangle((int)position.X, (int)position.Y,//rotation stuff
playerTexture.Width, playerTexture.Height);
spriteOrigin = new Vector2(spriteRectangle.Width / 2, spriteRectangle.Height / 2);

Rotate 3D Model in XNA

I am new to XNA, and I am creating a simple game. Sorry that this is probably really simple, but I can't find any help on it. There is a ship in the game that I made with Blender, and I want to be able to control the ship by being able to rotate the ship's X, Y and Z axises. Here is the code I have:
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
RotationMatrix = Matrix.CreateRotationY(MathHelper.PiOver2) * Matrix.CreateRotationY (rotationY) * Matrix.CreateRotationX(rotationX) * Matrix.CreateRotationZ(rotationZ);
Matrix shipTransformMatrix = RotationMatrix * Matrix.CreateTranslation(ship.Position);
DrawModel(ship.Model, shipTransformMatrix, ship.Transforms);
// TODO: Add your drawing code here
base.Draw(gameTime);
}
public void DrawModel(Model model, Matrix modelTransform, Matrix[] absoluteBoneTransforms)
{
//Draw the model, a model can have multiple meshes, so loop
foreach (ModelMesh mesh in model.Meshes)
{
//This is where the mesh orientation is set
foreach (BasicEffect effect in mesh.Effects)
{
effect.World = absoluteBoneTransforms[mesh.ParentBone.Index] * modelTransform;
effect.Projection = projectionMatrix;
effect.View = viewMatrix;
}
//Draw the mesh, will use the effects set above.
mesh.Draw();
}
}
This will rotate the ship, but not along the ship's axis's. If I rotate the Y axis (by changing the value of rotationY), the ship will rotate along its Y axis. But if I rotate the X or Z axis, the ship rotates according according to the world's X and Z axises, not its own. How do I make it so the ship rotates on its own axises? Do I need to do something different with the matrices?
Thanks
Using CreateRotationX, CreateRotationY, & CreateRotationZ all apply rotations around the world or global axes. Meaning it causes your object to rotate only around the world/global axes, not your object's local axes.
Using CreateFromAxisAngle allows you to input whatever rotation axis you want, including the ship's own local axes.
A shift in your overall rotation paradigm will be needed, however, since a rotation around an arbitrary axis like the ship's Up, for instance, can cause a change to any of the 3 angle values at once. Keeping track of all that is unnecessarily difficult. There is an easier way:
Simply store a rotation in matrix (or quaternion) form instead of the 3 angles.
EDIT: Giving some credit here to Steve below (great answer mate, been a while since I did alot of 3D math).
This tutorial here will show you how to setup what Steve suggested!
http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series1/Rotation_-_translation.php
Original Post:
I believe you have to create an effect.Rotation in your BasicEffect loop.
All of this is covered in the basic tutorials over at MSDN I believe. Your code even looks like it came from there.
http://msdn.microsoft.com/en-us/library/bb203897(v=xnagamestudio.31)
If not, check out this link, Reimer covers everything in 3D worth knowing to start:
http://www.riemers.net/eng/Tutorials/XNA/Csharp/series1.php
Here is what I ended up doing just in case anyone else gets stuck like I did:
Matrix RotationMatrix;
//every time I wanted to rotate around an axis, I would do something like this:
protected void rotateY()
{
RotationMatrix *= Matrix.CreateFromAxisAngle(RotationMatrix.Up, MathHelper.ToRadians(1.0f));
//For the X axis I used RotationMatrix.Right, and for the Z RotationMatrix.Forward
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
Matrix shipTransformMatrix = RotationMatrix * Matrix.CreateTranslation(ship.Position);
DrawModel(ship.Model, shipTransformMatrix, ship.Transforms);
// TODO: Add your drawing code here
base.Draw(gameTime);
}
public void DrawModel(Model model, Matrix modelTransform, Matrix[] absoluteBoneTransforms)
{
//Draw the model, a model can have multiple meshes, so loop
foreach (ModelMesh mesh in model.Meshes)
{
//This is where the mesh orientation is set
foreach (BasicEffect effect in mesh.Effects)
{
effect.World = absoluteBoneTransforms[mesh.ParentBone.Index] * modelTransform;
effect.Projection = projectionMatrix;
effect.View = viewMatrix;
}
//Draw the mesh, will use the effects set above.
mesh.Draw();
}
}

How can I hide certain faces of a cube model In XNA for a Voxel Engine to optimize it?

Hello I'm trying to make a terrain engine similar to that of Minecraft.
I was able to get a chunk loaded. It is very laggy and when there is more than one chunk loaded at once it becomes unplayable.
This is my render code:
public static void renderNormalBlock(GraphicsDevice g, Chunk chunk,Texture2D texture, int x, int y, int z)
{
float tileSize = 0.5F;
Vector3 blockPosition = new Vector3(x / tileSize, y / tileSize, z / tileSize);
Model blockModel = Main.defaultBlockModel;
ModelMesh mesh = blockModel.Meshes[0];
g.SamplerStates[0] = SamplerState.PointWrap;
BasicEffect effect = (BasicEffect)mesh.Effects[0];
effect.TextureEnabled = true;
effect.Texture = texture;
effect.View = Main.camera;
effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), g.DisplayMode.AspectRatio, 1, 128);
effect.World = Matrix.CreateWorld(blockPosition, Vector3.Forward, Vector3.Up);
mesh.Draw();
}
As You can see I am not using for-each or for loops because as it's only a cube; It is not required.
I did some research and the best answer I found was that I need to hide the cube's faces that are not visible. So say if there's 2 cubes next to each other, I don't want to render the in between faces.
This is where I get stuck, Most people are using cubes that were drawn in XNA, and I'm using a model.
I'm new to XNA and I don't understand too much of the Math involved in manually drawing a cube since I'm currently in grade 9, so I used a model.
So how would I go about rendering only the faces that are visible?
your starting to develop a game before learning the basics. you wont get too far this way. First grab a book about XNA development and go through it. This is a basic subject that will be covered there. In addition, go visit techCraft http://techcraft.codeplex.com/ and download their implementation which comes with all the code. you will learn alot from that alone.

Categories

Resources