Click to See Complete Forum and Search --> : Angles and vectors
git
Nov 5th, 2000, 05:10 AM
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
parksie
Nov 5th, 2000, 05:53 AM
If a is 45, then:
u = ( cos 45 )
( sin 45 )
That leaves a normalised vector, so you may need to scale it up.
In VB:
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):
Function Deg2Rad(dDegrees As Double)
Deg2Rad = dDegrees * 0.0174532925199432957692369076848861
End Function
git
Nov 5th, 2000, 06:15 AM
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
parksie
Nov 5th, 2000, 06:23 AM
When I tried to implement your code, it was only in intervals of 45 degrees.
Huh? It works fine for me, just use:
a = Deg2Rad(30)
or
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
git
Nov 5th, 2000, 06:31 AM
Ok, here's what I did.
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
parksie
Nov 5th, 2000, 06:39 AM
This seemed to work for me:
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.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.