Public xPos, yPos, zPos, xView, yView, zView, xUp, yUp, zUp, xStrafe, yStrafe, zStrafe As Single
Public Sub New(ByVal x As Single, ByVal y As Single, ByVal z As Single, _
ByVal xV As Single, ByVal yV As Single, ByVal zV As Single, _
ByVal xU As Single, ByVal yU As Single, ByVal zU As Single)
xPos = x
yPos = y
zPos = z
xView = xV
yView = yV
zView = zV
xUp = xU
yUp = yU
zUp = zU
End Sub
Private Sub UpdateCamera(ByVal xdir As Single, ByVal zdir As Single, ByVal dir As Single)
xPos += xdir * dir
zPos += zdir * dir
xView += xdir * dir
zView += zdir * dir
End Sub
Public Sub MoveCamera(ByVal amount As Single) 'Amount will be Negative for moving backwards
Dim xLookDirection, yLookDirection, zLookDirection As Single
xLookDirection = xView - xPos
yLookDirection = yView - yPos
zLookDirection = zView - zPos
Dim dp As Single = CSng(Math.Sqrt(xLookDirection * xLookDirection + yLookDirection * yLookDirection + zLookDirection * zLookDirection))
xLookDirection /= dp
yLookDirection /= dp
zLookDirection /= dp
UpdateCamera(xLookDirection, zLookDirection, amount)
End Sub
Public Sub StrafeCamera(ByVal direction As Single) 'Negative = Left, Positive = Right
CalculateStrafe()
UpdateCamera(xStrafe, zStrafe, direction)
End Sub
Private Sub CalculateStrafe()
Dim xDir, yDir, zDir As Single
Dim xCross, yCross, zCross As Single
xDir = xView - xPos
yDir = yView - yPos
zDir = zView - zPos
Dim dp As Single = 1 / CSng(Math.Sqrt(xDir * xDir + yDir * yDir + zDir * zDir))
xDir *= dp
yDir *= dp
zDir *= dp
xCross = (yDir * zUp) - (zDir * yUp)
yCross = (zDir * xUp) - (xDir * zUp)
zCross = (xDir * yUp) - (yDir * xUp)
xStrafe = xCross
yStrafe = yCross
zStrafe = zCross
End Sub
Public Sub RotateCamera(ByVal AngleDir As Single, ByVal xSpeed As Single, ByVal ySpeed As Single, ByVal zSpeed As Single) 'AngleDir will be Negative for rotating left, positive for right
Dim xNewLookDirection, yNewLookDirection, zNewLookDirection As Single
Dim xLookDirection, yLookDirection, zLookDirection As Single
Dim CosineAngle, SineAngle As Single
CosineAngle = CSng(Math.Cos(AngleDir))
SineAngle = CSng(Math.Sin(AngleDir))
xLookDirection = xView - xPos
yLookDirection = yView - yPos
zLookDirection = zView - zPos
Dim l As Single = CSng(Math.Sqrt(xLookDirection * xLookDirection + yLookDirection * yLookDirection + zLookDirection * zLookDirection))
xLookDirection /= l
yLookDirection /= l
zLookDirection /= l
xNewLookDirection = (CosineAngle + (1 - CosineAngle) * xSpeed) * xLookDirection
xNewLookDirection += ((1 - CosineAngle) * xSpeed * ySpeed - zSpeed * SineAngle) * yLookDirection
xNewLookDirection += ((1 - CosineAngle) * xSpeed * zSpeed + ySpeed * SineAngle) * zLookDirection
yNewLookDirection = ((1 - CosineAngle) * xSpeed * ySpeed + zSpeed * SineAngle) * xLookDirection
yNewLookDirection += (CosineAngle + (1 - CosineAngle) * ySpeed) * yLookDirection
yNewLookDirection += ((1 - CosineAngle) * ySpeed * zSpeed - xSpeed * SineAngle) * zLookDirection
zNewLookDirection = ((1 - CosineAngle) * xSpeed * zSpeed - ySpeed * SineAngle) * xLookDirection
zNewLookDirection += ((1 - CosineAngle) * ySpeed * zSpeed + xSpeed * SineAngle) * yLookDirection
zNewLookDirection += (CosineAngle + (1 - CosineAngle) * zSpeed) * zLookDirection
xView = xPos + xNewLookDirection
yView = yPos + yNewLookDirection
zView = zPos + zNewLookDirection
End Sub