# Collision Detection... Simplified?

This topic is 4830 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi there! Right now I've done a rather stupid solution... I'm checking the distance to every vertex of a model against a specified point... this works, but as you can imagine, if the triangle is big enough, there will be no collision... so; is it possible to adapt my cheap-poor-mans-excuse for collision detection, into a point in triangle test? I have a clue on how I should do it, but only in 2D... how should one translate the 3d positions into a flat triangle... I'm not that good at math, so a gentle and nice explanation would be really nice. :)

##### Share on other sites
To detect if a point is inside a triangle(or another convex polygon) you must first project the point on the polygon plane and then do a point_in_polygon test
code(I don't have my recent code here, so it's my old VB code :) ):

Public Function PlaneDistance(Normal As D3DVECTOR, vPoint As D3DVECTOR) As Single    PlaneDistance = -(D3DXVec3Dot(Normal, vPoint))End FunctionPublic Function AngleBetweenVectors(Vector1 As D3DVECTOR, Vector2 As D3DVECTOR) As Single    AngleBetweenVectors = ArcCos(D3DXVec3Dot(Vector1, Vector2) / (Magnitude(Vector1) * Magnitude(Vector2)))End FunctionPublic Function InsidePolygon(vIntersection As D3DVECTOR, Poly() As D3DVECTOR, verticeCount) As Boolean    Dim angle As Double    Dim vA As D3DVECTOR, vB As D3DVECTOR    Dim i As Integer    For i = 0 To verticeCount - 1        vA = VplusV(Poly(i), vIntersection, -1)        If i + 1 < verticeCount Then            vB = VplusV(Poly(i + 1), vIntersection, -1)        Else            vB = VplusV(Poly(0), vIntersection, -1)        End If                angle = angle + AngleBetweenVectors(vA, vB)    Next i        If angle >= (MATCH_FACTOR * (2 * PI)) Then        InsidePolygon = True    Else        InsidePolygon = False    End IfEnd FunctionPublic Function PointPolyIntersect(vPoly() As D3DVECTOR, vertCount As Long, vNorm As D3DVECTOR, vPoint As D3DVECTOR) As Boolean    Dim vProjPoint As D3DVECTOR    Dim dist As Single    dist = PlaneDistance(vNorm, vPoint)    vProjPoint = VplusV(vPoint, VxV(vNorm, Vec3D(1, 1, 1), dist))        PointPolyIntersect = InsidePolygon(vProjPoint, vPoly(), vertCount)   End Function

##### Share on other sites
I would suggest using hit boxes or even hit spheres for your models. The problem is that a point practically never hits a triangle. It is also very hard to determine if a point is inside your model.

If you want to use your original way of detecting collisins you can always automatically add points inside the bigger triangles.

##### Share on other sites
yeah, the problem in 3D is that a triangle has no volume, so has a point, and the collision detection works with volumes (Am I inside / outside an object?).

you'll need some tolerances, if you want to do vertex-triangle only tests.

Secondly, there is a problem with edges in 3D. Two boxes could collide with only two edges touching. But that's not so bad. YOu can detect if an edge intersects triangles or not.

Consequently, you may have better luck working with edges intersecting triangles, detecting that would be much better than just vertices, but it's more work.

It's quite a bit more complicated in 3D.

##### Share on other sites
Thanx guys, for all your replies! :)

I've did some stress testing for my collision detection, and at 15 points to check it starts to behave rather nasty... so I think I will go for another approach, boxes and spheres... :)

Don't really know if it's worth it, but I'm thinking along the lines of this:

1) Artist creates 3d scene.

2) Artist (or someone else) create a collision map, with oriented boxes, regular boxes and spheres...

3) Type, position, rotation and size are saved into a mapfile.

depending on where the object which should be checked for collisions is positioned, the corresponding area in the collision mapfile is checked...

I've got the boxes (and spheres, just hypot... ) up and running... the problem is oriented boxes... but I guess, you could simply align the point to check with the boxes using regular sin and cos functions...?

1. 1
Rutin
28
2. 2
3. 3
4. 4
5. 5

• 13
• 11
• 10
• 13
• 20
• ### Forum Statistics

• Total Topics
632950
• Total Posts
3009424
• ### Who's Online (See full list)

There are no registered users currently online

×