Jump to content

  • Log In with Google      Sign In   
  • Create Account


models spawning in same position


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Null_Reference_Exception   Members   -  Reputation: 210

Like
0Likes
Like

Posted 09 December 2013 - 07:46 PM

Hi
I am having a problem I cant work out, and I am looking for some direction. I am trying to draw a list of 20 models and they are being to the screen but they are all being drawn to the game window at the exact same position and I cant see where my code is wrong, 
 
In my enemy class I have
 
public Enemy(Game1 game)
        {
            randX = random.Next(1, 10);
            randY = random.Next(1, 10);
            randZ = random.Next(1, 10);
            enemyPos = new Vector3(randX, randY, randZ);
            enemyColBox = new Box(enemyPos, 3f, 3f, 3f);                       
            enemyModel = new EntityModel(enemyColBox, game.Content.Load<Model>("Models/enemyShip1"), Matrix.Identity *                                                                            Matrix.CreateScale(0.03f), game);
            hit = game.Content.Load<SoundEffect>("Audio/Hit");
            game.space.Add(enemyColBox); 
            game.Components.Add(enemyModel);
            enemyColBox.Tag = enemyModel;
        }
 
 
Game class
 
protected override void LoadContent()
{
     makeEnemies(20);
}
 
public void makeEnemies(int num)
        {
            for (int x = 0; x < num; x++)
            {              
                    enemy.enemyPos = new Vector3(enemy.randX, enemy.randY, enemy.randZ);
                    newEnemies.Add(new Enemy(this)); 
         
                
            }           
        }

Edited by Null_Reference_Exception, 09 December 2013 - 07:48 PM.


Sponsor:

#2 gezegond   Members   -  Reputation: 366

Like
2Likes
Like

Posted 09 December 2013 - 08:47 PM

I'm not familiar with XNA, but are you properly seeding your random function? I found a problem similar to yours here, see if it helps.



#3 Orangeatang   Members   -  Reputation: 1438

Like
1Likes
Like

Posted 10 December 2013 - 02:17 AM

Have you tried increasing the range of the random positions you generate (say 1 to 1000 instead of 1 to 10)?

 

It could be that they're all getting spawned at different locations, but because the range is so small it looks like they're all in exactly the same spot.

 

Seeding the random number generator is also a good idea.



#4 Null_Reference_Exception   Members   -  Reputation: 210

Like
0Likes
Like

Posted 10 December 2013 - 06:49 AM

Thanks Guys

 

 

The problem was as gezegond said that I was generating the same seed values each time. 

simple soulotion was to generate a new seed.

 

//instantiate a new seed

private Random random = new Random(Guid.NewGuid().GetHashCode());






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS