Hi,

... but we can add incorporate some of the DirectX Utilities.

The code below is not fully checked, but basically, the concept is to answer to the question: is a

ray (of no dimension) hits a triangle or not. If the ray starts at time t=0 from the position

RayStart, with a speed Velocity, and if the target is not moving (from time t=0 to after the

collision) and is in position defined by the triangle defined by the three points TargetA, TargetB

and TargetC, then, supply those inputs to RayInterceptTriangle and it if it returns TRUE, then,

there is a hit (time and position of the hit returned by the last two arguments).

See Testing( ), the last procedure, to have the example I have tested.

See comments after the code.

=========Have a reference to DirectX 8 for Visual Basic Type Library

Option Explicit

Public Function RayInterceptTriangle( _

RayStart As D3DVECTOR, _

Velocity As D3DVECTOR, _

TargetA As D3DVECTOR, _

TargetB As D3DVECTOR, _

TargetC As D3DVECTOR, _

ByRef time As Single, _

ByRef HitPoint As D3DVECTOR) _

As Boolean

' Determine if a ray starting at point RayStart, with

' a constant velocity Velocity,

' hit the surface of an immobile triangular target

' defined by the 3 points TargetA, TargetB and TargetC.

' If there is a hit, the time of the hit

' is returned in time (t=0 when the ray bullet is at RayStart)

' and the exact hit point in the triangle is returned in

' HitPoint

' Solve a system of 3 equations, 3 unknowns.

RayInterceptTriangle = False 'assume no interception.

' Compute the determinant, using D3D utility functions

Dim Determinant As Single

Dim v21 As D3DVECTOR

Dim v31 As D3DVECTOR

D3DXVec3Subtract v21, TargetB, TargetA

D3DXVec3Subtract v31, TargetC, TargetA

Dim temp As D3DVECTOR

D3DXVec3Cross temp, v21, v31

Dim v0 As D3DVECTOR

v0.x = -Velocity.x

v0.y = Velocity.y

v0.z = -Velocity.z

Determinant = D3DXVec3Dot(v0, temp)

If 0.000001 > Abs(Determinant) Then

' too imprecise or ray almost

' // to the target

Exit Function

End If

' Find the time t at which the collision occur

D3DXVec3Subtract v0, TargetA, RayStart

v0.y = -v0.y

time = -D3DXVec3Dot(v0, temp) / Determinant

If time < 0 Then

' The bullet does not fire backward

Exit Function

End If

' We hit the plane in which the triangle lay, do we

' hit the inside of the triangle itself?

' Here, we will use standard algebra

Dim r As Single

Dim s As Single

r = -Velocity.x * (v0.y * v31.z - v0.z * v31.y) _

+ Velocity.y * (v0.x * v31.z - v0.z * v31.x) _

- Velocity.z * (v0.z * v31.y - v0.y * v31.x)

r = r / Determinant

If r < 0 Then

' The bullet passes "below/above/aside" the side A-B

Exit Function

End If

s = -Velocity.x * (v21.y * -v0.z - v21.z * v0.y) _

+ Velocity.y * (v21.x * -v0.z - v21.z * -v0.x) _

- Velocity.z * (v21.x * v0.y - v21.y * -v0.x)

s = s / Determinant

If s < 0 Then

' The bullet passes "below/above/aside" the side A-C

Exit Function

End If

If r + s > 1 Then

' The bullet passes "below/above/aside" the side B-C

Exit Function

End If

' We have a hit.

RayInterceptTriangle = True

' Re-use v0, v21 and v31 to store the

' temporary results as we built it

D3DXVec3Scale v21, v21, r

D3DXVec3Scale v31, v31, s

D3DXVec3Add v0, v21, v31

D3DXVec3Add v0, TargetA, v0

HitPoint = v0

End Function

Public Function M3D(ByVal x As Single, _

ByVal y As Single, _

ByVal z As Single) As D3DVECTOR

' Constructor for D3DVector:

' Take 3 singles and returns one D3DVECTOR

' easy, use the result variable already created for us.

M3D.x = x

M3D.y = y

M3D.z = z

End Function

Public Sub Testing()

Dim v1 As D3DVECTOR

Dim t As Single

If RayInterceptTriangle(RayStart:=M3D(2, 2, 2), _

Velocity:=M3D(0, 0, -1), _

TargetA:=M3D(3, 0, 0), _

TargetB:=M3D(2, 4, 0), _

TargetC:=M3D(1, 0, 0), _

time:=t, HitPoint:=v1) Then

Debug.Print "hit at (", v1.x, v1.y, v1.z, "), at time=", t

Else

Debug.Print "No hit"

End If

End Sub

===================================

The theory is just about solving 3 eq. with 3 unknowns. I used the determinant method. The

vectorial equation (3 equations if we expand in x, y and z directions) is:

Start + Velovity * time = PointA + r * (PointB-PointA) + s * (PointC-PointA)

with time, r and s simple scalar (non directional, "standard" value) and the other are 3D vectors.

To have a real hit, additional inequations have to be respected: time >= 0, r >=0, s>=0 and

r+s <= 1.0

If your bullet does not rotate on itself, for a rectangular bullet, just test the FOUR corners,

each being from a different StartingPoint. So, if the bullet is a square 2 by 2, starting centered

at (0 0 0), in the z direction, then, have a ray starting at (.7071, .7071, 0), another at (-.7071,

.7071), a third at (-.7071, -.7071) and the fourth at (-.7071, .7071). If any ray hit the boat,

at least some part of the bullet hit the targetted-triangle. If the target is made of multiple

triangle, test for each triangle, one at a time. Alternatively, if the bullet is a circle, change

the last three inequations to:

r+radius >=0, s+radius>=0, r+s-radius <= 1.0

and make just one test with the ray starting at the center of the circle.

Hoping it may help,

Vanderghast, Access MVP

Quote:

> this is a general graphics question, and not related to DirectX.

> perhaps an algorithms related group would be a better resource.

> a google search might also prove fruitful.

> --

> Phil Taylor

> PM : DirectX SDK, Managed DirectX, Windows XP Inbox 3D screensavers, and a

> few more bits and bobs.

> http://msdn.microsoft.com/directx

> This posting is provided "AS IS" with no warranties, and confers no rights.

> > how would u fix collision detection?

> > i got 2 ships on eitherside of the screen...

> > Type tShip

> > X as integer

> > Y as integer

> > end type

> > type tBullet

> > X as integer

> > Y as integer

> > Who as Boolean ' true = ship on the left side's bullet, false ship on the

> > right side's bullet

> > InTraffic as Boolean ' true = bullet is in game and needs to be moved

> > end type

> > dim ship1 as tship

> > dim ship2 as tship

> > dim bullet(1 to 50) as sbullet

> > the ship bmp's are 100 width and 75 height, bullets are 8 width and 8

> > height

> > regards patrick