Direction from angle

BlitzMax Forums/BlitzMax Programming/Direction from angle

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Sin(angle) returns the "x portion" of a movement in direction <angle> of length 1. Cos(angle) returns the "y portion" of a movement in direction <angle> of length 1. If you are moving by a different distance, multiply the result by the distance.

Eg, to move 45 degrees for 5 units:
x=5*sin(45)

Small example to demonstrate:

Type Vector
Field x
Field y
End Type

Graphics 800,600,0

Local q:Vector=New Vector
q.x=100
q.y=100

Local p:Vector=q

Local direction:Float=0

While Not KeyHit(KEY_ESCAPE)

DrawLine q.x,q.y,p.x,p.y,True

q=p
p=move(p,direction,5)


If KeyDown(KEY_SPACE) direction :+ 5

Delay 20

Flip

End While

Function move:Vector(initialPos:Vector,direction:Float,distance:Float)

Local result:Vector=New Vector

result.x=initialPos.x+(distance*Sin(180-direction))
result.y=initialPos.y+(distance*Cos(180-direction))

Return result

End Function



The above code does what you need. Hold down the SPACE key whilst the line is being plotted to change the direction.