Results 1 to 7 of 7

Thread: question about drawing 3D

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    231

    question about drawing 3D

    Hi all I am trying to draw my 3D enemy each time I call the enemy class at random position , but without success

    That is my drawing code:
    Code:
       if ((texture != null) && (texture.Meshes.Count > 0))
                {
                    Matrix[] transforms = new Matrix[texture.Bones.Count];
                    texture.CopyAbsoluteBoneTransformsTo(transforms);
                  
                    foreach (ModelMesh meshh in texture.Meshes)
                    {                   
                        foreach (BasicEffect effect in meshh.Effects)
                        {
                            effect.EnableDefaultLighting();
                            effect.World = transforms[meshh.ParentBone.Index] *
                                Matrix.CreateRotationY(modelRotation)
                                * Matrix.CreateTranslation(position);
                            effect.View = Matrix.CreateLookAt(new Vector3(0.0f, 50.0f, 5000.0f),
                              Vector3.Zero, Vector3.Up);
                            effect.Projection = Matrix.CreatePerspectiveFieldOfView(
                                MathHelper.ToRadians(45.0f), aspectRatio,
                                1.0f, 10000.0f);
                        }                  
                        meshh.Draw();
                    }            
                }
    At this line Matrix.CreateTranslation(position); position is Vector3 an the argument in it are always random:
    Code:
     Microsoft.Xna.Framework.Vector3 position = new Microsoft.Xna.Framework.Vector3(random.Next(0, GraphicsDevice.Viewport.Width), random.Next(0,300),1000.0f);
                  Enemy  eninit = new Enemy(secondModel, position, 100, 100, 0.5f);
    But when I call the class 3 times I get 3 models at same coordinates.
    This is the whole idea:

    But there is one difference now everything on the above picture is 3D !
    Last edited by mitko29; Feb 2nd, 2012 at 12:32 PM.

  2. #2
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,181

    Re: question about drawing 3D

    I suspect the problem isn't in the 3d drawing. If you are getting repeat values from random.Next you are probably defining the random object at too low a level. Instead declare it just once at Form/Class level. BB

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    231

    Re: question about drawing 3D

    Quote Originally Posted by boops boops View Post
    I suspect the problem isn't in the 3d drawing. If you are getting repeat values from random.Next you are probably defining the random object at too low a level. Instead declare it just once at Form/Class level. BB
    Hi boops boops thanks for you comment , but I am not sure that I fully understand you .
    What do you mean with
    Code:
     from random.Next you are probably defining the random object at too low a level.

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: question about drawing 3D

    Where is the random object declared and instantiated?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,181

    Re: question about drawing 3D

    Quote Originally Posted by mitko29 View Post
    Hi boops boops thanks for you comment , but I am not sure that I fully understand you .
    What do you mean with
    Code:
     from random.Next you are probably defining the random object at too low a level.
    By "too low a level" I mean declaring the random object inside a function instead of under the class name. BB

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    231

    Re: question about drawing 3D

    Quote Originally Posted by boops boops View Post
    By "too low a level" I mean declaring the random object inside a function instead of under the class name. BB
    I don't think that this is the general problem.
    How am I suppose to say to xna framework that I wan't to initialize the objects only behind the black line ?

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    231

    Re: question about drawing 3D

    Ok,I did it but now I am trying to apply Bounding Sphere on my 3D model and detect collision.



    So far so good but I have problem detecting the collision...
    Here is the code I use(draw method of enemy) :
    Code:
     public void Draw(GameTime gameTime)
            {
                //if (alive)
                //{                               
                //    spriteBatch.Draw(texture, position, Microsoft.Xna.Framework.Color.Wheat);                                 
                //}     
                if ((texture != null) && (texture.Meshes.Count > 0))
                {
                    Matrix[] transforms = new Matrix[texture.Bones.Count];
                    texture.CopyAbsoluteBoneTransformsTo(transforms);
    
                    foreach (ModelMesh meshh in texture.Meshes)
                    {
                        if (sphere.Radius == 0)
                            sphere = meshh.BoundingSphere;
                        else
                            sphere = BoundingSphere.
                                     CreateMerged(sphere, meshh.BoundingSphere);
                        DebugShapeRenderer.AddBoundingSphere(meshh.BoundingSphere, Microsoft.Xna.Framework.Color.Red);              
                        foreach (BasicEffect effect in meshh.Effects)
                        {
                            effect.EnableDefaultLighting();
                            enemymat = effect.World = transforms[meshh.ParentBone.Index] *
                                Matrix.CreateRotationY(modelRotation)
                                * Matrix.CreateTranslation(position);                        
                            effect.View = Matrix.CreateLookAt(new Vector3(0.0f, 50.0f, 5000.0f),
                              Vector3.Zero, Vector3.Up);
                            effect.Projection = Matrix.CreatePerspectiveFieldOfView(
                                MathHelper.ToRadians(45.0f), aspectRatio,
                                1.0f, 10000.0f);
                            view1 = Matrix.CreateLookAt(new Vector3(0.0f, 50.0f, 5000.0f),
                              Vector3.Zero, Vector3.Up);
                            projection1 = Matrix.CreatePerspectiveFieldOfView(
                                MathHelper.ToRadians(45.0f), aspectRatio,
                                1.0f, 10000.0f);
                        }                   
                        meshh.Draw();
                        DebugShapeRenderer.AddBoundingSphere(sphere, Microsoft.Xna.Framework.Color.Pink);
                        DebugShapeRenderer.Draw(gameTime, view1, projection1);
                    }
                    sphere = sphere.Transform(enemymat);
                    //sphere.Center = position;
                    //sphere.Radius = 0.1f;
              
                }
            }
    DebugShapeRenderer is the class I am using to draw the sphere !
    and here is the code for the player:
    Code:
     protected override void Draw(GameTime gameTime)
            {
                GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.Black);
                if (texture != null)
                {
                    spriteBatch.Begin(0, BlendState.Opaque);
                    spriteBatch.Draw(texture, new Vector2(0, 0), Microsoft.Xna.Framework.Color.White);
                    for (int i = 0; i < shoots.Count; i++)
                    {
                        shoots[i].Draw(spriteBatch);
                    }
                    spriteBatch.End();
                    GraphicsDevice.BlendState = BlendState.Opaque;
                    GraphicsDevice.DepthStencilState = DepthStencilState.Default;
                    #region 3D model tower
                    if ((myModel != null) && (myModel.Meshes.Count > 0) && transformation == true)
                    {
                        Matrix[] transforms = new Matrix[myModel.Bones.Count];
                        myModel.CopyAbsoluteBoneTransformsTo(transforms);
                        Matrix rotation = Matrix.CreateFromYawPitchRoll(-yaw, -pitch, roll);
                        Matrix translation = Matrix.CreateTranslation(
                        modelTransformaton.V03, modelTransformaton.V13, -modelTransformaton.V23);
                        Matrix scaling = Matrix.CreateScale(90);
                        foreach (ModelMesh mesh in myModel.Meshes)
                        {
                            if (modelsp.Radius == 0)
                                modelsp = mesh.BoundingSphere;
                            else
                                modelsp = BoundingSphere.
                                         CreateMerged(modelsp, mesh.BoundingSphere);
                            DebugShapeRenderer.AddBoundingSphere(mesh.BoundingSphere, Microsoft.Xna.Framework.Color.Green);
                            foreach (BasicEffect effect in mesh.Effects)
                            {
                                effect.EnableDefaultLighting();
                              world =  effect.World = Matrix.CreateLookAt(
                            new Microsoft.Xna.Framework.Vector3(0, 0, 3),
                                Microsoft.Xna.Framework.Vector3.Zero,
                                Microsoft.Xna.Framework.Vector3.Up);
    
                                effect.View = transforms[mesh.ParentBone.Index] *
                                Matrix.CreateScale(1 / mesh.BoundingSphere.Radius)
                                * scaling * rotation * translation;
                                effect.Projection = Matrix.CreatePerspective(
                                1, 1 / GraphicsDevice.Viewport.AspectRatio, 1f, 10000);
    
                                view1 = transforms[mesh.ParentBone.Index] *
                                Matrix.CreateScale(1 / mesh.BoundingSphere.Radius)
                                * scaling * rotation * translation;
    
                                projection1 = Matrix.CreatePerspective(
                                1, 1 / GraphicsDevice.Viewport.AspectRatio, 1f, 10000);
    
                                pos = Matrix.Invert(translation).Translation;
    
                            }
                            mesh.Draw();
                            DebugShapeRenderer.AddBoundingSphere(modelsp, Microsoft.Xna.Framework.Color.Beige);
                            DebugShapeRenderer.Draw(gameTime, view1, projection1);
                        }
                        //modelsp.Center = pos;
                        //modelsp.Radius = 0.5f;
                        modelsp = modelsp.Transform(view1);
    
                    }
                    #endregion
                    for (int i = 0; i < enemies.Count; i++)
                    {
                        enemies[i].Draw(gameTime);
                    }
                }
                base.Draw(gameTime);
            }
    This way I am trying to detect collision:
    Code:
     for (int i = enemies.Count - 1; i >= 0; i--)
                {
                    enemies[i].Update(gameTime);
                    if (enemies[i].sphere.Intersects(modelsp))
                    {
                        enemies[i].Speed = 0.0f;
                    }
                    if (enemies[i].Alive == false)
                    {
                        enemies.RemoveAt(i);
                    }
                }
    Where enemies is List<Enemy> and Enemy is the class.
    Any ideas what am I doing wrong ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width