redneon

I've hit a really weird problem with the game I'm writing for the DBP competition. The reason it's so weird is because I'm sure it wasn't a problem before.

Basically, I'm implementing the p1_wedge model in a class I have created called Player. Originally, I only had one player on screen but now I've added a second player. The problem I'm having is that it only draws the model for the second player. However, once the second player gets destroyed it then starts to display the model for the first player. It's as if the first player can't be drawn because the second player is using the model.

I haven't done anything special to implement the Player class. I have made the main class a Singleton so that I have access to the graphics and content objects, amongst other things. Then, in my Game class I create the players by doing...

public Game()
{
...
player1 = new Player(new Vector3(-330.0f, -330.0f, 0.0f), this);
player2 = new Player(new Vector3(330.0f, -330.0f, 0.0f), this);
...
}

I then get the main class' LoadGraphicsContent method to call a LoadGraphicsContent method in my Game class like this...

public void LoadGraphicsContent()
{
...
player1.PlayerModel = AstroGame.GetAstroGame().Content.Load<Model>("Content\\Models\\p1_wedge");
player1.Initialise();
player2.PlayerModel = AstroGame.GetAstroGame().Content.Load<Model>("Content\\Models\\p1_wedge");
player2.Initialise();
...
}

Then I have Update and Draw methods in my Game class which just call the respective methods in the Player class like this...

public void Update()
{
...
player1.Update();
player2.Update();
...
}

public void Draw()
{
...
player1.Draw();
player2.Draw();
...
}

Does anyone have any idea what I could be doing wrong It's driving me nuts. I'm absolutely positive that I had two Players on screen when I first started writing this game =o)



Re: XNA Game Studio Express Multiple Instances of the Same Model

Kyle_W

Please post your Player.Update() and Player.Draw() methods.



Re: XNA Game Studio Express Multiple Instances of the Same Model

Dan Lingman

Something quick to try:

Do you see both models if you switch them to be different (i.e. p1_wedge and p2_wedge instead of both p1_wedge)

In my DBP game, I've got a shape manager class, that loads each model once, and then hands them out on demand. I'm including it below.

Hope it helps, or at least gives some ideas. (ModelIndex is a simple enum, which should be obvious from how it's used)

Sorry about the formatting - once it's imported into VS, should be trivial to fix.

Dan Lingman

NogotoGames.com

(My DBP project got slightly sidetracked yesterday - Wife went into labor - Now have a happy baby girl)

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.Xna.Framework;

using Microsoft.Xna.Framework.Audio;

using Microsoft.Xna.Framework.Content;

using Microsoft.Xna.Framework.Graphics;

using Microsoft.Xna.Framework.Input;

using Microsoft.Xna.Framework.Storage;

namespace SpaceFortress.Shapes

{

class ShapeManager

{

private static Model[] models;

public static void init(SpaceFortressGame game)

{

// we need to load and initalize all the models

// 6 ships, 2 asteroids, and one missile

models = new Model[9];

models[0] = SpaceFortressGame.ContentManager.Load<Model>("Content\\Models\\p1_wedge");

models[1] = SpaceFortressGame.ContentManager.Load<Model>("Content\\Models\\p2_wedge");

models[2] = SpaceFortressGame.ContentManager.Load<Model>("Content\\Models\\p1_pencil");

models[3] = SpaceFortressGame.ContentManager.Load<Model>("Content\\Models\\p2_pencil");

models[4] = SpaceFortressGame.ContentManager.Load<Model>("Content\\Models\\p1_saucer");

models[5] = SpaceFortressGame.ContentManager.Load<Model>("Content\\Models\\p2_saucer");

modelsDevil = SpaceFortressGame.ContentManager.Load<Model>("Content\\Models\\asteroid1");

models[7] = SpaceFortressGame.ContentManager.Load<Model>("Content\\Models\\asteroid2");

modelsMusic = SpaceFortressGame.ContentManager.Load<Model>("Content\\Models\\p1_rocket_proj");

}

public static Model getModel(ModelIndex index)

{

return models[(int)index];

}

}





Re: XNA Game Studio Express Multiple Instances of the Same Model

waruwaru

Dan Lingman wrote:

(My DBP project got slightly sidetracked yesterday - Wife went into labor - Now have a happy baby girl)

I don't think you can enter her into DBP contest... ;) CONGRATS!!!






Re: XNA Game Studio Express Multiple Instances of the Same Model

redneon

Congratulations to Dan Lingman! You're not getting any sleep for a year or two ;)

Here are three methods I use in my Player class. First I call Initialise when I created the instances...

public void Initialise()
{
foreach (ModelMesh mesh in playerModel.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.World = worldMatrix;
effect.View = AstroGame.GetAstroGame().Camera.View;
effect.Projection = AstroGame.GetAstroGame().Camera.Projection;
}
}
}

Here is my Update...

public void Update()
{
...
Matrix tempMatrix = worldMatrix * Matrix.CreateRotationY(rotation.Y) * Matrix.CreateTranslation(position);

foreach (ModelMesh mesh in playerModel.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.World = tempMatrix;
}
}
...
}

Here is my Draw...

public void Draw()
{
...
foreach (ModelMesh mesh in playerModel.Meshes)
{
mesh.Draw();
}
...
}

I really can't see what I'm doing wrong.





Re: XNA Game Studio Express Multiple Instances of the Same Model

Dan Lingman

Thanks.

Take a look at the docs for ContentManager, Load Method:

Return Value

The loaded asset. Repeated calls to load the same asset will return the same object instance.
Both of your player objects are referring to the same model.
You'll need to add your own variable in your player class to keep track of the position, then use that inside your draw method instead of setting the position in your update. (cause your second call to Player.Update is overwriting the first one)
So this:
foreach (ModelMesh mesh in playerModel.Meshes)
{
mesh.Draw();
}
becomes:
foreach (ModelMesh mesh in playerModel.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.World = this.WorldMatrix;
}
mesh.Draw();
}
Where WorldMatrix is the variable you've added to your Player class.
Cheers,
Dan




Re: XNA Game Studio Express Multiple Instances of the Same Model

redneon

Ahh, of course. It make perfect sense :) So effectively all I'm doing is drawing the same model instance in two positions each frame

Cheers.





Re: XNA Game Studio Express Multiple Instances of the Same Model

Dan Lingman

Worse - you're drawing the same model instance twice in the same postion each frame.

Since you only have once instance, you are setting it's world matrix twice in the update method, then drawing it twice.

Cheers,
Dan.

(Everyone is asleep now, so I am madly working to get my submission ready - at 5:00 in the morning. sigh)





Re: XNA Game Studio Express Multiple Instances of the Same Model

redneon

Mine's far from ready, I'm afraid. I'm still going to submit what I have, though. I started a new job working for a mobile phone game dev company a few weeks ago so I haven't had as much free time as I'd like to work on my DBP game.

Is there are particular time the games have to be submitted by or is it just any time on the 28th





Re: XNA Game Studio Express Multiple Instances of the Same Model

Dan Lingman

I would have polished mine a bit more, but you know what they say about adding people to a late project...

(I guess it's really not fair to blame my 2 day old for me not getting stuff done)

The deadline are:

Official entry period is from 2/21/07 at 12:00 AM Central Time (Ħ°CTĦħ) (CT = GMT ¨C 06:00) through
2/28/07 at 11:59:59 PM CT in eligible geographies.

Cheers,

Dan





Re: XNA Game Studio Express Multiple Instances of the Same Model

redneon

Well, I live in the UK, and as I understand it central time is six hours behind us. So in theory I have until 6am tomorrow morning to finish my project