Post

 Resources 

Console

Home | Profile | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 VBGamer
 VBGamer
 Code Snippet - 3rd person movement

Note: You must be registered in order to post a reply.

Screensize:
UserName:
Password:
Format Mode:
Format: BoldItalicizedUnderlineStrikethrough Align LeftCenteredAlign Right Horizontal Rule Insert HyperlinkInsert EmailInsert Image Insert CodeInsert QuoteInsert List Spell Checker
   
Message:

* HTML is OFF
* Forum Code is ON
Smilies
Smile [:)] Big Smile [:D] Cool [8D] Blush [:I]
Tongue [:P] Evil [):] Wink [;)] Clown [:o)]
Black Eye [B)] Eight Ball [8] Frown [:(] Shy [8)]
Shocked [:0] Angry [:(!] Dead [xx(] Sleepy [|)]
Kisses [:X] Approve [^] Disapprove [V] Question [?]

   Insert an File
Check here to include your profile signature.
Check here to subscribe to this topic.
    

T O P I C    R E V I E W
VBBR Posted - Oct 15 2004 : 1:58:56 PM
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.
'/-----------------------------------------\
'| File: modCharSystem.bas                 |
'| Author: VBBR                            |
'| Last Changed: 10/15/2004                |
'|                                         |
'|  Demonstrates the math used in 3rd      |
'| person games to move a character and a  |
'| camera in a 3D world using the keyboard |
'| in conjunction with the mouse.          |
'\-----------------------------------------/
  
Option Explicit
  
Public CharPosX As Single, CharPosZ As Single 'Horizontal plane  
Public CharRot As Single 'Y axis  
  
Public CamPosX As Single, CamPosY As Single, CamPosZ As Single
Public CamRot  As Single 'Y axis, relative to CharRot  
Public CamAng  As Single 'Z axis rotation  
  
'IMPORTANT: Be sure to initialize this somewhere or you may get REALLY weird results
Public CamDist As Single 'Zoom  
  
'You may change this to a variable so you can change it at run-time
Public Const CAM_HEIGHT As Single = 1.5 'Camera height  
  
'Angles
Private pSine(359)   As Single
Private pCosine(359) As Single
Private Const PI As Single = 3.141593  
Private Const RAD As Single = 0.01745329  
  
'Call this before anything else
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
  
'Returns the sine of an angle, in degrees
Public Function Sine(ByVal Angle As Single) As Single
    Angle = ((Angle Mod 360) + 360) Mod 360  
    Sine = pSine(Angle)  
End Function
  
'Returns the cosine of an angle, in degrees
Public Function CoSine(ByVal Angle As Single) As Single
    Angle = ((Angle Mod 360) + 360) Mod 360  
    CoSine = pCosine(Angle)  
End Function
  
'Call this every frame
Public Sub Update()  
    'avoid overflow errors by limiting the angles' values  
    CharRot = ((CharRot Mod 360) + 360) Mod 360  
    CamRot = ((CamRot Mod 360) + 360) Mod 360  
  
    'limit camera rotation on the Z axis  
    If CamAng > 80 Then CamAng = 80  
    If CamAng < -70 Then CamAng = -70  
  
    'Set Camera position  
    CamPosX = CharPosX + CoSine(CharRot + 180 + CamRot) * CamDist * CoSine(CamAng)  
    CamPosZ = CharPosZ - Sine(CharRot + 180 + CamRot) * CamDist * CoSine(CamAng)  
    CamPosY = CAM_HEIGHT + Sine(CamAng) * CamDist  
  
    'Avoid the camera from going below the ground  
    'Change this according to your floor level  
    If CamPosY < 0.5 Then CamPosY = 0.5  
  
    ' The last thing to do is actually set the character and camera positions.  
    ' This depends on which engine/wrapper/API you're using so I won't go into  
    'any specifics, just point out what goes where for your convenience.  
  
    ' Set the character X and Y positions using CharPosX and CharPosZ.  
    ' Set the Y position according to your world data.  
    ' Set the character Y rotation to the value stored in CharRot.  
  
    ' Set the camera position using the CamPos variables.  
    ' Set the camera lookat point to (CharPosX, CAM_HEIGHT - 0.3, CharPosZ)  
    'or something like it. You can use just CAM_HEIGHT for the Y position but  
    'it looks a lot better when subtracting a small number.  
End Sub
  
'*******************************************************
'*  NOTE on the following call:                        *
'*   modGraphics.ScaleValue                            *
'*                                                     *
'*  This is an OUTSIDE method I used to convert the    *
'* speed from units/sec to units/frame according to    *
'* the frame rate. Change this according to the method *
'* you use to control timed movements/animations;      *
'* or if you're using frame-based movement just        *
'* use units/frame in the Speed argument and delete    *
'* all the "Dim fMove" and "modGraphics.ScaleValue"    *
'* lines and use "Speed" in place of "fMode" in the    *
'* calculations below.                                 *
'*******************************************************
  
Public Sub MoveForward(Speed As Single) 'Speed = units/sec  
    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) 'Speed = units/sec  
    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) 'Speed = units/sec  
    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) 'Speed = units/sec  
    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.
3   L A T E S T    R E P L I E S    (Newest First)
Almar Posted - Oct 17 2004 : 04:23:04 AM
quote:
Originally posted by Eric Coleman

Instead of
  
    Angle = CInt(Angle)  
    Do Until Angle > -1  
        Angle = Angle + 360  
    Loop
    Do Until Angle < 360  
        Angle = Angle - 360  
    Loop
  


you could use this

  
    Angle = ((Angle mod 360) + 360) mod 360  
  






Hehe. thanks. I once had this thing, but lost it. and couldn't think it the right way again ;)
VBBR Posted - Oct 15 2004 : 8:03:18 PM
Thanks, I'll change that.
Eric Coleman Posted - Oct 15 2004 : 7:41:26 PM
Instead of
  
    Angle = CInt(Angle)  
    Do Until Angle > -1  
        Angle = Angle + 360  
    Loop
    Do Until Angle < 360  
        Angle = Angle - 360  
    Loop
  


you could use this

  
    Angle = ((Angle mod 360) + 360) mod 360  
  



VBGamer © Go To Top Of Page
This page was generated in 0.23 seconds. Snitz Forums 2000

Copyright © 2002 - 2004 Eric Coleman, Peter Kuchnio , et. al.