I've looked at many collision detection technique for a 2D game but I can't find exactly what I'm looking for. My game is a brickbreaker kind of game.
I've looked at pixel to pixel collisions style but it looks complicated for what i'm trying to do.
Is there an easier way?
I'm using XNA.
Thanks in advance.
Have you tried looking into Rectangle collisions ?
You declare a Rectangle on your sprites and check if they Intersect to test for a collision. It's the easiest way I know.
Look here for explanations on how to use it.
Hope this helps.
Here is the correct way to do it.
Tessellate both objects into a set of convex sub regions. This can be done as a pre-processing step.
For each combination of two sub regions from object A and object B, do the following comparison:
a. Construct a skewed prism in N+1 dimensional space. This is the extrusion of the convex subregion as it moved from its location at time=0 to time=n. If the object rotated, you're SOL as this will not be a convex region.
b. Use the Simplex algorithm to determine if there is a feasible intersection of the two convex sub regions. Alternately you could use the ellipsoidal method or an interior traversal LP algorithm.
c. If any of the pairs of convex sub regions intersect, then there is a collision.
... or you can use axis-aligned rectangles like everyone else. If you need to speed them up, use interval trees.
Related
I'm developing a 2D program that requires a lot of judgment on whether a polygon (generated or imported by the user, could be either convex or concave) is intersecting (overlaping) with AABBs. The data structure part I've implemented with kdtree, and I now need a way to determine whether a polygon intersects with an AABB box.
I have seen many discussions on the web, but they are all about convex polygons, and I have also seen methods that use each line segment of a polygon to determine if it intersects with an AABB, but since it is an AABB box, I am afraid this method is too expensive.
1 The polygon is defined by a series of Vector2 list stored in counterclockwise order, AABB Box is defined by List[minx,miny,maxx,maxy].
2 I only care whether they intersect or not, I don't need to return overlaping ranges or intersection points.
3 Any pre-processing of polygons is possible.
4 The language used is C#, and any available libraries are welcome.
It is possible to convert any polygon into triangles, this is called triangulation. Then you can use any triangle/aabb intersection algorithm you want.
In addition to this, you can draw an aabb around the polygon and check that against the other aabb first. If those don't intersect you don't have to check all the triangles separately.
Furthermore, you can use a bounding volume hierarchy to prevent doing unnecessary collision checks of the triangles and aabb. This is a nice explanation and implementation but it's in the context of raytracing. You can easily adapt it for your purposes though.
I want to slice a 3D model relative to an infinite plane(In WPF). I'm checking if edges intersect with the infinite plane. If true, I'll create a new point at the intersection position, so I'm getting a couple of points that I want to generate a cap on so that the model is closed after slicing. For example, if this is the cross section, the result would be as follows:
Note: The triangulation ain't important. I just need triangles.
I also need to detect the holes as follows(holes are marked in red):
If it is impossible to do it the way I think(It seems to be so), the how should I do it? How do developers cap an object after being sliced?
There is also too much confusion. For example, The first picture's result may be:
What am I missing??
EDIT:
After some research, I knew one thing that I am missing:
The input is now robust, and I need the exact same output. How do I accomplish that??
In the past, I have done this kind of thing using a BSP.
Sorry to be so vague, but its not a a trivial problem!
Basically you convert your triangle mesh into the BSP representation, add your clipping plane to the BSP, and then convert it back into triangles.
As code11 said already you have too few data to solve this, the points are not enough.
Instead of clipping edges to produce new points you should clip entire triangles, which would give you new edges. This way, instead of a bunch of points you'd have a bunch of connected edges.
In your example with holes, with this single modification you'd get a 3 polygons - which is almost what you need. Then you will need to compute only the correct triangulation.
Look for CSG term or Constructive Solid Geometry.
EDIT:
If the generic CSG is too slow for you and you have clipped edges already then I'd suggest to try an 'Ear Clipping' algorithm.
Here's some description with support for holes:
https://www.geometrictools.com/Documentation/TriangulationByEarClipping.pdf
You may try also a 'Sweep Line' approach:
http://sites-final.uclouvain.be/mema/Poly2Tri/
And similar question on SO, with many ideas:
Polygon Triangulation with Holes
I hope it helps.
Building off of what zwcloud said, your point representation is ambiguous. You simply don't have enough points to determine where any concavities/notches actually are.
However, if you can solve that by obtaining additional points (you need midpoints of segments I think), you just need to throw the points into a shrinkwrap algorithm. Then at least you will have a cap.
The holes are a bit more tricky. Perhaps you can get away with just looking at the excluded points from the output of the shrinkwrap calculation and trying to find additional shapes in that, heuristically favoring points located near the centroid of your newly created polygon.
Additional thought: If you can limit yourself to convex polygons with only one similarly convex hole, the problem will be much easier to solve.
I'm running a marching squares (relative of the marching cubes) algorithm over an iso plane, then translating the data into a triangular mesh.
This works, but creates very complex mesh data. I would like to simplify this to the minimum triangles required, as seen illustrated below:
I have tried looping around contours (point -> segment -> point -> ...), but the contour can become inverted if a point has more than 2 attaching segments.
Ideally the solution should be fairly fast so that it can be done at runtime. The language I'm using is C#, but could probably port it from most other C-like languages.
This is a very common problem in 3D computer graphics. Algorithms solving this problem are called mesh simplification algorithms.
The problem is however much simpler to solve in the 2D case, as no surface normals need to be considered.
The first important thing is to have a reliable mesh data structure, that provides operations to modify the mesh. A set of operations, that can produce and modify any mesh is for instance the set of "Euler Operators".
To simplify a 2D mesh provide a dense version of the 2D mesh.
You can simply convert your quad mesh to a triangle mesh by
splitting each quad at its diagonal.
Dense triangle mesh:
Then iteratively collapse the shortest edges, which are not at the boundary.
"Edge collapse" is a typical mesh operation, depicted here:
After some steps your mesh will look like that:
Simplifying a mesh generated by marching squares (like suggested in the answers above) is highly inefficient. To get a polygon out of a scalar field (e.g. a bitmap) you should first run a modified version of marching squares that only generates the polygon contour (i.e. in the 16 cases of marching squares you don't generate geometry, you just add points to a polygon), and after that you run a triangulation algorithm (e.g. Delaunay or Ear Clipping).
Do it with an immediate step, go from your volume representation to the grid representation.
After that you can then group the areas with case 3,6,9,12 into bigger/longer versions.
You can aslo try to group squares into bigger squares, but its a NP problem so an ideal optimization could take a long time.
Then you can convert it into the polygon version.
After converting it to polygons you can fuse the edges.
EDIT: Use Ear Clipping on resulting contours:
http://www.geometrictools.com/Documentation/TriangulationByEarClipping.pdf
I'm using slimdx in c#, and my problem is a follows:
I have a list of vertices that forms a polygon, in linestrip format, and I need to transform it to a trianglestrip that covers the polygon.
I started with a center-of-mass calculation, however it only covers convex ones, and I need a general solution.
The final result should look smth like this:
Does anyone happens to know any algorithms for the issue?
Thanks.
There's ear clipping algorithm that is quite nice for your use case, an example can be found here:
Ear clipping c#
I am creating a CAD like program, creating modelvisual3D objects. How do i do collision detection between my objects(modelvisual3d) using MeshGeometry3D. Do i have to compare every triangle in the moving object against the still standing objects?
What will be my best way to do collision detection?
It depends on how precise your collision detection needs to be.
There is no built-in collision detection in WPF's 3D library. If you need high precision, you'll need to compare every triangle.
That being said, you can start with comparing bounding boxes and/or bounding spheres. This is always a good first step, since it can quickly eliminate most cases. If you don't need precision collision detection, this alone may be fine.
To add to Reed's answer (based on my answer here):
After you've eliminated most of your objects via the bounding box/sphere to bounding box/sphere test you should test the triangles of your test object(s) against the other object's bounding box/sphere first before checking triangle/triangle collisions. This will eliminate a lot more cases.
To rule out a collision you'll have to check all the triangles in the test object, but to find a case where you'll need to go down to the triangle/triangle case you only need to find the first triangle that interacts with the bounding box/sphere of the other object.
Look at the SAT theorem (Separating Axes Theorem), it's the fastest and easiest one out there.
The theory about this is that if you can draw a line which separates the triangles, then they're not colliding.
As is said, first do an AABB earlier detection, and when two objects collide, test each polygon of object A against each polygon of object B.
Starting in 2D, to test if two polygons collide, you get the extents of them in the possible axes (in this case X and Y), if those extents intersect, then the poligons are colliding.
On this page you can find a very good explanation on how it works and how to apply it:
http://www.metanetsoftware.com/technique/tutorialA.html
To apply it to 3D simply use the edges of each polygon as the separating axes.
If the extents on those axes intersect, then the polygons are colliding.
Also, this method resolves collission for moving objects, giving also the momentum of collision (resolve the relative angular velocity, substracting velocity B from velocity A, this way the problem is reduced to a moving object and a static one, and add the velocity in the axis you are testing to the extent of the polygon A, if they intersect, rest the original extent of the polygon and you will get the momentum of collission).
Another option would be to use BulletSharp, a C# wrapper of the well-known Bullet Physics Engine. In this case, you would need to write functions to create a (concave) collision shape from a MeshGeometry3D.
In my experience, it works pretty well, even though dynamic collision between concave shapes is not supported. You'll need to use convex decompsition, as a workaround.