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. 4 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. 1 Comment.

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.

Enabling Last.FM Scrobbling in VLC

Open the preferences window by going to Tools > Preferences or pressing CTRL + P and switch the “Show Settings” radio button to “All”.

Drop the “Interface” menu option and click “Control interfaces”, you will now see an option labelled “Submission of played songs to last.fm”; check this option.

Drop down the “Control interfaces” menu option and click on “Audioscrobbler”, enter your login details on this screen, hit save and restart VLC.

Posted in Music, Networking, Software, Technology at July 2nd, 2011. No Comments.

Google+

I finally got myself an invite to the new Google+ network, and it is awesome! If you haven’t heard of it, it’s essentially Google’s attempt to combat Facebook; and to be frank, if the beta is anything to go by it’s going to be a Fatality.

As you can see in the two screenshots below, the Stream is the equivalent to Facebook’s Wall, and looks and works in a similar way (I think at this point doing anything radically different wouldn’t be a smart move on their part). It contains people’s latest activity such as photo uploads, people being tagged in photos, messages etc. The key feature in this which a lot of people seem excited about, is the ability to select only certain “circles” to share information with (see the third screenshot below).

The other big thing that is causing some hype is the “Hangouts”, these are essentially chat rooms which you can start with members of your circles but with the ability to utilise both audio and video. I’ve unfortunately not been able to try this out as of yet, but if the quality of this feature is up to the same standard that the rest of the system is, it will be very impressive. In terms of problems I’ve surprisingly not ran into any yet, for a product that hasn’t even been finished yet I’d say that is quite a promising sign.

Modded NES Controller

As an avid gamer I like to revisit some of the classics that I used to play as a kid, most notably Super Mario Bros. As I sold the system back in 1995 to get a SNES my only option is to play my old games on my PC, but playing Super Mario Bros just isn’t the same when you haven’t got a square brick for a controller that is giving you cramp in your hands! This ended up leading to me thinking – why not just convert an old controller to work via USB? And so I have. See below some pictures and a little demonstration video of the controller in use :)

Posted in Gaming, Hacking, Hardware, Software, Technology at April 15th, 2011. 2 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.

Mario & Yoshi Minecraft Statues

A friend of mine has been running a Minecraft server for a while from his house but as it is a bit of an inconvenience for people to not be able to play when he decides to shut down I have migrated it to a dedicated server. Along the way I decided to pick up a copy of Minecraft and give it a try myself and I can’t stop playing it now! After seeing someone building a Megaman statue on our server I decided to throw up a Mario and Yoshi statue to accompany it. Click on the thumbnail to view the full size image.

If you’re interested in joining us contact me for the details.

 

Posted in Gaming, Software, Technology at April 10th, 2011. No 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.

Running Spotify in Ubuntu 10.10

I’ve recently switched back to Ubuntu as my main operating system and wanted to continue to use Spotify but ran into a dead end after following Spotify’s own guide to run it in Ubuntu. I thought I would post up how I got it working as there are a number of people who have the exact same problem I had (that being that Spotify would display a message saying “There is a problem with your sound card”).

First of all, remove your existing WINE installation as we are going to be using the version that is currently in Beta. Do this by running:

  • sudo apt-get remove wine

Note: you will have to manually remove the .wine directory from your home directory (if you can’t see it press CTRL + H when viewing your home directory)

Now open the Software Sources menu by going to Applications->Ubuntu Software Center, then selecting Edit->Software Sources. Choose the Other Software tab and click Add then copy and paste the line below.

  • ppa:ubuntu-wine/ppa

Now that you have the repository setup use the following command to install the latest version

  • sudo apt-get install wine1.3

Once WINE 1.3 is installed grab the Spotify Installer from This Page, save it to your desktop, name it “spotify.exe” and run the following command to begin the installation:

  • wine ~/Desktop/spotify.exe

After the installation has finished use the following command to open the WINE configuration window:

  • winecfg

In the configuration window that appears go to the Audio tab and you will most likely be presented with a message box telling you that a driver has been automatically selected for you (this is fine). Make sure that only the ALSA Driver box is selected and then press OK.

You can now run Spotify by using the following command:

  • wine ‘C:\Program Files\Spotify\spotify.exe’

You can also create a launcher on your desktop using this command so you don’t have to load up a terminal every time you want to open Spotify.

Posted in Hacking, Music, Software, Technology at March 27th, 2011. 4 Comments.