Now you all were probably thinking I was dead, right? So I'm reborn from the ashes and bring with me another code bin pearl!
(background music comes in)
OK, seriously now. My next contribution for the future code bin is here. A module with (nearly) all the maths needed to do movements and camera rotations in a 3rd person 3D app/game using the keyboard, mouse or whatever you think of. Just a little note: since this is all about math. there is no graphics or input code AT ALL. I will just explain which values I took from the keyboard and mouse as an example.
- Keyboard: WASD keys used for movement. Use them to fire the Move/Strafe methods. Q/E keys for the secondary camera rotation.
- Mouse: X axis rotates the character/camera in the Y axis (change the CharRot variable). Y axis rotates the camera in the Z axis (change the CamAng variable). Mouse wheel controls camera zoom (change the CamDist variable).
I have attached the module but if you're lazy you can copy and paste everything from here.
Option Explicit
Public CharPosX As Single, CharPosZ As Single Public CharRot As Single
Public CamPosX As Single, CamPosY As Single, CamPosZ As Single
Public CamRot As Single Public CamAng As Single
Public CamDist As Single
Public Const CAM_HEIGHT As Single = 1.5
Private pSine(359) As Single
Private pCosine(359) As Single
Private Const PI As Single = 3.141593
Private Const RAD As Single = 0.01745329
Public Sub InitMath()
Dim i As Integer
For i = 0 To 359
pSine(i) = Sin(i * RAD)
pCosine(i) = Cos(i * RAD)
Next i
End Sub
Public Function Sine(ByVal Angle As Single) As Single
Angle = ((Angle Mod 360) + 360) Mod 360
Sine = pSine(Angle)
End Function
Public Function CoSine(ByVal Angle As Single) As Single
Angle = ((Angle Mod 360) + 360) Mod 360
CoSine = pCosine(Angle)
End Function
Public Sub Update()
CharRot = ((CharRot Mod 360) + 360) Mod 360
CamRot = ((CamRot Mod 360) + 360) Mod 360
If CamAng > 80 Then CamAng = 80
If CamAng < -70 Then CamAng = -70
CamPosX = CharPosX + CoSine(CharRot + 180 + CamRot) * CamDist * CoSine(CamAng)
CamPosZ = CharPosZ - Sine(CharRot + 180 + CamRot) * CamDist * CoSine(CamAng)
CamPosY = CAM_HEIGHT + Sine(CamAng) * CamDist
If CamPosY < 0.5 Then CamPosY = 0.5
End Sub
Public Sub MoveForward(Speed As Single) Dim fMove As Single
fMove = modGraphics.ScaleValue(Speed)
CharPosZ = CharPosZ - Sine(CharRot) * fMove
CharPosX = CharPosX + CoSine(CharRot) * fMove
End Sub
Public Sub MoveBackward(Speed As Single) Dim fMove As Single
fMove = modGraphics.ScaleValue(-Speed)
CharPosZ = CharPosZ - Sine(CharRot) * fMove
CharPosX = CharPosX + CoSine(CharRot) * fMove
End Sub
Public Sub StrafeLeft(Speed As Single) Dim fMove As Single
fMove = modGraphics.ScaleValue(Speed)
CharPosZ = CharPosZ - Sine(CharRot - 90) * fMove
CharPosX = CharPosX + CoSine(CharRot - 90) * fMove
End Sub
Public Sub StrafeRight(Speed As Single) Dim fMove As Single
fMove = modGraphics.ScaleValue(Speed)
CharPosZ = CharPosZ - Sine(CharRot + 90) * fMove
CharPosX = CharPosX + CoSine(CharRot + 90) * fMove
End Sub
Download Attachment: modCharSystem.bas
5.44 KB
If anything's wrong with the code, please warn me.