-
Line rotation
How would I make this simple line rotate? So far I can just move it foward and backward wherever it points.
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Static posx As Integer
Static posz As Integer
Static lookx As Integer
Static lookz As Integer
Static vecx As Integer
Static vecz As Integer
Static length As Integer
length = Line1.x2 - Line1.X1
Label1.Caption = length
posx = Line1.X1
posz = Line1.Y1
lookx = Line1.x2
lookz = Line1.Y2
vecx = lookx - posx
vecz = lookz - posz
If KeyCode = vbKeyUp Then
posz = posz + vecz
lookz = lookz + vecz
posx = posx + vecx
lookx = lookx + vecx
Line1.X1 = posx
Line1.Y1 = posz
Line1.x2 = lookx
Line1.Y2 = lookz
Shape1.Left = posx - 135
Shape1.Top = posz - 135
End If
If KeyCode = vbKeyDown Then
posz = posz - vecz
lookz = lookz - vecz
posx = posx - vecx
lookx = lookx - vecx
Line1.X1 = posx
Line1.Y1 = posz
Line1.x2 = lookx
Line1.Y2 = lookz
Shape1.Left = posx - 135
Shape1.Top = posz - 135
End If
-
-
Huh I think you need some trig, it's not easy so unless you really really need it, you should forget about it :p
-
Oops sorry Sas, looks like I was too slow hehe :p
-
Drakon
Ok, I got it to rotate. However, when you change the direction it rotates, the line changes it's orientation then starts to rotate... I'm bad at explaining things in words sometimes, so feel free to try the code... it just needs a line, shape, and a label.
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Static posx As Integer
Static posz As Integer
Static lookx As Integer
Static lookz As Integer
Static vecx As Integer
Static vecz As Integer
Static length As Integer
Static angle As Double
Static speed As Integer
Label1.Caption = length
posx = Line1.X1
posz = Line1.Y1
lookx = Line1.x2
lookz = Line1.Y2
vecx = lookx - posx
vecz = lookz - posz
length = Sqr(((lookx - posx) ^ 2) + ((lookz - posz) ^ 2))
If KeyCode = vbKeyUp Then
posz = posz + vecz
lookz = lookz + vecz
posx = posx + vecx
lookx = lookx + vecx
Line1.X1 = posx
Line1.Y1 = posz
Line1.x2 = lookx
Line1.Y2 = lookz
Shape1.Left = posx - 135
Shape1.Top = posz - 135
End If
If KeyCode = vbKeyDown Then
posz = posz - vecz
lookz = lookz - vecz
posx = posx - vecx
lookx = lookx - vecx
Line1.X1 = posx
Line1.Y1 = posz
Line1.x2 = lookx
Line1.Y2 = lookz
Shape1.Left = posx - 135
Shape1.Top = posz - 135
End If
angle = angle + ((2 * 3.14) / 320)
If KeyCode = vbKeyRight Then
lookx = posx + (length * Cos(angle))
lookz = posz + (length * Sin(angle))
Line1.x2 = lookx
Line1.Y2 = lookz
End If
If KeyCode = vbKeyLeft Then
lookz = posz + (length * Cos(angle))
lookx = posx + (length * Sin(angle))
Line1.x2 = lookx
Line1.Y2 = lookz
End If
End Sub
Any help would be appreciated :)
-
Oh well I figured it out on my own:)
-
I'm glad we were of some help :D
-
Do you write anything useful? :)
-