C# XNA – Per Pixel Collision Detection on Rotated Objects

I’ve been working on a new project this week which requires per pixel collision detection, however all the game objects that I need to detect the collisions on need to be rotated dynamically which introduced a lot of trouble when trying to detect collisions.

To save anyone else from having to jump through hoops trying to figure this out I’ve put together a class called CollidableObject which will store a texture, position in world space and rotation factor and allow you to detect collisions between them by calling the IsColliding method.

In order for this class to work properly, you will also have to draw your textures using the rotation values (even if you aren’t rotating your sprite) like so:

// If the player is coliding with the enemy turn the screen red
if (this.player.IsColliding(this.enemy))
{
	GraphicsDevice.Clear(Color.Red);
}

// Draw the player to the screen
spriteBatch.Draw(this.player.Texture,
			this.player.Position,
			null, Color.White,
			this.player.Rotation,
			this.player.Origin,
			1.0f,
			SpriteEffects.None,
			0.0f);

It doesn’t support the use of sprite sheets or animation, however you should be able to modify it easily to suit your needs.

Click Here to View the Code on Pastebin

Posted in Programming, Tutorials at December 19th, 2011. No Comments.

Displaying a Player’s Latest World of Warcraft Achievements in VB.Net and C#

Setup

To fetch the achievement data of the World of Warcraft character we are going to make use of armoryapi, the armoryapi library currently requires that the project’s target framework be 3.5 or higher so set your project up to use 3.5 or higher in the project properties.

After creating your project and setting up the target framework the next step is to add the reference to the armoryapi library. Download and extract the latest armoryapi package and you will find a DLL and an XML file inside, make note of where you have extracted these files and head back to Visual Studio. Open the “Add Reference” dialog (see http://msdn.microsoft.com/en-us/library/wkze6zky(v=vs.80).aspx) and click the tab labelled “Browse” and navigate to the armoryapi DLL that you extracted.

Note: in order for the intellisense documentation to work you must also extract the XML file to the same directory as the DLL

Code

Now that your project is setup let’s get started with some code, first thing’s first let’s create a very simple form that we can use to fetch and display the data. Create a form and add the following controls:

characterNameTextBox – TextBox
realmTextBox – TextBox
achievementList – ListBox
fetchButton – Button

You should now have something similar to this (I have added a few labels and a groupbox, but you obviously don’t have to do this to get started):

The main form

Now that we have our GUI sorted double click on fetchButton to create the click event handler for the button. Once you are in the code view we need to add an import to the ArmoryAPI namespace, to do this add the following line at the top of the file:

VB.Net

Imports ArmoryAPI

C#

using ArmoryAPI;

Once you’ve added the namespace import go back to the event handler for the button click and add the following code:

VB.Net

Dim armory As Armory = New Armory(BattleNetRegion.Europe, BattleNetLocale.en_GB)
Dim character As Character = armory.GetCharacter(characterNameTextBox.Text, realmTextBox.Text)

For Each a As UnlockedAchievement In character.Achievements
    If (DateTime.Now.AddDays(-14) < a.Unlocked) Then
        achievementList.Items.Add(String.Format("{0} ({1} points)", a.Title, a.Points))
    End If
Next

C#

Armory armory = new Armory(BattleNetRegion.Europe, BattleNetLocale.en_GB);
Character character = armory.GetCharacter(characterNameTextBox.Text, realmTextBox.Text);

foreach (UnlockedAchievement a in character.Achievements)
{
    if (DateTime.Now.AddDays(-14) < a.Unlocked)
    {
        achievementList.Items.Add(String.Format("{0} ({1} points)", a.Title, a.Points));
    }
}

If you debug the project now you will be able to fetch the achievements the character entered has unlocked within the last two weeks as can be seen below.

Main screen with achievement data

This code queries the European servers and returns the data in English, if you need a different configuration simply change the line that creates the Armory object, the following regions and locales are supported:

Regions

  • America
  • China
  • Europe
  • Korea
  • Taiwan

Locales

  • de_DE
  • en_GB
  • en_US
  • es_ES
  • es_MX
  • fr_FR
  • ko_KR
  • ru_RU
  • zh_CN
  • zh_TW

Extra Steps

In order to improve upon this project, you can utiilise the caching functionality of armoryapi, to do this you’ll have to setup a database (see http://code.google.com/p/armoryapi/wiki/GettingStarted#Setting_up_a_Database) and simply pass the connection string to the Armory object and the time in minutes that the data should be cached for as shown in the example below.

armory.ConnectionString = "Data Source=(local)\SQLEXPRESS;Initial Catalog=armoryapi;User ID=username;Password=password"
armory.RefreshInterval = 120
Posted in Gaming, Programming, Technology, Tutorials at October 8th, 2011. 2 Comments.

Run Scripts in Separate Threads in PHP

I’ve been working on a new project over the past few weeks and one of the requirements of the system was to update a number of records in the database based off the data stored in a large quantity of XML files that would be stored on an external CDN. While initially this didn’t seem a problem, once the number of records grew that were required to be updated it became obvious that doing the updates sequentially wasn’t going to work, and as I didn’t have the required extensions to use pcntl-fork I had to come up with another way to launch multiple updates simulatenously, which turned out to be much simpler than I had thought but took a bit of searching to find:

<?php
	exec("nohup /usr/local/bin/php -f /path/to/script/file.php parameterValue &gt; /dev/null &amp;");
?>

This will execute the PHP script stored in file.php in its own thread but the PHP script calling it will not have to wait on it finishing execution as it would be if you called the script without the nohup and > /dev/null &. This solution will only work on *nix systems as opposed to Windows, but I imagine there is probably a way to do it in Windows too. The downside to this is that you will not be able to get any return value from the script, therefore it is only of any use if you need to run a task that does not require the calling file to get the results back. You can however pass data to the script as I have done in the example above, the value “parameterValue” would be available to file.php via $argv[1], for example:

<?php

	/* -- file.php -- */
	echo 'The value '.$argv[1].' was passed to me via a CLI command.';

?>
Posted in Programming, Web Development at September 6th, 2011. No Comments.

Minecraft Server Backup Script

As I mentioned in my previous post, I have started hosting a dedicated Minecraft server and I have created a script that will backup all the world data every night so that should any of the files get damaged at all we can easily restore to a given date.

For anyone else who needs this sort of solution you can find the script below, make sure to change the MC_PATH and BACKUP_PATH variables to the location of your server and the folder you want the backups to be stored to.

Note: this will create the following folder structure in the backups folder: Year/Month/Day


#!/bin/sh

MC_PATH="$HOME/minecraft"
BACKUP_PATH="$HOME/minecraft_backups"
NEW_FOLDER=`date +%Y/%m/%d`

echo "Creating backup directory..."
mkdir -p $BACKUP_PATH/$NEW_FOLDER

echo "Starting backup..."
cp -R $MC_PATH/world $BACKUP_PATH/$NEW_FOLDER

 

echo "Successfully backed up to: $BACKUP_PATH/$NEW_FOLDER"

Posted in Gaming, Programming, Software, Technology at April 11th, 2011. 3 Comments.

Setting up a ReadyNAS Shutdown Button in Ubuntu

If you’re as lazy as me, you’ll hate having to login to the web front end for your ReadyNAS every time you want to shut it down or getting up and holding down the power button for 5 seconds (it isn’t just me that hates doing this, right?). I finally decided to make an easier means of doing this which I thought I’d share with you all.

Although the title says this is for Ubuntu it should work in pretty much any Linux distribution seeing as it’s all pretty standard stuff. If you are using a different distribution you’ll have to use your distribution’s alternative to apt-get if it isn’t Debian.

The first thing you will need to do is install the Expect package using the following command:

sudo apt-get install expect

The reason we need Expect is to make the automation of the commands easier.

The second thing you will need to do is install the EnableRootSSH addon which is available from the official Netgear site (http://www.readynas.com/download/addons/4.00/EnableRootSSH_1.0.bin). Once you have downloaded this, login to your ReadyNAS web panel and make sure you are in Advanced Mode. Once logged in click the “Update” option underneath the System section and then click the “Local” tab. This screen will allow you to select the bin file you have just downloaded and install it to your ReadyNAS by pressing the “Upload and verify image” button. After the installation is complete you will have to restart your ReadyNAS.

Now that we have root access via SSH to the ReadyNAS and have Expect installed create a new document on your desktop and call it “ShutdownNAS.sh” and open it up in a text editor.

Copy and paste the following into the file, replacing IPADDRESSOFTHENAS with the I.P address of the ReadyNAS and YOURPASSWORD with the password you use to login to the web panel for the ReadyNAS (be sure to leave the \r at the end of the password) and save it:

Note: for some reason my blog is replacing the double quotation marks with a different character that will not work, so if you copy and paste this be sure to replace any quotation mark manually.

#!/usr/bin/expect

spawn ssh IPADDRESSOFTHENAS -l root
expect “*password:”
send “YOURPASSWORD\r”
send “\r”
expect “*:~#”
send “poweroff\r”
expect eof

Now run the following command to make the file executable:

chmod 755 ~/Desktop/ShutdownNAS.sh

Now if you drag the file into a terminal or double click and press Run it will login to your NAS and shut it down for you; simples :)

If you have any problems with this or want any help / information for expanding on this script feel free to ask.

HTML Engine Limitations in Outlook 2007

It’s been a while since I’ve posted on here, I have been incredibly busy as of late but today at work I came across something I thought I would share as it may save some people quite a lot of trouble. I was helping a colleague produce a HTML driven e-mail which requires some absolute positioning, tested it in IE – no problems, so naturally I assumed it would be no issue in Outlook as Outlook uses IE’s engine to render HTML – wrong.

In Outlook 2007 Microsoft have found it a good idea to use Microsoft Word to render the HTML as opposed to a web browser, who’s purpose is to render HTML. As a result of this a lot of CSS support has been cut (including positioning).  So before you start doing any coding for HTML based e-mails that will be read via Outlook 2007 I would strongly recommend taking a look at the Word 2007 HTML and CSS Rendering Capabilities in Outlook 2007 article on MSDN as it contains a full reference as to what is now supported.

I also came across a very handy eBook by John Doub which contains some tips and tricks to work around the newly introduced limitations, Click Here to Download HTML e-Mail Rendering in Outlook 2007 by John Doub.

They say sometimes you have to take a step backwards to move forward, but this is one giant leap backwards as far as I’m concerned; hopefully Microsoft will bring back the HTML support that Outlook was once well known for.

Posted in Programming, Software, Technology, Web Design, Web Development at September 14th, 2010. No Comments.

EDK 0.3 – Electric Drum Kit Preview

EDK is a new project of mine which is still in its infancy but is progressing at a rather promising speed. The goal of EDK is to provide a high quality and customisable drum module that is compatible with a plug and play kit.

The drum kit consists of four highly durable pads (similar to those you would find on any retail electric drum kit) and one pedal which connect to the MadCatz RB module via male stereo connections on the end of each pad. Once the pads are connected to the module, the module then connects to the computer via USB.

As can be seen in the demonstration video below the interaction code is fully working, along with the resource management (otherwise you wouldn’t be able to load and use the sounds), however there is still quite a long way to go before it is of much use to anyone as I still have to work on the GUI that allows for customisation; it is all coming all very nicely though.

If you have the time leave some feedback and let me know what you think of it so far :)

Posted in Computer Hardware, Music, Programming, Software at March 7th, 2010. 6 Comments.