Sunday, September 20, 2009

Dynamic Animation of User Controls

Ok, so you have a UserControl (or, in my case a sprite class) up on your canvas and it looks great.  Suppose something happens in your game logic and you want to animate a few parameters of your control.  In my case I wanted to have a menu expand out and “spin” into the correct place over the course of 2 seconds. 

Here’s a high level description of what you need to do: First, create a new StoryBoard that manages the animation.  Next create a DoublAnimation class that will run the value you are changing from the From value to the To value over the time Duration.  Next you need to tie everything together.  The DoubleAnimation needs to be added to the storyborard and  the DoubleAnimation needs to be associated with both the tag it’s working with AND the property of that tag.

The one trick comes when you try to connect the DoubleAnimation to the XAML tag.  If the tag isn’t named, you can’t do it.  Make sure you have a x:Name=”” for the tag holding the property you want to animate.  Once you do that, the tag should show up in intelisense for the UserControl.

Here is what the code will look like if you want to animate a 180 spin of a user control:

//create a duration for the animation
Duration twoSec = new Duration(new TimeSpan(0, 0, 0, 0, 2000));

//create the new System.Windows.Controls.UserControl
rm = new GambitApps.Sprites.RadialMenu();

//put it where it belongs on the canvas.
//The -100 is to adjust for the size of the control
rm.SetValue(Canvas.TopProperty, y - 100);
rm.SetValue(Canvas.LeftProperty, x - 100);

//create the animation objects and set their start, end, and duration
DoubleAnimation spin = new DoubleAnimation();
spin.From = -180; spin.To = 0; spin.Duration = twoSec;

// now create the storyboard that we will attach to our control
Storyboard sb = new Storyboard();

// add our animation to the storyboard
sb.Children.Add(spin);

// this is where the storyboard is connected to the XAML tag we want to animate
// note that you have to x:Name the tag you want to put here
Storyboard.SetTarget(spin, rm.Rotate);

// Next, associate the DoubleAnimation class to the property type you want it to change
Storyboard.SetTargetProperty(spin, new PropertyPath(RotateTransform.AngleProperty));

// tell the storyboard to start the animation
sb.Begin();

Thursday, September 3, 2009

CSS Style to format source code

One of the things I want to show in this blog is source code.  The best source code viewer I’ve seen to date is the one they use at Stack Overflow.  The cool thing they do is highlighting based on syntax.   My CSS isn’t quite as nice, but it works.   All right, let’s not kid ourselves.  Mine is just Courier New in a box.  Here’s what I am using:


<style type='text/css'>

code {
color: #000077;
background: white;
border-bottom: blue 1px solid;
text-align: left;
border-left: blue 1px solid;
margin: 1em 0px;
padding-left: 30px;
padding-right: 20px;
padding-top: 20px;
padding-bottom: 20px;
display: block;
font: 100%/17px "Courier New", courier, fixed, monospace;
font-weight: bold;
border-top: blue 1px solid;
border-right:blue 1px solid;
}

</style>



I use that style then surround my code snippet <pre><code>like so</code></pre>.



I experimented with green or yellow on black, but that always made my eyes get all funny.  The other thing I wanted was a horizontal scroll bar when the frame became too narrow.  From my reading of the specs, overflow: auto should do it, and it works perfectly in Firefox, but in IE 8 I loose the indenting so I’m not going to use that for now.

Wednesday, September 2, 2009

Mindless Collision Detection

I'm trying to get all the pieces in the right places and work out the design details for now.  That means that I'll make routines that I know work, but that may not be efficient enough to work in production. 
Of course, it's hard to predict where the bottlenecks will be.  For example, I think that collision detection will have to be tweaked to keep it from bogging down the system.  Statistically speaking, I'm probably going to be wrong.  My experience is that it is hard to think about what is really going to be slow.  Brute force collision detection is basically a lot of math.  Computers are good at math...maybe good enough that I don't have to care about it for now.  Only time will tell.
Anyway, here's my function.  It's about as stupid as can be.  I actually can think of a worse algorithm, but only one.


public List<Pair<Sprite>> FindCollisions() 
{ 
  List<Pair<Sprite>> collisions = new   List<Pair<Sprite>>(); 
  int numSprites = lSprites.Count; 
  for (int i=0; i< numSprites; i++) { 
    for (int j=i+1; j<numSprites; j++) { 
      if (Collides(lSprites[i], lSprites[j])) { 
        collisions.Add(new Pair<Sprite>(lSprites[i], lSprites[j])); 
      } 
    } 
  } 
return collisions; 
} 
I do have a basic test below.  I should add a more complex case, but Connor wants me to tuck him back into bed.

[TestMethod]
public void SimpleCollisionTest()
{
  List<GambitApps.Util.Pair<Sprite>> collisions;

  Sprite s1 = new SimpleShooter.Sprite(5, 5, new System.Windows.Point(10, 10));
  s1.Velocity = new Vector(-3, -3);
  Sprite s2 = new SimpleShooter.Sprite(7, 9, new System.Windows.Point(10, 10));
  s2.Velocity = new Vector(10, 10);

  world.addSprite(s1);
  world.addSprite(s2);

  collisions = world.FindCollisions();
  Assert.IsTrue(collisions.Count == 1);

  world.Update(new TimeSpan(0, 0, 5));

  collisions = world.FindCollisions();
  Assert.IsTrue(collisions.Count == 0);

}

Tuesday, September 1, 2009

Galaxy Command

I haven't mentioned it before, but I do actually have a plan for what I'm building. The "code name" I'm working with now is Galaxy Command. The basic premise is that you are trying to defend something, maybe a space station or battleship. All your defenses are computer controlled. The player won't aim or fire, just give orders to the defense bots.

There will be the normal purchasable upgrades, but I want to put in a parallel tech tree as well. You can buy whatever you want no matter where you are in the tech tree, but the advanced tech will get you choices of more and better AI routines to run your bots. So if you are at tech level 0, you can buy a squadron of fighters, but all they will be able to do is ram your enemies. Useful, but not great. Upgrade your AI a bit, and they will learn how to avoid crashing into their targets. Upgrade more and they will start to avoid incoming fire. Upgrade more and you get the ability to give certain commands, like defend this area or attack the biggest enemy ships.

One motive for doing this is that I haven't ever coded game AI before so I want to be able to have a few low targets. Another is that the programmer in me thinks it will be cool.