## Natural movement using polar coordinates in shoebot

Mon 18 March 2013

Here's a little shoebot bot to experiment with natural movement.

This uses polar coordinates to decide the direction and velocity of an arrow on the screen.

Polar coordinates mean we can give an object a sense of 'forward', 'back', 'left' and 'right'

The code below works on the current version of shoebot

https://github.com/shoebot/shoebot.git

With planar.py to handle the directions and velocity

https://pypi.python.org/pypi/planar

There's some code to handle edges (the arrow will bounce off the edges). Also, since the nodebox grammar which we are using does anticlockwise rotations we need to handle this.

```

# space is a brake, return resets

from planar.py import Vec2, Affine

def setup(): global pos, angle, velocity speed(60) size(800, 600) pos = Vec2(WIDTH / 2, HEIGHT / 2) velocity = 0.0 angle = 0

def draw(): global pos, angle, velocity transform(mode=CENTER) if keydown: if keycode == KEY_UP: velocity += 0.2 elif keycode == KEY_DOWN: velocity -= 0.2 elif keycode == KEY_LEFT: angle -= 5 elif keycode == KEY_RIGHT: angle += 5 elif keycode == KEY_SPACE: velocity = 0.9 elif keycode == KEY_RETURN: pos = Vec2(WIDTH / 2, HEIGHT / 2) velocity = 0.0 angle = 0 else: velocity = 0.99

``````angle = angle % 360
pos += Vec2.polar(angle = angle, length = velocity)

if pos.x < 10 or pos.x > WIDTH:
angle = 180 - angle
if pos.y < 10 or pos.y > HEIGHT - 10:
angle = - angle

translate(pos.x, pos.y)
rotate(-angle) # nodebox1/shoebot rotation is anticlockwise

arrow(40, 40, 80, fill=(0, 0, 1, 0.2))
``````

```

Running the code in shoebot:

```{: .brush:bash} sbot -w arrow_drive.bot.```

This has basic movement, + intertia, it can be used for a number of things (e.g. 2d games)