Code Snippet: Launch velocity from target position and angle


This is the first of hopefully many little coding snippets; pieces of code I feel may be useful to others. Most of the code will come from theRedEngine, the 3D Engine I’ve been working on for a few years now, so you may have to do a bit of ctrl-h’ing to format it appropriately. Many of the snippets will assume some formal knowledge and may require prerequisite functions. In general, these functions will be widely known but if you struggle please do contact me or use the comments. These snippets may be hot off the press (I.e. I’ve just implemented them), so could be subject to optimisation, cleaning-up and enhancements over time. I’ll try my best to reflect those changes here.

The first snippet is a handy function to set the launch velocity required to hit a target when firing at a specified angle (in degrees). The ‘tRE_CBody’ class contains members ‘m_vPosition’, which is the origin of the projectile, and ‘m_vVelocity’ which is the value we’re calculating. The only other input is gravity, which I’ve defaulted to 9.81f. This function accounts for vertical offsets too, i.e. the angle is relative to the vector between source and target positions:

// Set the velocity required to hit the specified target using the specified launch angle (degrees)
tRE_void tRE_CBody::SetLaunchVelocityToHit( const tRE_f32 kfAngle, const tRE_SVector3f& krvTarget, const tRE_f32 kfGravity )
	// Projectile velocity
	const tRE_SVector3f kvDifference = ( krvTarget - m_vPosition );
	const tRE_f32 kfDistance = kvDifference.Length();

	// Launch angle
	const tRE_f32 kfTheta = tRE_DegreesToRadians( kfAngle );

	// Determine slope distance (
	const tRE_f32 kfFlatDistance = tRE_NMaths::Sqrt( ( kvDifference.X * kvDifference.X ) + ( kvDifference.Z * kvDifference.Z ) );
	const tRE_f32 kfSlopeAngle = tRE_NMaths::ATan( kvDifference.Y / kfFlatDistance );
	const tRE_f32 kfSlopeDistance = kfDistance / ( ( 1.0f - tRE_NMaths::Tan( kfTheta ) * tRE_NMaths::Tan( kfSlopeAngle ) ) / tRE_NMaths::Cos( kfSlopeAngle ) );

	// Required firing velocity
	const tRE_f32 kfRequiredSpeed = tRE_NMaths::Sqrt( ( kfSlopeDistance * kfGravity ) / tRE_NMaths::Sin( 2.0f * kfTheta ) );

	// Rotate launch angle
	tRE_SMatrix44 mTransform;
	mTransform.BuildLookAt( m_vPosition, krvTarget );

	tRE_SMatrix44 mRotate;
	mRotate.BuildRotate( tRE_SVector3f( -kfTheta, 0.0f, 0.0f ) );

	tRE_SMatrix44 mInverse( mTransform );
	mInverse *= mRotate;
	mInverse *= mTransform;
	mTransform *= mInverse;

	// Set rotated launch angle/speed
	m_vVelocity = ( mTransform.GetForward() * kfRequiredSpeed );
Posted in Coding, Games, Snippet | Tagged , , | Leave a comment

My experiences with Gold/Silver investment


(Invest in silver and gold bullion here: BullionVault – Online gold and (VAT free) silver trading – Free gram of gold/ounce of silver when registering!)

A few years ago I’d never even considered silver or gold as an investment, to me it was jewellery, nothing more. But then a friend mentioned he’d started investing in it. At the time I’d just started looking at investing in shares so thought what’s the harm in diversifying my portfolio. As it turned out, it’s one of the greatest invetments I ever made.

Please note, I’m not a professional accountant, investor or speculator, I’m just you’re average guy looking to save money as effectively as possible. I accept no responsibility for any financial loss – we all know investments can go down as well as up.

I started investing in silver and gold bullion around 2 years ago. I use a drip-feed approach, much like I do with shares, as opposed to buying in bulk so it stabilises any flutuations in price (one month it’s low, the next month it’s high – it balances out), so each month I buy a certain amount.

There are two real options for buying bullion, physical and virtual. Physical means you buy actual bullion bars and get them delivered to you, virtual means you buy the bars but someone else looks after them. Both have their advantages and disadvantages, with physical you get to hold your pretty new bars but have to make sure they’re safe. With virtual, you know they’re safely stored in a vault somewhere but you have to trust that is the case.

‘Give me the numbers!’ I hear you shouting. Ok, in 2 years, my silver investment has gained 99.78% (yes, nearly doubled in value!) and my gold investment has gained 32.47%. Not too shabby I feel!

I personally prefer silver, and my understanding is that it’s used a lot more in industry with supplies predicted to run out imminently, so believe it has more potential than gold. In the UK however, silver bullion still attracts VAT so needs to gain more to provide a profit. Gold bullion is a safe option, but used less in industry, so my feeling is that its gains will be more reserved. Gold bullion does not attract VAT in the UK, however, so can generate a more immediate profit.

I personally have double the amount of silver than gold and will be keeping it that way for the foreseeable future. ‘So where do I start?’ you ask. I use the following, virtual, company who also provide a free gram of gold/ounce of silver on registration (approx £25 worth each), all you pay is a small holding fee each month for them to keep your bullion secure:

BullionVault – Online gold and (VAT free) silver trading – Free gram of gold/ounce of silver when registering!

Posted in Financial | Leave a comment

WD TV Live ‘Invalid username or password’


The Western Digital TV Live is an excellent piece of kit, you can throw anything at it and the WDTV will play it. The main reason I purchased it was to stream content from my PC, via. network shares. At first this worked flawlessly but all of a sudden things stopped working.

I’m using Windows 7 Ultimate 64-bit and tried all manner of things to fix it; playing with homegroups, making sure I had ‘everyone’ listed under my shares, triple checking all of my share options but nothing worked. After a bit of reading I found the culprit! What, you ask? Microsoft Live Essentials of course! Yup, I uninstalled this and suddenely everything worked again. Futher reading suggests that this isn’t just an issue for WD boxes but other media streaming devices. Hopefully WD/Microsoft will provide a fix but in the meantime I’ve opted to abandon MSN Messenger.

WD TV Live

As previously mentioned, these are excellent pieces of kit; able to stream full 1080p, connect to YouTube and handle all manner of video, audio and picture formats. You can purchase one of these units here or wish for one here!

Posted in Gadgets | Tagged , , | Leave a comment

WishForThis (Beta) now open to UK users!


After many late nights, I’ve managed to get the Beta of WishForThis up and running with registration enabled for UK users. So what is WishForThis you ask?


WishForThis Homepage

Here’s the blurb:

‘The answer to the question, “What would you like..?”. Be it your Birthday, Christmas, Wedding, Anniversary or just a friend feeling generous, you can simply answer, “Check out my wish list!”. Unwanted presents could be a thing of the past! WishForThis is the online wish list that you can access at any time, anywhere in the world!

In its simplest form a WishForThis list is just a convenient place to store a list of everything you’d like. As and when you can have some spare cash, you can buy the items on your list by following the link through to the respective retailers.

The real purpose of WishForThis is for your friends and family to be able to see what you’d like too. Next time it comes to your Birthday, Christmas etc.. they’re just a few clicks away from buying you something you’d really like. It saves them the sometimes tricky task of finding a gift for you and it saves you the disappointment of possibly another unwanted present.’

With just over a month to go until Christmas now is the time to sign up!

(More information can be found here)

Posted in WishForThis | Leave a comment

Domain parking/redirection loop bug in cPanel


I suffered an issue the other day that I though might be worth a mention. As well as, I also own the domain To get the domain to forward to the .com site I added the as a parked domain under the cPanel of the main site, then re-directed to Easy enough.

There was one slight problem though. When I visited a sub-folder of (i.e. /Links), it would remain on instead of forwarding. Turned out I needed a wildcard re-direct as well as making sure my target site ended with a ‘/’. The problem was that when I tried to append ‘/’ to my target domain, cPanel would fail: ‘Redirecting to will cause a redirection loop..’.

After a bit of investigation it turned out cPanel was writing the wrong code to my .htaccess file. I believe this to be a known bug that’s et to be fixed. The solution? Do it manually:

RewriteCond %{HTTP_HOST} ^$ [OR]
RewriteCond %{HTTP_HOST} ^$
RewriteRule ^(.*)$ "http\:\/\/www\.mishtertea\.com\/$1" [R=301,L]

If copying, you’ll just need to change the ‘’ and ‘’ to you source and destination domains respectively. Once updated this also shows up correctly in cPanel.

Posted in Coding, Web | Tagged , , , | Leave a comment

Chilli (Con Carne) with balls


I like chillies. I _really_ like them. I’ll add them to pretty much anything, but Chilli Con Carne is the perfect marriage. Over the past few years I’ve used the same recipe, which usually get’s a good reception, so thought I’d share.

WARNING: This is hot, and I don’t mean Vindaloo hot, Vindaloo rates as ‘if I concentrate I can feel a slight tingle’ on this scale – chilli is addictive and over time you develop a certain tolerance – this may take some practice! This recipe will make you sweat and possibly have rather poorly guts the following day, but is mega-tasty! I accept no responsibility for breaking people – but please, be sensible, this should not be tried, or even touched, by children.

First step is to go here and buy these: Dried Naga Jolokia Chillies. These are what make this chilli stand out from the rest, they’re hot! (and I mean hot – even I wouldn’t eat these whole!)

Ingredients (Serves 4-6)

  • 1 tbsp oil (olive/veg)
  • Gloves (for chopping chillies – optional, but recommended for the weak)
  • 2 medium onions
  • 3 cloves garlic
  • 2 medium carrots
  • 500g steak mince/minced beef
  • 1 tube tomato puree
  • 1 tsp ground coriander
  • 1 tsp cinnamon
  • 2 tsp cumin seeds
  • 5 tsp hot chilli powder
  • 1 tin chopped tomatoes
  • 1 cube vegetable stock/500ml boiling water
  • 2 small squares dark (preferably 80%+) chocolate
  • 2 tins kidney beans
  • 8 dried Naga Jolokia, chopped/crushed
  • Small packet fresh bird’s eye chillies (approx. 10), chopped


  • Few droplets (depending on strength!) hot chilli sauce (500k Scoville+ I.e. Da’ Bomb)
  • 1 dried Chipotle chilli, chopped/crushed (adds a nice smoky flavour)
  • Fresh coriander


  • Chop onion, garlic, carrots and fry until onion has softened
  • Add mince, fry until browned
  • Add tomato puree and powdered herbs/spices, cook for 5 minutes
  • Add tinned tomatoes, stock and chocolate
  • Add kidney beans and fresh/dried chillies + optional chilli sauce
  • You’ll have quite a watery mixture due to the stock – simmer until it’s reduced to a sauce
  • Once reduced it’s ready – optionally, garnish with fresh coriander

Now all that’s left is to psych yourself up and eat. It’s great with rice, baked potato or tortillas. Whichever option you take, some (/lot’s!) of sour cream really helps. Any left-over’s can be stored for a few days, and also frozen. I find it actually improves, both heat and taste-wise, with age!


Posted in General, Random | Tagged , | Leave a comment

PHP crc32 inconsistent return values across servers

I’ve been beavering away recently on WishForThis and encountered a nice little issue, worthy of a quick blog post. I use crc32 in PHP for some hashing, which is all fine and dandy for my purposes, but when my site moved to a new server the values returned were inconsistent with my previous server!

This caused quite a headache, but after a bit of investigation it turns out the issue is that my new server is 64-bit whereas the previous was 32-bit. It seems that on 64-bit implementations, crc32 will always return an unsigned integer, whereas on previous versions it could be signed.

The solution? A handy bit of PHP to make the return signed:

function StdCRC32( $uValue )
	$uCRC = crc32( $uValue );
	if( $uCRC & 0x80000000 )
		$uCRC ^= 0xffffffff;
		$uCRC += 1;
		$uCRC = -$uCRC;

	return $uCRC;
Posted in Coding, Web | Tagged | Leave a comment

Prevent duplicate messages with Outlook rules

A quick post here for anyone that’s added a rule to Outlook and since had duplicate messages appearing. The problem is that Outlook doesn’t terminate when it processes the first rule it matches, so it’ll carry on until everything is satisified.

The solution? The ‘stop processing more rules’ rule! When you add your new rule and get to the ‘Select action(s)’ box, make sure you tick the aforementioned box too. Job done.

Posted in Web | Tagged , | Leave a comment

Website/Twitter widget validation


Whilst trying to validate this site using the W3C validator ( – very handy to check your site conforms to standards – something for another blog), I found an issue with the (official) Twitter widget I use refusing to validate. The widget code is as follows:


    NOTE: I’ve added the ‘DELETETHIS’ to the id to prevent the Twitter widget to the right of this blog injecting it’s code into this post. The syntax highlighter also appears to have removed double quotes around id’s and target’s for some reason.

    The problem is the empty <ul></ul> statement – it’s not valid XHTML 1.1. The solution?

    • Attempting to load Twitter feed..

    All I’ve done is added a <li>Attempting to load Twitter feed..</li> – this allows it to validate and handily, also allows you to display a message whilst the widget is loading..

    Posted in Coding, Web | Tagged , | Leave a comment

    Re-ordering e-mail accounts on iPhone


    I currently have about 15 e-mail accounts for various sites I have set up. I’ve added them to my iPhone but they all get jumbled up – OCD not happy! Having searched through every menu possible, I couldn’t find a way to re-order my accounts. But, there’s a trick!

    NOTE: This assumes you have Outlook or another mail client supported by iTunes in use, with your mail accounts attached. I also find it best to delete all mail accounts from my phone before doing this.

    Open up iTunes, select your phone and go to the ‘Info’ tab – scroll down and find the ‘Sync Mail Accounts from’ option – tick it and select your mail client. Now select the account you want to be first in the list and ‘Apply’ it. You may have to use the ‘Replace information on this iPhone’ button in the ‘Advanced’ section at the bottom of the page to get the first account to stick. Next, keeping the previous account selected, tick then next account you’d like in the list and ‘Apply’. Repeat as necessary.

    It’s a bit messy, and will have to be re-done if you add new accounts but it works. Hopefully Apple will eventually provide a simpler, phone based, solution..

    Posted in Gadgets, Web | Tagged , | Leave a comment

    > Talk to me!

    Twitter profile for Richard Turnbullwitter:
    • Attempting to load Twitter feed..
    Facebook profile for Richard Turnbull LinkedIn profile for Richard Turnbull

    > Tools

    Share |

    > Advert