Archive for the ‘Technology’ Category.

Getting the Wiimote Pointer Oriented

One of the things the Wiimote doesn’t know by default is whether or not it’s upside down. This presents a mild problem when using the Wiimote Library and you want to rotate the pointer most every Wii game does.

The trick is realizing that the Wiimote is calculating the angle based on the two outer points. To know if you’re upside down you simply check to see which point is to the left of the other. You then invert the angle if the points are flipped.

The angle given by the Wiimote is from -1 to 1 so you subtract the angle from 2 when the remote is upside down. You multiply the given angle by 90 degrees to get the 0-360 degree angle.

In Wii-Land the x axis is pointing into the screen so we use the x axis rotation to determine how to rotate the pointer on the screen.

You can see in the code below that the x pos is flipped. That’s because 1024 is the left side of the screen and 0 is the right side of the screen according to the Wiimote. I flip that as well to simplify things.

This is all part of the work I’ve been doing with software rendering. I finally got a wireless Wii sensor bar to use with my computer so I can start making games that make use of the Wiimote and other Wii controllers.


if (ws.IRState.IRSensors[0].Found && ws.IRState.IRSensors[1].Found)
{
remote.rot_x = accel.X;
remote.rot_y = accel.Y;
remote.rot_z = accel.Z;

if (ws.IRState.IRSensors[0].Position.X < ws.IRState.IRSensors[1].Position.X)
remote.rot_x = 2.0f - remote.rot_x;

remote.pos_x = 1.0f - (float)ws.IRState.RawMidpoint.X / 1024.0f;
remote.pos_y = (float)ws.IRState.RawMidpoint.Y / 768.0f;
}

Free Ringtone Heaven 2.0 Going Live

Free Ringtone Heaven is currently being purged and reloaded. It’ll probably be an hour or two before it’s back up. The biggest change is to the file and directory structure both on the server and in the database. Previously all stats were tied to file ids. Now they’re tied to the file hash. What this means is that in the future I can do some reorganization on the files without having to take the whole site down. And no stats will be lost.

The next step is to separate the physical file location from the virtual directory structure. That will make it even easier to adjust the location of files without having to reupload all the ringtone files. With all the current changes, that switch should be transparent and require no down time.

Free Ringtone Heaven 2.0

Free Ringtone Heaven.com is getting a redo. When first designing a web-site you tend to whip things up quickly and get them out there to see what happens. Then over time you tweak it here and there and maybe add a feature or 10. And eventually the site becomes stale and ugly.

That’s when you get to go in and force yourself to do things right. It’s time to clean up the code, apply better code design practices, update the interface, tweak the database, etc. And finally when it’s all done you get to tear down the old site and replace it with the brand new one.

In the case of Free Ringtone Heaven there is myMobile which users could use to upload their own files. However, it was so poorly implemented I’ve decided to just tear it down. People weren’t really using it anyway. So there will be a brand new myMobile launched. So when the new myMobile launches you will need to create a new account.

In that sense, I got lucky. There’s nothing about the old site I have to save. Except for the ringtones of course. I’ve got the backend pretty much complete with far more intelligent usage tracking. myMobile will have a password recovery feature finally. I’ve also cleaned up the ringtone directory structure a bit which should make it easier to find what you’re looking for.

The coming changes are too numerous to list them all. My goal is to have the new version launched sometime this weekend. FunkyCatz.com is my other ringtone web-site which I used to try out some things that are now being applied to Free Ringtone Heaven.

There is an old saying “if it ain’t broke don’t fix it.” Well technically Free Ringtone Heaven ain’t broke but it certainly has a lot of room for improvement. My main goals are to fix some of the stale aspects of FRH with the new download tracking and improve myMobile.

Sometimes it’s good to go back deep into the code of a site you built long ago and give it a redo. You’ll discover things you could improve, simplify and maybe even learn more for your next project.

Running a Home Server

One of the nice things about being able to run a server off your home internet connection is that you can do whatever you want. You can build the server you want, install the software you want, run the web-sites you want. The downside is that home internet connections aren’t always reliable. They’re also much slower than you can get with a hosting company like GoDaddy.

However, they are a good way to test out web-site concepts before spending the money on hosting them somewhere else. There used to be a lot of misconceptions about the cost of running web-sites and they probably still exist. The biggest misconception is what hardware is required. A “server” is just a PC. My home server is a run of the mill dual core Intel processor running at 1.7Ghz per core. The motherboard and the CPU cost less than $100 combined. In addition I have four harddrives (OS, website files, database files, log files) although one would suffice if you’re just getting started. Everything is contained in a standard mid-tower case which cost around $35.

You can purchase more expensive computers that are designed to handle high volume traffic. But you don’t need to. I think the misconception about what hardware is needed comes from what server computers typically look like; long and flat. That’s because they are typically stored in a rack and every unit of rack space costs money. A lot of money. With a home server you don’t have to worry about saving money by not having to pay for space to store your system. It can be as bulky as you want it to be. That means you can buy whatever parts you want. You don’t have to buy expensive parts designed to fit into a confined space.

So now you know that you don’t need special hardware to run a web-server. You can build from parts or buy a pre-built desktop system. The next misconception is the software. The software is really what separates a server from a home computer. A lot of people will probably tell you that you should run Linux because it’s “faster” or “more secure.” Both things are essentially false. If you happen to like Linux and want to use it then great. But more than likely when you bought he system or built it, you put some version of Windows on it. You can leave Windows on it. I would actually recommend it. There’s enough to learn about the actual server software that you shouldn’t subject yourself to a foreign operating system.

When people talk about “Linux” being faster or more secure in the context of web servers, they’re not talking about the operating system. They’re talking about Apache and IIS. Even professional highly paid writers who specialize in technology often can’t tell the difference between an operating system and applications. IIS is the web-server application that comes with professional and server versions of Windows. Apache is a lightweight web-server application that was originally made for Linux but, guess what, it also runs on Windows. Apache is just as fast and just as secure on Windows as it is on Linux.

I prefer Apache although I also develop for IIS. Apache is free and will run on anything. I once handled over 1 million requests in a month with Apache running on Windows Millenium. The simple fact is, the operating system doesn’t matter. It’s the software that runs on top of it that matters.

The typical software that people use to run hobby web-sites is Apache, MySQL, PHP and Subversion. All the software is 100% free and runs just as well and just as securely on Windows as it does Linux.

The security of Apache comes from the fact that it does the bare minimum. It only serves up web-pages. The lack of security for IIS (or the difficulty of making it secure) comes from the fact that IIS does a lot. If IIS only did what Apache did it would be just as secure out of the box.

What makes the operating system secure is only having the ports open that you want people to use. Namely, the port that Apache is using to serve up web-pages. Typically that’s port 80. A $30 router will lock down your network as well as anything.

So you can see that running a server may involve a bit of education on your part. But it doesn’t require a lot of money. The software is free. You can use any old computer you have lying around. And then you can go to town making whatever web-sites you want to make. If a web-site becomes very popular then you can look into other options for how to host the site. But until then, don’t waste your money.

Hyperspeed

Back in the early to mid 1990’s I decided to create a racing game called “Hyperspeed.”

You can actually download it from here although I’m not sure how functional it is. There was nothing fancy to it. There were three track pieces: straight, left and right. Shifting shades of gray were used to simulate movement. It was written in BASIC.

After mucking around with Bunnies for quite awhile now (going on 5 years) I’ve decided to take a stab at bringing “Hyperspeed” back to life in the form of a Super Mario Kart clone. I was expecting it to be complex. Turns out that the techniques used to create Super Mario Kart are 100% identical to Bunnies minus the walls. So actually, it’s easier.

I originally created a new code branch for Hyperspeed but have now compiled a number of pieces into libraries in a “common” folder. Bunnies has been modified to use the new common code. Hyperspeed has been linked up with the Bunnies server. Earlier I had added a feature to the Bunnies website where you could upload a large image, break it into tiles and auto generate the floor of a map from it. Well, that’s why.

The floor is just the first step. Racing games like Super Mario Kart also need a collision map and way points. After playing Tower Defense for awhile and seeing some sample code I realized that you can simply use way-points like in Tower Defense to create racing AI and ensure that the player is going the right direction and not cheating.

A collision map is simply a gray-scale image that indicates whether a position within a tile slows the player down and by how much or if it stops the player entirely.

Currently you have to apply bump and depth maps one tile at a time. I’ll be adding a feature to allow collision maps and make it possible to apply them all to an entire image. There’s a very good chance that the maximum file dimensions will be going up. 2048×2048 works but I’d like to allow higher resolutions. 2048 gets you about 64 pixels per floor tile. I’d like to at least double that. The limit on the site is rather arbitrary. I could just remove the restriction.

Hyperspeed will probably not be released for at least a month. There’s a lot of work to be done to make it a game. In the mean time there’s a new release of Bunnies and an updated game server which fixed a few major bugs. Part of this project is to demonstrate the versatility of the Bunnies game engine and web-site. Once Hyperspeed is ready to be released Bunnies and Hyperspeed will be part of the same download package.

Mode 7 and You

Sin & Cos: The Programmer’s Pals! is probably the most straight forward example of Mode 7 implemented in a C like language available. You may be wondering what Mode 7 is.

F-Zero and Mario Kart for the SNES made use of Mode 7 and it has been used in quite a few games since. Mode 7 is a term to describe the ability to scale and rotate a texture. It simply translates the height of a texture into the depth.

Not so surprisingly you will find that the equations used to render Mode 7 are essentially identical to the equations used to render the ceilings and floors in Bunnies. Mode 7 is Bunnies without the walls.

The above link provides the basic rendering function but gets the key part of the equation wrong. Fortunately, Bunnies gets it right so I just used the equation from Bunnies.

WRONG:
distance = camera.z * scale_y / (screen_y + horizon);

CORRECT:
distance = camera.z * scale_y / (2.0 * screen_y - horizon);

I’ve started translating the old SoftGel C++ code to C# and this Mode 7 stuff is going to be the first to make use of the new library. I’m going to try to find time to start writing more tutorials. Bunnies is pretty advanced at this point and so I think I’ll begin developing a simple racing game and writing tutorials to explain how it all works. Eventually I may work the tutorials back to Bunnies.

You may be wondering what the point is with OpenGL and DirectX available. There are a number of reasons. The main one is that I’ve been there and done that with OpenGL and DirectX. I want to learn the math behind the rendering. The second reason is that most of the work that goes into making a game has nothing to do with rendering graphics. And also, you’ll find many of the same concepts that are used to render graphics are also used to implement game mechanics.

And finally, software rendering works on any platform that can plot a pixel. Not all gaming systems have 3D capabilities. If you can figure out the 3D stuff in software you’ll have no trouble doing it in DirectX or OpenGL.

Fun With MySQL

Error in my_thread_global_end(): 1 threads didn’t exit

If you get an error like that you need to upgrade your installation of MySQL to the latest version. If you’re using PHP then you need to update to the latest version of PHP. With .Net and languages like C# you are probably using the 3.51 ODBC driver to connect to MySQL. You’ll need to update to version 5.1 of the driver.

I’ve found that there’s not a lot of information on the net concerning this issue with C#. This is probably because .Net developers are writing web-apps which will silently ignore the error or they are running apps which exit and close the window so they never see the message. The only time you can see the error when coding in .Net is if you run a console app from the command line and the program otherwise cleanly exits. If you break out of the program you won’t see it.

Who uses C# to write console apps?

Fortunatly the error is not really an error. It’s a counting bug. MySQL sees your app’s thread and includes it in the thread count. MySQL exits first when your app closes and it sees that 1 thread is still open that it can’t account for so it throws an error.

The threads that alledgedly didn’t exit belong to your app and your app did in fact close so there are no threads that didn’t actually exit.

It looks scary but the error doesn’t mean there is an error in your app. It means that the MySQL lib can’t count.

The Poor Man’s Log Rotator


rotate_logs.bat:

bin\httpd -k stop

@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
Set Month=%%A
Set Day=%%B
Set Year=%%C
)

ren logs logs-%Year%-%Month%-%Day%
mkdir logs

bin\httpd -k start

Instead of copying or moving large log files it’s much faster to shut down the Apache log server, rename the log folder based on the date, create a new log folder and then start up the server again. Once that process is done you’re free to copy or move the log files where ever you want without affecting the web server.

You can put this code into a bat file and use Windows Scheduler to automatically rotate your logs every week or day or whatever.

I’ve had a lot of problems with having Apache automatically name files based on the year and month to rotate logs and this seems to be a far more elegant solution. Apache does the minimum amount of work logging activity and the bat file does the minimum amount of work rotating the logs. The result is only a second or two of downtime depending on how quickly Apache stops and starts.

Putting Apache Logs into MySQL

On Lamp has an article titles “Writing Apache’s Logs to MySQL” which explains how to log directly to a MySQL database. This seems like a good idea on the surface. There’s nothing better than a database to store large amounts of information for processing. However, it’s not necessary or even advisable to try to store a bunch of log information from Apache directly into MySQL.

If you want your Apache server to run efficiently you need to have it do as little work as possible. For small sites the novelty of putting logs directly into MySQL may seem like a good thing. But as your site gets more and more popular the time it takes to insert rows is suddenly going to be noticable.

The better approach is to stick to file based logging and do post processing on log files. It’s rather trivial to write a PHP or Perl script to parse logs files and populate a database with the information. I currently use a free PHP script (the “Apache Log Parser” script) which happily chugs away reading the Apache “combined” log format. The class is instantiated into a simple script which takes the lines read by the parser script and stuffs them into a database table based on the year and month the request was made. This keeps MySQL happy by not storing millions of rows spanning 8 or 9 years of access logs into a single table.

Log reports are generally done on a monthly basis anyway. If your server is really popular then creating tables based on the year month and day is easy enough to do.

In order to improve efficiency even more, my server stores all the log files on a dedicated hard drive. I have yet to find a good log rotater for Apache. Every time I try to rotate logs I end up with runaway processes and server errors. So to rotate logs manually simply stop the server, rename the current log folder, create a new log folder, restart the server. A few seconds of downtime and you’re free to process the log files without interrupting the server any further.

You could probably write a bat file to do that process and set up a cron to do it automatically every week or so.

WinTV Audio Sync Problems

I’ve had my WinTV PCI video capture card since about 1996.  The technology for these things hasn’t improved a whole lot since then.  It really comes down to the speed of your CPU.  One of the constant problems I’ve had is the audio being out of sync with the video.  There are two things I’ve found are necessary to solve the problem.  First, use the audio connector on the inside of the case.  Don’t go from audio out on the capture card to line in on your PC. The second is don’t compress the audio when capturing.

I use the free Divx Codec without audio compression and get perfect audio/video sync as far as I can tell.

ss_blog_claim=70b9168863fc97c91e6d88b40542a327 ss_blog_claim=70b9168863fc97c91e6d88b40542a327