Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I believe this is wrong:</p> <pre><code>Matrix.CreateLookAt(cameraPosition_racket, Vector3.Up, Vector3.Up); </code></pre> <p>it should be:</p> <pre><code>Matrix.CreateLookAt(cameraPosition, cameraTarget, Vector3.Up); </code></pre> <p>A recommendation I would like to make is that you should not use so big numbers such as 1000.0f as positions unless of course this is required for your game architecture, but using a smaller scale is better because of float imprecision.</p> <p>If you put the camera where the racket is then your camera may then be inside the racket and you won't be able to see it (because of backface culling you can't see faces from inside the racket)</p> <p>Other than that, I can't figure out from this code what could be wrong, but the wrong view matrix could invert your z values in a way which messes up the depth buffer. </p> <p>It's complicated because I don't understand what the camera vectors should be, you should use 3 vectors for the camera: cameraPosition, cameraTarget and cameraUp, then it will be clearer where you want the camera point to, and if you change the camera values you don't have to modify the matrix creation but just assign another vector.</p> <p>EDIT: Look there are some things you have to change</p> <p>First of all, calculating the view and projection matrix (which are the same for EVERY mesh in the scene) for each bone is very bad because generating the matrix is REAALLLLYYY SLOW <strong>SPECIALLY IN C#</strong> so you should manage to just do this: <code>effect.View = mView;</code> and the same with the projection matrix. </p> <p>This is the reason of you error, because of generating the matrix so many different times, you are building 2 different projection matrices: </p> <p>yo do this in DrawRacket():</p> <pre><code>effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), aspectRatio, 1.0f, 10000.0f); </code></pre> <p>and in DrawTable():</p> <pre><code>effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), aspectRatio, 1.0f, 1000.0f); </code></pre> <p>You're mapping the table mesh to 1 to 1000 and the racket to 1 to 10k Then all coordinates get messed up because of a single zero, when you need to use the same projection matrix for every object, so you should store it as a globabl variable or howeever it's handled in c# (a member variable or something like that)</p> <p>EDIT 2: </p> <p>Of course you MUST use the same camera for both objects!! if you don't then every object will be drawn as if the camera were in a different place for each!</p> <p>This is your fixed 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); Vector3 cameraPosition = cameraPosition_racket; Vector3 cameraTarget = new Vector3(0.0f, 0.0f, 0.0f); Matrix mView = Matrix.CreateLookAt(cameraPosition, cameraTarget, Vector3.Up); Matrix mProjection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), aspectRatio, 1.0f, 10000.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 = mView; effect.Projection = mProjection; } // 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 = mView; effect.Projection = mProjection; } // 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>UPDATE: try this:</p> <pre><code> Vector3 modelPosition_racket = new Vector3(0,100,100); // former modelPosition Vector3 modelPosition_table = new Vector3(0,40,500); // Set the position of the camera in world space, for our view matrix. Vector3 cameraPosition = modelPosition_racket; Vector3 cameraPosition.z -= 300; Vector3 cameraTarget = modelPosition_table; </code></pre> <p>UPDATE: What you have to understand is for you to properly show the image what you are doing with modelPosition is translating every object from its local coordinate space to the global/world coordinate space (each model has its own modelPosition). There all objects share the same coordinate space and the same projection transform. In the global/world coordinate space you also specify the camera position, then build a matrix from the world transform values of the camera. If you want to move an object in the world space, you have to modify modelPosition.. this is not something i just "believe" is that way, that's how 3d works. If you modify other values to change the position of the object, such as the projection or view matrix, then that won't do you any good, because it doesn't work that way, all your math will be messed up and you won't be able to work properly.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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