I got Camera, Terrain and player working, moving around, etc. When I try to add another player (another model to draw) it just dont any of the models. Here is my code;
public class World
{
public Terrain terrain;
public NewPlayer player;
public List<NewPlayer> players = new List<NewPlayer>();
public World(NewPlayer player)
{
this.player = player;
}
public void AddPlayer(NewPlayer _player)
{
players.Add(_player);
}
public void SetTerrain(Terrain terrain)
{
this.terrain = terrain;
}
public void Draw(NewCamera camera)
{
terrain.Draw(camera);
foreach (NewPlayer _player in players)
{
_player.Draw(camera);
}
player.Draw(camera);
}
}
public class Terrain
{
GraphicsDevice graphicsDevice;
// heightMap
Texture2D heightMap;
Texture2D heightMapTexture;
VertexPositionTexture[] vertices;
int width;
int height;
public Vector3 position;
public BasicEffect basicEffect;
int[] indices;
Matrix worldMatrix;
// array to read heightMap data
float[,] heightMapData;
public Terrain(GraphicsDevice graphicsDevice, Vector3 position)
{
this.position = position;
this.graphicsDevice = graphicsDevice;
RecreateWorld();
}
public void SetHeightMapData(TerrainData data)
{
this.heightMap = data.HeightMap;
this.heightMapTexture = data.Texture;
width = heightMap.Width;
height = heightMap.Height;
LoadHeightData(heightMap);
SetVertices();
SetIndices();
SetEffects();
}
private void LoadHeightData(Texture2D heightMap)
{
width = heightMap.Width;
height = heightMap.Height;
Color[] heightMapColors = new Color[width * height];
heightMap.GetData(heightMapColors);
heightMapData = new float[width, height];
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
heightMapData[x, y] = 0;// heightMapColors[x + y * width].R / 5.0f;
}
public void SetIndices()
{
// amount of triangles
indices = new int[6 * (width - 1) * (height - 1)];
int number = 0;
// collect data for corners
for (int y = 0; y < height - 1; y++)
for (int x = 0; x < width - 1; x++)
{
// create double triangles
indices[number] = x + (y + 1) * width; // up left
indices[number + 1] = x + y * width + 1; // down right
indices[number + 2] = x + y * width; // down left
indices[number + 3] = x + (y + 1) * width; // up left
indices[number + 4] = x + (y + 1) * width + 1; // up right
indices[number + 5] = x + y * width + 1; // down right
number += 6;
}
}
public void SetVertices()
{
vertices = new VertexPositionTexture[width * height];
Vector2 texturePosition;
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
texturePosition = new Vector2((float)x / 25.5f, (float)y / 25.5f);
vertices[x + y * width] = new VertexPositionTexture(new Vector3(x, heightMapData[x, y], -y), texturePosition);
}
//graphicsDevice.VertexDeclaration = new VertexDeclaration(graphicsDevice, VertexPositionTexture.VertexElements);
}
graphicsDevice.SetVertexBuffer(new VertexBuffer(graphicsDevice, typeof(VertexPositionColor), vertices.Length, BufferUsage.None));
}
public void RecreateWorld()
{
this.worldMatrix = Matrix.CreateTranslation(this.position);
}
public void SetEffects()
{
basicEffect = new BasicEffect(graphicsDevice);
basicEffect.Texture = heightMapTexture;
basicEffect.TextureEnabled = true;
}
public void Draw(NewCamera camera)
{
Matrix worldMatrix = Matrix.CreateTranslation(-width / 2.0f, 0, height / 2.0f);
basicEffect.View = camera.viewMatrix;
basicEffect.Projection = camera.projectionMatrix;
basicEffect.World = worldMatrix;
foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
{
pass.Apply();
graphicsDevice.DrawUserIndexedPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, vertices, 0, vertices.Length, indices, 0, indices.Length / 3);
}
}
}
public class NewPlayer
{
Model model;
public string Guid;
public Vector3 position = new Vector3(8, 1, -3);
public Quaternion rotation = Quaternion.Identity;
public Matrix worldMatrix = Matrix.Identity;
public NewPlayer(Model _model)
{
model = _model;
foreach (ModelBone bone in model.Bones)
{
bone.Transform *= Matrix.CreateScale(0.01f);
}
}
public void RecreateWorld()
{
worldMatrix = Matrix.CreateRotationY(MathHelper.Pi)
* Matrix.CreateFromQuaternion(rotation)
* Matrix.CreateTranslation(position);
}
public void Draw(NewCamera camera)
{
RecreateWorld();
Matrix[] transforms = new Matrix[model.Bones.Count];
model.CopyAbsoluteBoneTransformsTo(transforms);
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.World = transforms[mesh.ParentBone.Index]
* worldMatrix;
if (camera != null)
{
effect.View = camera.viewMatrix;
effect.Projection = camera.projectionMatrix;
}
}
mesh.Draw();
}
}
public void MoveForward(float speed)
{
Vector3 addVector = Vector3.Transform(new Vector3(0, 0, 1), this.rotation);
this.position += addVector * speed;
}
public void MoveBackward(float speed)
{
Vector3 addVector = Vector3.Transform(new Vector3(0, 0, -1), this.rotation);
this.position += addVector * speed;
}
public void Yaw(float amount)
{
this.rotation *= Quaternion.CreateFromYawPitchRoll(MathHelper.ToRadians(amount), 0, 0);
}
public void ChangeBoneTransform(int boneIndex, Matrix t)
{
model.Bones[boneIndex].Transform = t * model.Bones[boneIndex].Transform;
}
}
If I only got the player variable, it draw everything fine, but if I add one player at the list, it doesnt draw any player. Is there anything wrong in the code??
EDIT: This is my code to add players, the value of position and rotation is the same as the initial player, i move backward a little so it dont draw at the same location both players.
NewPlayer player = new NewPlayer(Jangada.getInstance().humanModel);
player.Guid = message.PlayerLoginPacket.Player.PlayerGuid;
player.position = Util.fromPosition(message.PlayerLoginPacket.Player.PlayerPosition);
player.rotation = Util.fromQuaternionMessage(message.PlayerLoginPacket.Player.PlayerRotation);
Util.getWorld().players.Add(player);
This is LoadContent function, I load the model and add the "player" variable to World:
humanModel = Content.Load<Model>("human");
world = new World(new NewPlayer(Content.Load<Model>("human")));
Here some Screenshots:
So I got the problem solved, but cant figure out why is it doing it, I copied the fbx file to another file so now I got human.fbx and human2.fbx and it worked. dont kniw why