Archive

Posts Tagged ‘technical’

SpaceFight! trigonometry and limiting mobility

November 20th, 2010 No comments

One of the “features” I wanted to include in SpaceFight! is a range of mobility for the player pawns. The units of measurement for the distance is arbitrary, and the screenshot below reflects 200 “units”.

That green circle surrounding the highlighted pawn shows the player the possible area they can place that pawn. The tricky part here was limiting the “potential new location” to never exceed the radius of that circle. Perhaps there was an easier way to do this, but I solved it with a little high school trigonometry.

Calculate distance

a^2 + b^2 = c^2

or

square root of (a^2 + b^2) = c

In this case, a = x1 – x2, and b = y1 – y2 (using cartesian coordinates)

dist = Math.sqrt(
Math.pow((getX() - getMouseX()), 2)
+
Math.pow((getY() - getMouseY()), 2)
);

If that value c exceeds the pawn’s range then find the coordinate of the intersect between the circle and the line drawn between the pawn and the mouse cursor.

Calculate angle

Now, to force the new location to be where the lines intersect we only need two things. First, the distance which is just the pawn’s range of mobility. Second, the angle between the pawn and the mouse cursor.

angleRadian = Math.atan2(
(getMouseY() - getY()) ,
(getMouseX() - getX())
);

Set the location

Now that we have the angle we can calculate the new location.

pX = getX() + range * Math.cos(angleRadian);
pY = getY() + range * Math.sin(angleRadian);
setNewLocation(pX, pY);

Now if the mouse is located outside the pawn’s range then the new location will always be somewhere along the edge of the pawn’s range.

FizzBuzz and a false coin

April 16th, 2010 No comments

I just had my co-worker visit me at home. We all work remotely so we don’t see each other on a daily basis, but he was passing through town. He brought up an interesting topic; a programming “test” called “FizzBuzz”.

The rules: Print out all values between 1 and 100. When a number is divisible by 3 print “FIZZ” and when divisible by 5 print “BUZZ”. When the number is divisible by both, print “FIZZ BUZZ”.

My simple solution to it (a Perl script) is as follows:

for ($i = 1; $i <= 100; $i++) {
 print "$i:\t";
 if ($i%3==0) { print "FIZZ "; }
 if ($i%5==0) { print "BUZZ "; }
 print "\n";
}

This meets the criteria, though I don’t check if a number is specifically divisible by both within a condition.

This brought back a memory of an “Algorithm Analysis” exam from University. I call it “The False Coin” problem, though it probably has many names.

You have a pile of coins. All of the coins are equal, except one, which is lighter than the other coins. This is the false coin. You also have a scale which allows you to compare two weights against each other. Using the scale, what is the most efficient way to find the false coin?

The other students insisted you split the pile of coins into two equal piles, weigh the two against each other and find the lighter pile, then split the lighter pile into two piles, and so on.

Close. But wrong. There is a better way and it involves splitting the first pile into three equal piles. Compare two piles, identify which of the three is the lightest pile and split that into three more equal piles, and so on. With every weighing you get rid of 66% of the remaining coins, compared to only 50% when splitting the piles by half.

So keep that in mind before your next programming interview.

Beer Bottle Rocket Man

October 30th, 2009 No comments

It’s done. I made the deadline (after two full days of working on it).

BBRM_Teaser01

Next time I’ve got a full month to work on the game, I’ll manage my time better. No more leaving it until the last few days.

Here is the forum post announcing the details.

Pretend this is an old Nintendo game and the story
and instructions are in the booklet but the game doesn't
make sense without the booklet.

This is the booklet: The evil doctor wants a beer, so he
sent you, his robot, down into the cellar to get it.
It's easy enough for you to walk down stairs, but your
legs are too short to climb back up. Being a heavy robot
with short legs, you can't jump very high. 

How are you going to get back up to the doctor to give
him his beer?

Note: It is possible to finish the game with a beer for each of
you and the doctor.

Switch to our mobile site