Weird XNA problem with loading models

Started by
2 comments, last by ProjectOlle 13 years ago
Hey guys!

I have previously loaded several texture in a 2D game for XNA 4.0 and I have also loaded several models in 3D.. But now I ran into a weird problem I really dont get.

I have a boid, player and an obstecal class so far. The boid and player class share the same model, and obstecal has a different one. But when I load in the models it will only load the model for boid and player, if I change the model of the obstecal to load the same in Content.Load<Model>("") then it will load the model, meaning I can only have the same model in all instances :S which as I said.. is well.. weird :S ???

Any ideas for what could cause this??

I bring you the main code here:


using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace XNA3DTest
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;

Vector3 cameraPosition;
Matrix ViewMatrix;
Matrix Projection;

Vector3 obstecalPosition = Vector3.Zero;

Player player;
Boid boid;
Obstecal obstecal;

float aspectRatio;

Model model;
Model obstecalModel;

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";

}

/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
cameraPosition = new Vector3(200.0f, 30.0f, 50.0f);

aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;

ViewMatrix = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up);
Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(40.0f), aspectRatio, 10.0f, 100000.0f);

model = Content.Load<Model>("testSquare");
obstecalModel = Content.Load<Model>("testPolygon");

boid = new Boid(model);
player = new Player(model);
obstecal = new Obstecal(obstecalPosition, obstecalModel);

base.Initialize();
}

/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
// TODO: use this.Content to load your game content here
}

/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}

/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
float moveFactorPerSecond = 400 * (float)gameTime.ElapsedGameTime.TotalMilliseconds / 1000;
KeyboardState keyboard = Keyboard.GetState();
// Allows the game to exit
if (keyboard.IsKeyDown(Keys.Escape))
this.Exit();

if (keyboard.IsKeyDown(Keys.Space))
boid = new Boid(model);
if (boid.positionList.Count == 0)
boid = new Boid(model);

player.update();
boid.update();

for (int i = 0; i < boid.positionList.Count; i++)
{
boid.avoidPlayer(player, boid, i);
if (player.boundingSphere.Intersects(boid.boundingSphereList))
{
boid.killBoid(i);
}
}
// TODO: Add your update logic here

base.Update(gameTime);
}

/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);

// TODO: Add your drawing code here
boid.callDraw(model, ViewMatrix, Projection);
player.Draw();
obstecal.Draw();
base.Draw(gameTime);
}

}
}



And Ive tried both loading the models in the init function and the loading function.. same thing happens
Advertisement


protected override void Initialize()
{
// TODO: Add your initialization logic here
cameraPosition = new Vector3(200.0f, 30.0f, 50.0f);

aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;

ViewMatrix = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up);
Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(40.0f), aspectRatio, 10.0f, 100000.0f);

model = Content.Load<Model>("testSquare");
obstecalModel = Content.Load<Model>("testPolygon");

boid = new Boid(model);
player = new Player(model);
obstecal = new Obstecal(obstecalPosition, obstecalModel);

base.Initialize();
}



1/ Think this is supposed to go into the LoadContent() Method as a bit of cleanup

2/ There is more to loadinding a model than just Content.Load<Model>("myModel");
try this:

private Model LoadModel(string assetName)
{

Model newModel = Content.Load<Model> (assetName);
foreach (ModelMesh mesh in newModel.Meshes)
foreach (ModelMeshPart meshPart in mesh.MeshParts)
meshPart.Effect = effect.Clone();
return newModel;
}


and replace
model = LoadModel("myModel");

Also there is a lot of good info on this site: http://www.riemers.n...arp/series2.php What i use to fix all my problems :D
Same problem after I tried this >_< .. It pretty much rules out that it should be the effects in the mesh that is the problem.. At the moment I am using default effects, and normally it doesnt pose a problem, but today.. I guess it does :S
Found the problem!

When drawing the player object I specified my view and projection, which I did not do for the obstecal, hench it cannot be seen :P

This topic is closed to new replies.

Advertisement