Results 1 to 6 of 6

Thread: Angles and vectors

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Posts
    225
    Hiyas,

    I have an angle between 0 and 359 (360 is 0). How do I get vectors from it?

    Hope this is an easy enough question... =)

    Thanks.

    -Git

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    If a is 45, then:
    Code:
    u = ( cos 45 )
        ( sin 45 )
    That leaves a normalised vector, so you may need to scale it up.

    In VB:
    Code:
    Dim a As Single
    Dim x As Single
    Dim y As Single
    
    a = 1 ' Radians!!!!
    x = Cos(a)
    y = Sin(a)
    Since VB uses Radians (like most computer languages), you'll need to use rad = deg * (pi / 180):
    Code:
    Function Deg2Rad(dDegrees As Double)
        Deg2Rad = dDegrees * 0.0174532925199432957692369076848861
    End Function
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Posts
    225
    Hmm, I tried that, but it didn't really work.

    What I'm trying to do is to make a movement engine similar to that in Grand Theft Auto - you use the left/right keys to rotate the person, and the up key to walk in that angle...

    When I tried to implement your code, it was only in intervals of 45 degrees.

    I don't really know trig too well, so I'd appreciate any help I can get. =)

    -Git

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    When I tried to implement your code, it was only in intervals of 45 degrees.
    Huh? It works fine for me, just use:
    Code:
    a = Deg2Rad(30)
    or
    Code:
    a = Deg2Rad(23.45235)
    ...?

    So, to walk one unit at an angle of 30 degrees, you'd do:

    1. Convert angle to radians, and find (x,y) components
    2. Move the person by each of those. If necessary, scale them up by the same amount
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Posts
    225
    Ok, here's what I did.

    Code:
    If KeyCode = vbKeyUp Then
    XVector = Cos(Rotation * 1.74532925199433E-02)
    YVector = Sin(Rotation * 1.74532925199433E-02)
    XOffSet = XOffSet + XVector
    YOffSet = YOffSet + YVector
    End If
    Is that how to do it correctly? As I said, it appears to be moving in 45 degree intervals, still doing it... =(

    -Git

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    This seemed to work for me:
    Code:
    Dim rot As Integer
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
        If KeyCode = vbKeyRight Then
            rot = rot + 10
            If rot > 360 Then rot = rot - 360
        End If
        If KeyCode = vbKeyLeft Then
            rot = rot - 10
            If rot < 0 Then rot = rot + 360
        End If
        If KeyCode = vbKeyUp Then
            Shape1.Left = Shape1.Left + (Cos(rot * 1.74532925199433E-02) * 100)
            Shape1.Top = Shape1.Top + (Sin(rot * 1.74532925199433E-02) * 100)
        End If
        Debug.Print rot
    End Sub
    Add a Shape to your form, and put it in the middle somewhere.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width