2D Collision Detection and Physics

advertisement
Computer Game Design and
Development
Collision Detection
Havok Destruction
Overlap Bisection
Limitations of Overlap Testing
Intersection Testing
Collision Approximations
Minkowski Sum – sweep origin of X across Y
Performance
Possible collision
between R and B
since overlap in
all axis (2 in this case)
Subdivide such that on average
one object in each cell.
Per-Pixel Collision
// The color data for the images; used for per-pixel collision
Color[] personTextureData;
Color[] rocketTextureData;
// Load textures
rocketTexture = Content.Load<Texture2D>(“Rocket");
personTexture = Content.Load<Texture2D>("Person");
// Extract collision data
rocketTextureData = new Color[rocketTexture.Width * rocketTexture.Height];
rocketTexture.GetData(rocketTextureData);
personTextureData = new Color[personTexture.Width * personTexture.Height];
personTexture.GetData(personTextureData);
http://creators.xna.com/en-US/tutorial/collision2dperpixel
Per-Pixel Collision (cont)
static bool IntersectPixels(Rectangle rectangleA, Color[] dataA, Rectangle rectangleB, Color[] dataB) {
// Find the bounds of the rectangle intersection
int top = Math.Max(rectangleA.Top, rectangleB.Top);
int bottom = Math.Min(rectangleA.Bottom, rectangleB.Bottom);
int left = Math.Max(rectangleA.Left, rectangleB.Left);
int right = Math.Min(rectangleA.Right, rectangleB.Right);
// Check every point within the intersection bounds
for (int y = top; y < bottom; y++)
for (int x = left; x < right; x++) {
// Get the color of both pixels at this point
Color colorA = dataA[(x - rectangleA.Left) + (y - rectangleA.Top) * rectangleA.Width];
Color colorB = dataB[(x - rectangleB.Left) + (y - rectangleB.Top) * rectangleB.Width];
// If both pixels are not completely transparent,
if (colorA.A != 0 && colorB.A != 0) // then an intersection has been found
return true;
}
// No intersection found
return false;
}
Bounding Volumes
AABB
OBB
Terrain Collision
• Heightmap information
• Look up terrain height
• 3 LERPs – Linear Interpretation between
points
–X
–Z
– Between x and z
http://creators.xna.com/en-US/sample/collision3dheightmap
Resolving Collision
• Gross collision & subdivide if needed
• Three phases
– Prologue – ignore or trigger other events
– Collision – point of impact, new velocities
– Epilogue – destroy object, effects, damage
Download