If current direction angle < target direction angle
Increase current direction angle
If current direction angle > target direction angle
Decrease current direction angle
Which seemed reasonable enough, but I hadn't considered the way that angles are calculated.
In javascript all angles use radians. You may remember that there are 2π radians in a circle. Within HTML5's <canvas>, radians have the following meanings:
0 radians means you are pointing East
½π radians means you are pointing South
−½π radians means you are pointing North
π or −π radians means you are pointing West
(Radians weren't the problem here, I would have had the same problem if javascript had used degrees)
The problem that I was seeing was that sometimes a boid would be travelling South West and its target would be North West of it, but rather than turning clockwise it would turn anti-clockwise.
Here is a boid being told to rotate to and from a direction in the South West and a direction in the North West, because an example is often more helpful than an explanation:
As you can see, this boid is taking the long way around.
What's going on?
The boid stores its direction as a number which reflects the angle in radians that it should be facing. If it starts at +3.0 radians and we instruct it to move towards −3.0 radians then it will follow its simple instructions:
If +3.0 > −3.0
Decrease current direction angle
+3.0 is greater than −3.0, so the boid decreases its current direction angle, moving from +3.0 to +2.9 to +2.8 to +2.7, which turns it anti-clockwise, going through the zero point and sending it the long way around.
What we'd expect to see is the boid taking the short route between the two points, through π (remember +π and −π mean the same thing in this situation).
Why is this a problem?
As far as bugs go this isn't the end of the world, but it results in some undesirable behaviour.
If there are two boids, A and B, with directions of +3 and −3 respectively, when they come close to each other we would expect them to match directions, averaging to +π/−π (remember that +π and −π mean the same thing). What will actually happen is that the two boids will change direction away from each other, trying to average at 0, which is almost the opposite to the direction they're currently travelling in.
In a more general sense it makes it far less likely that a flock of boids will end up moving in a Westward direction.
How do we fix this problem?
We need to treat the direction more intelligently. Rather than just treating it as a number we have to recognise that it's an angle and that +3.0 is actually closer to −3.0 than it is to 0.
In order to find the difference between one angle and another we can say:
difference angle = end angle − start angle
But this only works if the two angles are either both North of 0 or both South of 0. If the two numbers are on opposite sides, say +2.0 and −2.0, then the result would be beyond either π or −π, in this case −4.0.
We can easily fix this though: we know that a circle has 2π radians, and that in HTML 5 canvas the semi-circle from East to South to West goes from 0 to +π, and the semi-circle from East to North to West goes from 0 to −π, so once we go beyond either π or −π we can just subtract or add 2π to that number to get a more reasonable number.
Now the result of passing in +2.0 and −2.0 will return around 2.28
Find difference between current direction and target direction
If the difference < 0
Increase the current direction
If the difference > 0
Decrease the current direction
Replacing this part of the code leads to the following behaviour:
I've always been fascinated by emergent behaviour, there's something cool about simple rules resulting in a complex outcome. I think that Boids is a really good way of demonstrating emergent behaviour.
Boids is an artificial life program, developed by Craig Reynolds in 1986, which simulates the flocking behaviour of birds.
I decided to put together a 2D version of Boids using HTML5 canvas. Click the "Start" link below to see them start moving.
The boids above follow three very simple rules (that you can switch on and off):
(This isn't a rule, it just helps visualise who's a neighbour!)
What's going on here
I start by creating 30 boids, each with a random starting position and direction.
On each frame, each boid investigates its neighbours and figures out the average directions of those that are reasonably close (within 100px) and those that are too close (within 30px).
If the first rule is enabled then the boid will rotate itself slightly towards the average direction of the reasonably close boids.
If the second rule is enabled then it will rotate itself slightly away from the average direction of the very close boids.
If the third rule is enabled then it will also survey all the boids on the canvas, figure out where the highest population of boids is and rotate slightly in that direction. This is useful for the boids that occasionally disappear off on their own and would otherwise fly off in a straight line forever (which isn't a huge problem in this example, since boids reaching the edge will wrap around to the opposite edge).
It's quite fun disabling the 2nd rule, watching the boids squish up close to each other, and then enabling it again to see them all fly away from each other.
Source code
The source code is available on my github page. It's written in coffeescript and is hopefully fairly self explanatory. I tried making some docco docs but that didn't go very well. Hopefully I'll get them up soon.
Let me know if you have any questions or suggestions @baxt3r
It's been about a year since I hit 1,024 tweets. In that time a few things have changed; after four years working for the Forestry Commission I left to join the guys at ASMALLWORLD building and maintaining a huge Rails app.
I wondered what my mood had been within this period. One extremely unscientific indicator of mood is emoticons.
Emoticons
As you can see, I have been mostly :), occasionally :D, sometimes :( and hardly ever D: which is exactly how things should be, so that's good.
Retweets
Over the past year I've averaged 2.61 tweets a day.
229 of those tweets (about 22%) have been retweets. That's higher than I thought it would be, so I had a look at who I'd been retweeting:
Of the 229 retweets that I've made, 128 of those, or 55%, were retweets from users that I've only retweeted once. I don't even follow most of them. I wasn't aware how often I retweeted retweets, but the data suggests I do it quite a lot!
Of the people that I've retweeted three times or more, here's who I've retweeted the most:
I've colour coded the charts to group people together.
In green are the people that I know through my time spent helping with the Guild Wars Wiki.
In brown are people that don't fall into any of the other groups!
At the top of the retweets is @saveourforests. This is actually an account I registered myself during my time assisting in the Save Our Forests campaign while at the Forestry Commission, so there is a little bit of self-promotion going on here, but since its creation the account has been updated almost exclusively by Tony, a former colleague, so I don't feel too guilty! And it was (and is) for a good cause!
Following on from that there's a good mix of people from ScotRUG and work who tend to say amusing or interesting things, and of course my girlfriend Lindsey, aka @Daily_Madness, who I am obliged to retweet from time to time to stay in her good books ;)
My @notch retweets are due to my recent Minecraft addiction. The less said about that the better.
Mentions
Mentions are more varied than retweets, probably because I'm more likely to have a conversation with most people than retweet them.
My colleagues @mungler and @mr_urf top the mentions chart, followed almost exclusively by members of ScotRUG and the Guild Wars Wiki, the only notable exceptions being my friend @mikos who is not a ruby developer (shock horror) and @Daily_Madness.
Hashtags
I feel like I have to mention hashtags since they're an important part of Twitter, but it would be pointless creating a chart for them since I only tend to use them as jokes.
The only two that I seem to have used seriously are #yaypril 3 times and #scotruby 7 times.
Language
My most used word is "I". I'm so egocentric. This hasn't changed from last year.
But short words aren't very interesting, so instead I examined my most used longer-than-four-letter words:
My most used words actually seem really positive! In fact you can almost read that last chart as a sentence.
I tend to say thanks a lot in real life, but I didn't realise this had carried over into Twitter.
I also probably say "awesome" a little too much. I am trying to replace this with "splendid".
Summary
I'm not sure if analysing my tweets is a normal thing to do. I'm pretty certain it isn't. But it's quite fun to look closely at something that you've been doing almost absent-mindedly – like reading too much into people's doodles or trying to predict the future by examining the remnants of a cup of tea.
Thank you to my Twitter followers/followees for a year full of :), here's hoping for another 2,048 mostly happy tweets!
Ifttt is a website that lets you set up tasks of the form "If this then that" where "this" is a trigger and "that" is an action. Some popular tasks include:
If I am tagged in a photo on Facebook then save it to Dropbox.
If tomorrow's weather forecast is rainy then send me a text message telling me about it.
If I star a tweet on Twitter then save it to Instapaper.
If an RSS feed is updated then post a tweet about it.
Ifttt has managed to simplify the interface between a number of popular web apps so that you can set up a whole host of tasks with the minimum of configuration. And even better, once you've created a cool task you can turn that into a "recipe" and share it with other people.
By Eric Chahi and Ubisoft Montpelier, From Dust is a god game, playing like a modern Populous. In fact it feels lot like a Peter Molyneux game except with more emphasis on helplessness than on morality.
Despite having the ability to manipulate the elements at whim, you soon discover that it's very easy to upset the balance of nature – blocking the flow of a river for some short term gain can quickly lead to bigger problems when the river breaks its banks.
This is a game about order and chaos, and about the difficulty of regaining control over a badly planned situation. I don't want to give any examples in case I spoil the fun, but trust me when I say it's clever and infuriating in equal measure.
Bastion is a top down action RPG in which everything that you do is narrated.
Having someone describe your every move is weird, but it adds an interesting dimension to the game. The narrator tells the story as someone who has already experienced it to the end, occasionally hinting at what the future holds, leading to a sense of foreboding.
I love Tetsuya Mizuguchi, the creator of Rez, and while Child of Eden isn't dramatically different from Rez in terms of gameplay, being another on rails shooter, it is certainly better looking than its predecessor. Where Rez was minimalist and polygonal Child of Eden is decadent and organic.
Another game based on Mizugughi's obsession with synesthesia, this time the conceit is that you are saving a developing artificial intelligence called Lumi from a destructive virus:
Child of Eden thrusts you in the center of a battle to save Project Lumi, a mission to reproduce a human personality inside Eden, the archive of all human memories.
Much like Rez, the plot doesn't really matter. Child of Eden manages to put me into "the zone" like few other games are capable of.
I know I'm late to the party but I've been playing Minecraft a lot recently. So far I have a little house, a pet wolf and a farm. I also found some iron the other day. I have a new appreciation for Creepers, possibly the most terrifying creatures ever imagined.
Here's a picture of my wolf and my farm, and an axe that I'm holding.
Jonathan Blow, creator of Braid, gave a talk to UC Berkeley computer science students on his style of programming.
Most of the talk is about the aesthetics of code, and about how it's possible for an individual to write the ~90,000 lines of code needed to write a game like Braid.
There is some great advice on ways to code to get things done, but probably the most important thing that Blow says isn't really about programming at all (transcribed below):
Back when I was an undergraduate the Internet existed but it really wasn't the thing that we have now. There wasn't that much of substance online then. Now of course the Internet is huge and it's in everybody's life and it's full of all these people saying things and trying to share information with each other, and I think what happens very often is that you read something somebody said on the Internet and you're like, yeah I get what that guy is saying but he's wrong, or yeah I get what that guy's saying, he's right, but I already knew that and he's stupid, I know better than that now.
Please entertain the idea as you go out into the world that usually neither of those things are true, usually you don't actually understand what the other person is saying. They may be using words where you think you understand each individual word, but the way that they interpret their phrase is different to the way you interpret their phrase and this leads very quickly to problems and it can prevent you from coming to a better understanding of computer science.
Worth a listen, especially for the context surrounding this section that really emphasises the point.