130 likes | 280 Views
XNA Physics. Collision, Gravity, Etc…. From: http ://rbwhitaker.wikidot.com/game-physics-tutorials. Collision Detection. Collision detection is often a big deal in games. It is important to know when objects in a game collide.
E N D
XNA Physics Collision, Gravity, Etc… From: http://rbwhitaker.wikidot.com/game-physics-tutorials
Collision Detection • Collision detection is often a big deal in games. It is important to know when objects in a game collide. • Collision detection can simply be done by comparing each polygon of one model with each polygon of every other model. This is intractably too expensive. • Typically, collision detection is first estimated via bounding boxes or bounding spheres.
Collision Detection Methods • One drawback to estimation is that it may not be a good approximation for an object. • For instance, imagine you have a long object, like the arrow. A large bound volumn, with much empty space, would be required to cover the entire object. More empty space, then, results in more false positives. • One common solution is to approximate the object with multiple bounding volumns. This allows for a better approximation of the object, as shown. • Drawback is more volumn comparisons, which is less efficient for distant objects.
Collision Detection Methods • A compromise would be to use nested, hierarchical bounding volumns, where you use one large sphere or box, and subdivide with positive hits, until finally doing a polygon-level comparison. • The draw-back is the complexity of code required to manage all of the nested spheres. • Another improvement to this method is to use multiple types of approximation techniques together. • For instance, you can combine the bounding spheres with bounding boxes, or provide the ability to use either.
Collision Detection with Bounding Spheres • The code to actually do collision detection is fairly simple, and it is easy to add to any game that you are already working on. • I've included some code below in case you don't have a place to already put it. This is the code that I will be using in this tutorial, as well. • It relies on stuff that we did in the using 3D models tutorial, as well as the simple 3D animation tutorial, and to a lesser degree, the BasicEffect lighting tutorial.
Collision Detection with Bounding Spheres • If you haven't gone through those tutorials, it might be helpful to go through them now. You should be able to copy and paste this code into a new game project. • Additionally, this code uses the SimpleShip model from the 3D Model Library, which you can download and include in your project like we have done before. • Add both the .fbx file and the texture file to your project, and then exclude the texture from your project so that it doesn't get handled twice by the content pipeline. You can use a model of your own instead, too, if you want.
Collision Detection with Bounding Spheres • When you run this code, you should see something similar to the image below, where two ships are moving along, one faster than the other. • When the fast one catches up to the slow one, it will pass through it and come out in front of it. • Here is ascreenshot of the program:
Collision Detection with Bounding Spheres • The actual code for collision detection is fairly simple. • We first need the two models that represent the objects that we are working with. • These will include BoundingSphereobjects for each of the meshes in the model. • In addition, we will need to know how the model has been transformed in the world, since the bounding spheres of the model are in model coordinates.
Collision Detection with Bounding Spheres • If the object in the game is located 20 units down the x-axis, we will also need to move our bounding sphere from the model 20 units down the x-axis. • The method used to create for collision detection will require two model objects and two Matrix objects for the transformations. • We will then compare each of the bounding spheres in one model to the bounding spheres in the other. • If any overlap, then we have a collision. If we get through them all without any overlaps, then there is no collision.
Collision Detection with Bounding Spheres • So here is the code to do this: private boolIsCollision(Model model1, Matrix world1, Model model2, Matrix world2) { for (int meshIndex1 = 0; meshIndex1 < model1.Meshes.Count; meshIndex1++) { BoundingSphere sphere1 = model1.Meshes[meshIndex1].BoundingSphere; sphere1 = sphere1.Transform(world1); for (int meshIndex2 = 0; meshIndex2 < model2.Meshes.Count; meshIndex2++) { BoundingSphere sphere2 = model2.Meshes[meshIndex2].BoundingSphere; sphere2 = sphere2.Transform(world2); if (sphere1.Intersects(sphere2)) return true; } } return false; }
Collision Detection with Bounding Spheres • In the Update() method, we will want to add some code to check for collisions between objects. • In the original code, there are only two ships in the scene that we are checking. If you have more objects, you would need to check each pair of objects. • Add the following code to your Update()method, which will call the collision detection method, and, if a collision is found, will restart the fast ship at its beginning location.
Collision Detection with Bounding Spheres Matrix ship1WorldMatrix = Matrix.CreateTranslation(ship1Location); Matrix ship2WorldMatrix = Matrix.CreateTranslation(ship2Location); if(IsCollision(model, ship1WorldMatrix, model, ship2WorldMatrix)) { ship1Location = new Vector3(0,-20,0); }
Gravity • When dealing with terrestrial games, you need to keep the piece on the playing field, which will likely be uneven. • To do this, a piece’s vector should include the gravity component: Vector3 gravity = new Vector3(0,-9,8,0); • Keep in mind that objects accelerate toward the center of Earth at 9.8 meters per second. Other planets would have different gravitational fields based on planetary mass.