Another space shooter update. Added some background graphics to make it look more interesting. Also started testing out adding an enemy ship and collision detection.
I started with this tutorial and basic collision detection with the Rectangle struct is almost too easy. Buuuuut, then I discovered the limitations, mainly that creating a bounding box using Rectangle struct can't rotate. So I'm going to have to go back to the drawing board and come up with something else.
You can try using circular collision, it's easy like rectangular but works fine with rotation. You just need to give each object a radius, which you can determine from it's sprite dimensions or what looks realistic on the screen. Here's the code
private static bool IsColliding(Entity a, Entity b)
{
//Add the length of the radiuses
float radius = a.Radius + b.Radius;
//Check if either entity is already expired, then check the distance from their positions VS how large they are
return !a.IsExpired && !b.IsExpired && Vector2.DistanceSquared(a.Position, b.Position) < radius * radius;
}
To get this to work you need to draw your sprites on the screen with their origin being the center of the image and not the upper left corner. You can do this with in the SpriteBatch.Draw method like this
//get the size of the sprite
size = new Vector2(image.Width, image.Height);
//divide the sprite in half in both directions and use that as the origin
spriteBatch.Draw(image, Position, null, EntityColor, Orientation,
Size / 2f, 1f, 0, 0);
Hope this helps.