0
\$\begingroup\$

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: 1 2

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

\$\endgroup\$
  • \$\begingroup\$ How exactly are you adding players? I assume you call addplayer(new newplayer);? \$\endgroup\$ – Peethor Jan 20 '15 at 20:03
0
\$\begingroup\$

Please say for what are this variables?

public NewPlayer player;
public List<NewPlayer> players = new List<NewPlayer>();

And why are you drawing both player an players, but not only players?

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??

Please provide the code snippet where you create and add players.

UPDATE

So, problem in that you apllying scale each time you create model. If model is same instance you will get to small model to see it.

\$\endgroup\$
  • \$\begingroup\$ The diference is that the player variable is the one who controls the camera, and the players are the others players who i just draw it, i dont change the position or rotation, im getting in work, when i got there ill provide the code i use to create and add players \$\endgroup\$ – Caio Keto Jan 21 '15 at 9:54
  • \$\begingroup\$ I guess that if you draw all models one manner they should look the same. The problem must be hiding somewhere in the code that you added when several players want to render or where you call World.Draw(). And another question: the model is not drawn or they simply not visible? \$\endgroup\$ – user52551 Jan 21 '15 at 10:19
  • \$\begingroup\$ Its not visible, the draw command is called, the position and rotation is all the same before and after add more players. I add the code snippet \$\endgroup\$ – Caio Keto Jan 21 '15 at 10:21
  • \$\begingroup\$ Did player appears if you comment in world.Draw() method all instead of player.Draw(camera) call? I mean exactly now, not before. \$\endgroup\$ – user52551 Jan 21 '15 at 10:25
  • \$\begingroup\$ I didnt understand if I comment world.Draw it wont draw the players \$\endgroup\$ – Caio Keto Jan 21 '15 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.