Direction from angle

BlitzMax Forums/BlitzMax Programming/Direction from angle

Is there a way to work out the increments to x and y from the angle you want to head? So if i wanted my little ant to go 90 degrees he would move 1 along x and 0 along y. I thought Sin would have something to do with it (why i dunno i just remebered using angles with it :P).

Could anyone help?

Here you go:
Local angle, speed
Local xrate, yrate
Local x, y

angle = 150
speed = 5

xrate = speed * Sin( angle )
yrate = speed * Cos( angle )

x = x + xrate
y = y + yrate

Angle is of course the angle of the direction of your ant. Speed is how fast your ant is going. Xrate and Yrate are the rate that x and y are changed by. They are calculated as above and added to their respective variables.

This is how a circle is calculated. If you did a For angle = 0 to 360 sort of thing, it would calculate a circle that has a radius of 5, because a circle is calculated X = Radius * Sin( Degree ), and Y = Radius * Cos( Degree ). So in reality you are calculating a circle for one specific degree.

Hope it helps.

<EDIT> I think there is a sample of Asteroids that comes with BMX if not you could try B+. Look at those examples and you can see how they do it.

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.

Thanks very much! This is really going to help. I guess i'll have to start reading more about these different mathematical things!

I would've told you about using the bresenhams line technique which uses integers to follow a line, but with the speed of modern computers it's not only a lot easier but possibly also less code and just as fast to do it with floating point numbers.

Hmm, I've always used cos for x and sin for y, because it's correct according to my math teacher and the sin/cos circle thingy (the Unit Circle? I forget its name.) That way, 0 faces to the right like it should, and increasing angles go clockwise.

Hmm, I've always used cos for x and sin for y, because it's correct according to my math teacher and the sin/cos circle thingy (the Unit Circle? I forget its name.)
Me too. The problem is that on a computer the second axis (y) is inverted compared to traditional trigonometry. Thus the y coordinate for computers would be -cos(angle).

That's why most people just reverse x and y orientation, otherwise you get a lot of "interesting" results when trying to use it with ATan2 and RotateImage (which uses angles like in navigation, not trigonometry).

That way, 0 faces to the right like it should, and increasing angles go clockwise.
Except increasing angles are supposed to go counter-clockwise!