LoginLogin

Sprites following an angle

Root / Programming Questions / [.]

UltraPhoenix4Created:
Is is possible to make a sprite face a certain angle and then go forward along that angle? If so, how?

Use this code:
SPEED#=2
ANGLE#=90
INC X#,SIN(RAD(ANGLE#))*SPEED#
INC Y#,COS(RAD(ANGLE#))*SPEED#
SPOFS 0, X#, Y#
SPROT 0, ANGLE#
The sprite should move to the right I think? SPEED# is the movement speed.

Are angles in SmileBASIC calculated in degrees or radians? Or is there a way to select which it uses?

Are angles in SmileBASIC calculated in degrees or radians? Or is there a way to select which it uses?
The SIN and COS functions require radians (as do other trig functions). SPROT, on the other hand takes degrees. Perska's code is a nice example of converting from polar coordinates into Cartesian x-y. If you choose an angle for the sprite to go, however, you have to be careful about the y-direction. An angle of zero degrees will have COS (0) which should yield +1 sending the sprite DOWNWARD in screen coordinates. Similarly, selecting 45 degrees for the angle will send the sprite directly "southeast". In an application where I have similar code I used -COS(RAD(ANGLE)) to invert typical polar coordinates for the screen. In Perska's code sample one could use DEC instead of INC to achieve the same Y inversion (depending on your application).

In fact, SPROT only takes integers in degrees. Real numbers are truncated. It would be nice if they just settled on one format to use but hey, nothing's perfect.