Results 1 to 4 of 4

Thread: Collision Detection (3D Sphere to Triangle) - Need optimization/faster method

Threaded View

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    10

    Collision Detection (3D Sphere to Triangle) - Need optimization/faster method

    so any of you know any thing about 3D Sphere to Triangle Collision Detection i know a little but i need it to run faster.
    this is the code i used:
    VB Code:
    1. Public Function STCD(TrianglePoint1 As D3DVECTOR, TrianglePoint2 As D3DVECTOR, TrianglePoint3 As D3DVECTOR, SphereCenter As D3DVECTOR) As D3DVECTOR
    2. Dim TV0 As D3DVECTOR, tv1 As D3DVECTOR, tv2 As D3DVECTOR, tv3 As D3DVECTOR
    3. Dim NTTV0 As D3DVECTOR, NTTV1 As D3DVECTOR, NTTV2 As D3DVECTOR, NTTV3 As D3DVECTOR
    4. Dim NTTV4 As D3DVECTOR, NTTV5 As D3DVECTOR, NTTV9 As D3DVECTOR, NTTV10 As D3DVECTOR
    5. Dim NTTV11 As D3DVECTOR, NTTV12 As D3DVECTOR, NTTV13 As D3DVECTOR, NTTV14 As D3DVECTOR
    6. Dim NTTV15 As D3DVECTOR, NTTV16 As D3DVECTOR, NTTV17 As D3DVECTOR, NTTV18 As D3DVECTOR
    7. Dim NTTV19 As D3DVECTOR, NTTV20 As D3DVECTOR, NTTV21 As D3DVECTOR, NTTV22 As D3DVECTOR
    8. Dim NTTV23 As D3DVECTOR, NTTV24 As D3DVECTOR, NTTV25 As D3DVECTOR, NTTV26 As D3DVECTOR
    9. Dim NTTV27 As D3DVECTOR, NTTV28 As D3DVECTOR, NTTV29 As D3DVECTOR, NTTV30 As D3DVECTOR
    10. Dim NTTV31 As D3DVECTOR
    11. D3DXVec3Subtract TV0, TrianglePoint1, TrianglePoint1
    12. D3DXVec3Subtract tv1, TrianglePoint2, TrianglePoint1
    13. D3DXVec3Subtract tv2, TrianglePoint3, TrianglePoint1
    14. D3DXVec3Subtract tv3, SphereCenter, TrianglePoint1
    15. D3DXVec3Subtract NTTV0, tv1, TV0
    16. D3DXVec3Subtract NTTV1, tv2, tv1
    17. D3DXVec3Subtract NTTV2, tv2, TV0
    18. D3DXVec3Cross NTTV3, NTTV2, NTTV0
    19. D3DXVec3Normalize NTTV3, NTTV3
    20. D3DXVec3Scale NTTV4, NTTV3, D3DXVec3Dot(tv3, NTTV3)
    21. D3DXVec3Subtract NTTV5, tv3, NTTV4
    22. D3DXVec3Subtract NTTV9, tv1, TV0
    23. D3DXVec3Cross NTTV10, NTTV9, NTTV3
    24. D3DXVec3Normalize NTTV10, NTTV10
    25. D3DXVec3Subtract NTTV9, tv2, tv1
    26. D3DXVec3Cross NTTV11, NTTV9, NTTV3
    27. D3DXVec3Normalize NTTV11, NTTV11
    28. D3DXVec3Subtract NTTV9, tv2, TV0
    29. D3DXVec3Cross NTTV12, NTTV9, NTTV3
    30. D3DXVec3Normalize NTTV12, NTTV12
    31. D3DXVec3Scale NTTV0, NTTV0, 0.5
    32. D3DXVec3Scale NTTV1, NTTV1, 0.5
    33. D3DXVec3Scale NTTV2, NTTV2, 0.5
    34. D3DXVec3Add NTTV0, NTTV0, TV0
    35. D3DXVec3Add NTTV1, NTTV1, tv1
    36. D3DXVec3Add NTTV2, NTTV2, TV0
    37. D3DXVec3Subtract NTTV16, NTTV5, NTTV0
    38. D3DXVec3Scale NTTV4, NTTV10, D3DXVec3Dot(NTTV16, NTTV10)
    39. D3DXVec3Subtract NTTV13, NTTV16, NTTV4
    40. D3DXVec3Add NTTV13, NTTV13, NTTV0
    41. D3DXVec3Subtract NTTV17, NTTV5, NTTV1
    42. D3DXVec3Scale NTTV4, NTTV11, D3DXVec3Dot(NTTV17, NTTV11)
    43. D3DXVec3Subtract NTTV14, NTTV17, NTTV4
    44. D3DXVec3Add NTTV14, NTTV14, NTTV1
    45. D3DXVec3Subtract NTTV18, NTTV5, NTTV2
    46. D3DXVec3Scale NTTV4, NTTV12, D3DXVec3Dot(NTTV18, NTTV12)
    47. D3DXVec3Subtract NTTV15, NTTV18, NTTV4
    48. D3DXVec3Add NTTV15, NTTV15, NTTV2
    49. D3DXVec3Subtract NTTV19, NTTV5, NTTV13
    50. D3DXVec3Normalize NTTV19, NTTV19
    51. D3DXVec3Subtract NTTV20, NTTV5, NTTV14
    52. D3DXVec3Normalize NTTV20, NTTV20
    53. D3DXVec3Subtract NTTV21, NTTV5, NTTV15
    54. D3DXVec3Normalize NTTV21, NTTV21
    55. D3DXVec3Subtract NTTV22, tv1, TV0
    56. D3DXVec3Subtract NTTV23, tv1, tv2
    57. D3DXVec3Subtract NTTV24, tv2, TV0
    58. D3DXVec3Subtract NTTV25, NTTV13, TV0
    59. D3DXVec3Subtract NTTV26, tv1, NTTV13
    60. D3DXVec3Subtract NTTV27, NTTV14, tv2
    61. D3DXVec3Subtract NTTV28, tv1, NTTV14
    62. D3DXVec3Subtract NTTV29, NTTV15, TV0
    63. D3DXVec3Subtract NTTV30, tv2, NTTV15
    64. If Abs(NTTV22.x) + Abs(NTTV22.y) + Abs(NTTV22.z) < Abs(NTTV25.x) + Abs(NTTV25.y) + Abs(NTTV25.z) Then NTTV13 = tv1
    65. If Abs(NTTV22.x) + Abs(NTTV22.y) + Abs(NTTV22.z) < Abs(NTTV26.x) + Abs(NTTV26.y) + Abs(NTTV26.z) Then NTTV13 = TV0
    66. If Abs(NTTV23.x) + Abs(NTTV23.y) + Abs(NTTV23.z) < Abs(NTTV27.x) + Abs(NTTV27.y) + Abs(NTTV27.z) Then NTTV14 = tv1
    67. If Abs(NTTV23.x) + Abs(NTTV23.y) + Abs(NTTV23.z) < Abs(NTTV28.x) + Abs(NTTV28.y) + Abs(NTTV28.z) Then NTTV14 = tv2
    68. If Abs(NTTV24.x) + Abs(NTTV24.y) + Abs(NTTV24.z) < Abs(NTTV29.x) + Abs(NTTV29.y) + Abs(NTTV29.z) Then NTTV15 = tv2
    69. If Abs(NTTV24.x) + Abs(NTTV24.y) + Abs(NTTV24.z) < Abs(NTTV30.x) + Abs(NTTV30.y) + Abs(NTTV30.z) Then NTTV15 = TV0
    70. D3DXVec3Scale NTTV19, NTTV19, 1000
    71. D3DXVec3Scale NTTV20, NTTV20, 1000
    72. D3DXVec3Scale NTTV21, NTTV21, 1000
    73. D3DXVec3Scale NTTV10, NTTV10, 1000
    74. D3DXVec3Scale NTTV11, NTTV11, 1000
    75. D3DXVec3Scale NTTV12, NTTV12, 1000
    76. D3DXVec3Scale NTTV3, NTTV3, 1000
    77. If (Int(NTTV21.x) = Int(NTTV12.x) And Int(NTTV21.y) = Int(NTTV12.y) And Int(NTTV21.z) = Int(NTTV12.z)) Then
    78. STCD = NTTV15
    79. ElseIf Not (Int(NTTV19.x) = Int(NTTV10.x) And Int(NTTV19.y) = Int(NTTV10.y) And Int(NTTV19.z) = Int(NTTV10.z)) Then
    80. STCD = NTTV13
    81. ElseIf Not (Int(NTTV20.x) = Int(NTTV11.x) And Int(NTTV20.y) = Int(NTTV11.y) And Int(NTTV20.z) = Int(NTTV11.z)) Then
    82. STCD = NTTV14
    83. Else
    84. STCD = NTTV5
    85. End If
    86. D3DXVec3Subtract NTTV31, NTTV5, tv3
    87. D3DXVec3Normalize NTTV31, NTTV31
    88. D3DXVec3Scale NTTV31, NTTV31, 1000
    89. If Not (Int(NTTV31.x) = Int(NTTV3.x) And Int(NTTV31.y) = Int(NTTV3.y) And Int(NTTV31.z) = Int(NTTV3.z)) Then STCD.x = 0: STCD.y = 0: STCD.z = 0
    90. D3DXVec3Subtract STCD, tv3, STCD
    91. End Function
    this is how it works it projects a point on to the plane of the triangle then projects that
    point on to three other planes one for each edge of the triangle then figures out which
    edge the point has passed.
    if it does pass an edge then it gives the point that was projected onto that edge.
    otherwise it gives the point that was projected on to the triangle.
    Last edited by NoituloverRevolution; Nov 22nd, 2006 at 01:27 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width