Note that there are some explanatory texts on larger screens.

plurals
  1. POone model overlapping another, should be other way round
    text
    copied!<p>I'm currently trying to develop a 3D table tennis game. My problem is that when drawing the table and racket, the table is currently overlapping the racket, which should be the other way round. Could anyone help please? </p> <p>Heres part of the code:</p> <pre><code> Model table; Model racket; Vector3 modelPosition = new Vector3(0,100,150); Vector3 modelPosition_table = new Vector3(0,40,0); // Set the position of the camera in world space, for our view matrix. Vector3 cameraPosition_racket = new Vector3(0.0f, 1000.0f, 10.0f); Vector3 cameraPosition_table = new Vector3(0.0f, 150.0f, 250.0f); void Draw_Racket() { // Copy any parent transforms. Matrix[] transforms_racket = new Matrix[racket.Bones.Count]; racket.CopyAbsoluteBoneTransformsTo(transforms_racket); // Draw the model. A model can have multiple meshes, so loop. foreach (ModelMesh mesh in racket.Meshes) { // This is where the mesh orientation is set, as well // as our camera and projection. foreach (BasicEffect effect in mesh.Effects) { effect.EnableDefaultLighting(); effect.World = transforms_racket[mesh.ParentBone.Index] * Matrix.CreateRotationY(modelRotation) * Matrix.CreateTranslation(modelPosition); effect.View = Matrix.CreateLookAt(cameraPosition_racket, Vector3.Up, Vector3.Up); effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), aspectRatio, 1.0f, 10000.0f); } // Draw the mesh, using the effects set above. mesh.Draw(); } } void Draw_Table() { // Copy any parent transforms. Matrix[] transforms_table = new Matrix[table.Bones.Count]; table.CopyAbsoluteBoneTransformsTo(transforms_table); // Draw the model. A model can have multiple meshes, so loop. foreach (ModelMesh mesh in table.Meshes) { // This is where the mesh orientation is set, as well // as our camera and projection. foreach (BasicEffect effect in mesh.Effects) { effect.EnableDefaultLighting(); effect.World = transforms_table[mesh.ParentBone.Index] * Matrix.CreateTranslation(modelPosition_table); effect.View = Matrix.CreateLookAt(cameraPosition_table, Vector3.Down, Vector3.Up); effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), aspectRatio, 1.0f, 1000.0f); } // Draw the mesh, using the effects set above. mesh.Draw(); } } protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); Draw_Table(); Draw_Racket(); base.Draw(gameTime); } </code></pre> <p>If you would need any more of the code just let me know. Thanks in advance :)</p> <p>EDIT...</p> <p>@Pablo Ariel..ye I understand now and realized that instead of trying to change the camera, I could have rotated the racket at the x-axis, and managed to sort of get what i want, but now one prob is that the racket is a bit to big..heres the new code..</p> <pre><code> // Set the 3D model to draw. Model table; Model racket; // The aspect ratio determines how to scale 3d to 2d projection. float aspectRatio; // Set the position of the model in world space, and set the rotation. Vector3 modelPosition = new Vector3(0,250,-25); Vector3 modelPosition_table = new Vector3(0,40,500); float modelRotation = (MathHelper.Pi*3)/2; Vector3 cameraPosition; Vector3 cameraTarget; Matrix mWorld; Matrix mWorld1; Matrix mView; Matrix mView1; Matrix mProjection; protected override void Initialize() { // TODO: Add your initialization logic here graphics.PreferredBackBufferWidth = 1000; graphics.PreferredBackBufferHeight = 500; graphics.IsFullScreen = false; graphics.ApplyChanges(); Window.Title = "Table Tennis 3D"; mProjection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), 1.0f, 1.0f, 10000.0f); cameraPosition = modelPosition; cameraTarget = modelPosition_table; modelPosition_table.Z += 40; mView = Matrix.CreateLookAt(cameraPosition, cameraTarget, Vector3.Up); cameraPosition.Z -= 300; modelPosition.Y -= 100; mView1 = Matrix.CreateLookAt(cameraPosition, cameraTarget, Vector3.Up); base.Initialize(); } protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); mWorld = Matrix.CreateRotationY(modelRotation) * Matrix.CreateRotationX(modelRotation) * Matrix.CreateTranslation(modelPosition); mWorld1 = Matrix.CreateTranslation(modelPosition_table); Draw_Table(); Draw_Racket(); base.Draw(gameTime); } void Draw_Table() { // Copy any parent transforms. Matrix[] transforms_table = new Matrix[table.Bones.Count]; table.CopyAbsoluteBoneTransformsTo(transforms_table); // Draw the model. A model can have multiple meshes, so loop. foreach (ModelMesh mesh in table.Meshes) { // This is where the mesh orientation is set, as well // as our camera and projection. foreach (BasicEffect effect in mesh.Effects) { effect.EnableDefaultLighting(); effect.World = transforms_table[mesh.ParentBone.Index] * mWorld1; effect.View = mView; effect.Projection = mProjection; } // Draw the mesh, using the effects set above. mesh.Draw(); } } void Draw_Racket() { Matrix[] transforms_racket = new Matrix[racket.Bones.Count]; racket.CopyAbsoluteBoneTransformsTo(transforms_racket); // Draw the model. A model can have multiple meshes, so loop. foreach (ModelMesh mesh in racket.Meshes) { // This is where the mesh orientation is set, as well // as our camera and projection. foreach (BasicEffect effect in mesh.Effects) { effect.EnableDefaultLighting(); effect.World = transforms_racket[mesh.ParentBone.Index] * mWorld; effect.View = mView1; effect.Projection = mProjection; } // Draw the mesh, using the effects set above. mesh.Draw(); } } </code></pre> <p>Should I make a new projection for the racket so that i change its size to half? I've tried changing its z position but no luck :/..thanks once again :) ps..heres a screenshot..http://i1099.photobucket.com/albums/g395/krt_ricci/3.png</p> <p>EDIT... I've multiplied my world matrix for the racket with Matrix.CreateScale(0.5f, 0.5f, 0.5f) so that the racket is scaled down by half :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload