Showing posts with label Tutorials. Show all posts
Showing posts with label Tutorials. Show all posts

Did you know?

That I had problems with drawing models?? Apparently, when the model is exported and processed in the content pipeline, all of it's meshes are positioned on 0, 0, 0 (X, Y, Z). So, if your model had a box at 1, 2, 4, when you draw it, it would draw at 0, 0, 0. So, what's the deal?? Suddenly, I discovered that they can be positioned the way you like, but you have to do something. There is an array of matrices, that will help you to do this. So, let's begin:

private Matrix[] transforms;

You will use that matrix, to alter the world matrix, and set it to the shader. Now, we need to fill it and resize it.

transforms = new Matrix[model.Bones.Count];
model.CopyAbsoluteBoneTransformsTo(transforms);

and when rendering your mesh parts, always set the new world matrix which is equal to the main world matrix multiplied with the transform. Here's a sample:

below, or see this (link)

foreach (ModelMesh modelMesh in model.Meshes)
{
effect.Parameters["World"].SetValue(transforms[modelMesh.ParentBone.Index] * Matrix.Identity);

effect.Begin();
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Begin();
foreach (ModelMeshPart meshPart in modelMesh.Parts)
{
device.VertexDeclaration = meshPart.VertexDeclaration;
device.Vertices[0].SetSource(modelMesh.VertexBuffer,
meshPart.StreamOffset, meshPart.VertexStride);

device.Textures[0] = meshMaterials[i].Texture;

device.DrawIndexedPrimitives(PrimitiveType.TriangleList,
meshPart.BaseVertex, 0, meshPart.NumVertices,
meshPart.StartIndex, meshPart.PrimitiveCount);
}
pass.Begin();
}
effect.End();
}

Note that I multiplied the transform with Matrix.Identity, you should swap that with your object matrix...
btw, you won't get another collision detection tutorial :( It's because you should do a million things besides it, and I'm too booooored to write about them :(
Sorry again

Warning: I have not tested any of this code. I've done it in my engine, but on another way so it's compatible with it. If you find any bugs, or you want to add something, say it! Thanks! Now, read up...
And also, these tutorials assume you have some knowledge in C#, and you know how to work with arrays and lists...

What is collision detection??? Well, without it, a model could pass through another model, bullets wouldn't collide with walls, enemies etc... It's very hard to do vertex perfect collisions, so, models are presented with boxes or spheres. XNA has BoundingBox and BoundingSphere classes, which we'll use to detect collision between different objects. So, here we go.
Create a new class called BoundingGroup. That class will do anything for us, it will first create the main bounding box, and then for each mesh in meshes, it will create a bounding box just for that mesh. First some variables:

private BoundingBox boundingBox = new BoundingBox();
private List boundingBoxes = new List();
private Matrix[] Transforms;

These variables will hold our main bounding box, and a list of our small bounding boxes and the transforms. Now make a method called CalculateBoxes(Model model) which looks like this:

http://paste.lisp.org/display/43066

That's all you need to do to get the bounding boxes, but, that's not all. You also need a field, to get the bounding boxes, and while getting them, you need to transform them to the current object position. So, for that, we'll use a method, and not a field.

http://paste.lisp.org/display/43066#1

And you should add a empty constructor BoundingGroup() just in case...
Now, to learn to use it. When you load the graphics content, and when you load your model, make a new bounding group, and then call CalculateBoxes method passing the model you just loaded.
When you check for collisions, check the main bounding boxes first, and if there is collision, check everything else. To check for collisions, use the BoundingBox.Intersects method.

In the next tutorials, you'll see how to get bounding spheres from the models you want...

And of course, do not stop with this. You see that you need to pass the objects position, rotation and scaling everytime you want to get the boxes. Try to edit the class so you will bind it to a object, and then you won't need to pass the values, just read them from the object (that's what I did).


 

Copyright 2006| Blogger Templates by GeckoandFly modified and converted to Blogger Beta by Blogcrowds.
No part of the content or the blog may be reproduced without prior written permission.