Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Found source code on Boids in C#.

How do I go about making the raven class in the above player controlled? If I could control it myself, it would be a great help, but I don't know how to go about it.

share|improve this question
Did you try setting the Raven.Position property to the mouse cursor coordinates, for example? – Marton Nov 26 '12 at 13:06
up vote 5 down vote accepted

Change the raven's hunt method to hunt the mouse pointer instead of the nearest sparrow. Something like:

private void Hunt()
    offSet += mouse.Position - position;

This will have the raven hunting the mouse pointer, and therefore following the mouse instead of a sparrow. Wa-la, player controller raven.

Setting the ravens position directly to that of the mouse pointer will work, but you'll get a raven that can move across the screen very, very fast.

Keyboard control is a bit more complicated if you want more natural looking flight. A simpler way to do keyboard control would be to use the keyboard to position a target for the raven to fly towards, similar to the mouse control. But that's not very intuitive for the player. So instead I would do as JHocking suggests, use the arrow keys to increment the offset value:

if (left arrow) {
    offset.x = -1;
} else if (right arrow) {
    offset.x = 1;
} else {
    offset.x = 0;
if (up arrow) {
    offset.y = 1;
} else if (down arrow) {
    offset.y = -1;
} else {
    offset.y = 0;

That will probably give you some ugly flight, but it will give you a start. You should probably implement some better steering for your raven.

share|improve this answer
Thanks for the response! Instead of it being mouse controlled, how would i make it controlled by up down left and right arrow keys? – Craig Nov 26 '12 at 16:36
Increment 'offset' based on the arrow keys instead of the mouse position, something like if (left arrow) {offset += 1;} else if (right arrow) {offset -= 1;} – jhocking Nov 26 '12 at 16:40
perfect, thanks! – Craig Nov 26 '12 at 21:54

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.