关于3D游戏中碰撞的问题,请教高手!!!!!
小弟最近看一个3d游戏中的碰撞,希望哪位大虾告诉小弟这是使用的什么算法。
Private Function GameKeyboard(yLookDir As D3DVECTOR) As Boolean
Dim KeybState As DIKEYBOARDSTATE
Dim yWalkDir As D3DVECTOR, yPosBef As D3DVECTOR, yCollDet As D3DVECTOR
Dim RotAngle!, cntEnable%
Mk3d.diDeviceKeyb.GetDeviceStateKeyboard KeybState
If Not KeybState.Key(1) = 0 Then
'ESC, go to the menu
Do
DoEvents
Mk3d.diDeviceKeyb.GetDeviceStateKeyboard KeybState
Loop While Not KeybState.Key(1) = 0 'wait until ESC is no longer pressed
GameKeyboard = True
Exit Function
End If
RotAngle = GetMoveAngle(KeybState)
If Not RotAngle = -1 Then
yWalkDir = yLookDir
yWalkDir.y = 0
Mk3d.dx.VectorNormalize yWalkDir
yWalkDir = Mk3d.VectorRotate(yWalkDir, Mk3d.VectorMake(0, RotAngle, 0))
Mk3d.dx.VectorScale yWalkDir, yWalkDir, WalkSpeed * FrameT
'碰撞检测
yPosBef = yPos
Mk3d.dx.VectorAdd yPos, yPos, yWalkDir
yCollDet = GetCollDet(yPosBef, yPos, 2, False, True)
If yCollDet.x = 0 Then
yWalkDir.x = 0
yPos = yPosBef
cntEnable = cntEnable + 1
End If
If yCollDet.z = 0 Then
yWalkDir.z = 0
yPos = yPosBef
cntEnable = cntEnable + 1
End If
If cntEnable = 1 Then
'only if of the two values is zero. if both are zero, you can't walk in ANY direction
Mk3d.dx.VectorNormalize yWalkDir
Mk3d.dx.VectorScale yWalkDir, yWalkDir, WalkSpeed * FrameT
Mk3d.dx.VectorAdd yPos, yPos, yWalkDir
End If
Mk3d.dx.VectorAdd yEyes, yEyes, yWalkDir
If Not cntEnable = 2 Then
'start playing the walk sound
dsWalkSound.Play DSBPLAY_LOOPING
Else
'stop playing the walk sound
dsWalkSound.Stop
dsWalkSound.SetCurrentPosition 0
End If
'move also MG
If Not MGHoldingState = MG_NONE Then
Mk3d.dx.VectorAdd MGHoldingPos, MGHoldingPos, yWalkDir
MGHolding.MoveTo MGHoldingPos
End If
Else
'stop playing the walk sound
dsWalkSound.Stop
dsWalkSound.SetCurrentPosition 0
End If
End Function
Private Sub GameMouse()
Dim MouseState As DIMOUSESTATE, yRot As D3DVECTOR
Mk3d.diDeviceMouse.GetDeviceStateMouse MouseState
yRot.x = MouseState.y * 6.283 / PixelPer360
yRot.y = MouseState.x * 6.283 / PixelPer360
yAngle.x = yAngle.x - yRot.x
yAngle.y = yAngle.y - yRot.y
If yAngle.x > 1 Then yAngle.x = 1
If yAngle.x < -1 Then yAngle.x = -1
If yAngle.y > 6.283 Then yAngle.y = yAngle.y - 6.283
If yAngle.y < 0 Then yAngle.y = yAngle.y + 6.283
If MGHoldingState = MG_NORMAL Or MGHoldingState = MG_FIRE Then
If Not MouseState.buttons(0) = 0 And MGHoldingState = MG_NORMAL Then
MGHoldingState = MG_FIRE
'start playing the MG sound
dsShootSound.SetCurrentPosition 0
dsShootSound.Play DSBPLAY_LOOPING
ElseIf MouseState.buttons(0) = 0 Then
MGHoldingState = MG_NORMAL
Mk3d.LightSetState MGHoldingLightIndex, False
yEyes.y = EyesHeight
'stop playing the MG sound
dsShootSound.Stop
End If
End If
End Sub