3
\$\begingroup\$

Found source code on Boids in C#.

http://3carrotsonastick.wordpress.com/2012/11/01/boids-in-c-xna/

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.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Did you try setting the Raven.Position property to the mouse cursor coordinates, for example? \$\endgroup\$
    – Marton
    Commented Nov 26, 2012 at 13:06

1 Answer 1

5
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks for the response! Instead of it being mouse controlled, how would i make it controlled by up down left and right arrow keys? \$\endgroup\$
    – Craig
    Commented Nov 26, 2012 at 16:36
  • 1
    \$\begingroup\$ 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;} \$\endgroup\$
    – jhocking
    Commented Nov 26, 2012 at 16:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .