Sunday 30 October 2011

Ship Behaviour

I finally worked out all the bugs in the ship behaviour, so it looks shippy now :D

The basic idea is to make it tack towards the beacon (the lamp) rather than approach it directly, and once there, circle the beacon continuously. The algorithm is as follows:

  • Move ahead
  • Influence your turning speed right or left, whichever way points you at the beacon
  • Turn by the amount of your turning speed
Because there is a maximum turning speed, and the amount that it can be influenced each frame is small, this gives the ship very 'heavy' steering. It causes swinging behaviour because the ship effectively has turning inertia, once it is turning in one direction it takes quite a few frames for it to be able to turn back the other way, by which point it is well off the target again.

It will also cause the ship to orbit the beacon, as long as it has a reasonable approach (i.e. not flying straight into the beacon). If the beacon is always to the left, and the max turning speed never allows the ship to turn left far enough to point at the beacon, it will keep trying to and therefore orbit. This motion is intended to say, 'I've arrived', but in a shippy sort of language, where you can't stop moving and you can't just run straight into the target.


This algorithm doesn't require any state changes to do these two behaviours (although could probably be more tightly controlled by having states), it just needs to be tuned so both circling and tacking look reasonably close to what I want.

I had to deal with handedness (the exercises we did set me up well for that though) and a problem of comparing two angles to see which one is 'bigger', when the angles wrap around from 359 to 0 and throws everything off. After many ideas for solutions, and some input from JP, I figured out that my case was a little simpler since I didn't need to find the angle, just know which direction to steer the ship in. So I just tested if the difference between the two angles I had found was > 180, and if it was, it meant I should reverse the sign, because going the other way around the circle was going to be faster, thus the best way to turn to start making these two angles line up. I'm sure that was really hard to follow, but it took a while to figure out so I wanted to write about it! Diagrams would help, but the sketches in my book are probably even more like gibberish than this paragraph.

No comments:

Post a Comment