For the latest 25 lines contest, I wanted to do something with a Valentine’s Day theme. I decided to try modeling a heart, and apply some lighting to it. Either of these would by themselves would be challenging to do in under 25 lines, and although I was able to achieve the result I was looking for, I could not quite get it down to 25 lines in time, and had to cut the animation. So I wanted to present it here as I wanted it to be seen. In retrospect it is probably not all that exciting, but I’m pretty happy that my idea for doing lighting worked.
The lighting works like this: you have a BitmapData object which is used for the texture map. You have the uvtData which specifies which points on the texture match up with which vertices on the model. So what I did is calculate the angle between the lighting angle (represented by a Vector3D object) and the normals of each triangle in the mesh. You get the normal of a triangle by finding the vector which is perpendicular to two of the lines in the triangle. Then you draw onto the BitmapData with the calculated shading color, using the uvt coordinates for that tringle in the mesh. So there’s a feedback between the position of the object and the BitmapData object used to skin it. Amazingly I was able to do this with just a few calls to some of the 3D api methods, and no hairy trigonometry was required. I will be doing another example of the lighting technique soon, with more details - just consider this a proof of concept at this point ;)
After watching this video by Mr Thibault Imbert at WiiFlash.org I’ve been wanting to try using the wiimote with the flashplayer on my MacBook. Briefly, WiiFlash consists of a server which reads the icoming bluetooth signal from the WiiMote , and an actionscript library which provides you with an API to access that data.
The Installation
Get a WiiMote (about $50)
Get WiiFlash server from the Download page on wiiflash.org. Differently than what the video says, both Mac and Windows servers are contained in the same .zip file. I just mounted the .dmg and dragged the WiiFlashServerJ.app to my Applications folder.
Set up a project with the WiiFlash.swc library. The video has an example using FlexBuilder. I just made sure to include the .swc in my library path using mxmlc. Compile.
start the WiiFlash Server
Pair your WiiMote (or similar Wii - controller) with the server, by holding down the 1 & 2 buttons. This takes about 5 seconds, then you should see the wiimote listed on the server panel. One thing I’ve noticed is that if you switch the wiimote off , you’ll need to restart the WiiFlash server to reconnect again.
I then found that it didn’t work - FlashTracer was reporting an error to do with a security violation. Thinking that perhaps the movie needed to be served, I tried it on my localhost. No joy. My next thought was that since it is using sockets, it must need a socket policy file. This is something new - you used to be able to set socket policy from a crossdomain.xml file, but not anymore. The socket policy file must be served on a specific socket. There’s a good article about setting up a socket policy server here: http://www.adobe.com/devnet/flashplayer/articles/socketpolicyfiles.html. The bottom line is you need to download the flashpolicy_d scripts and go to the Standalone folder. The depending on whether you prefer perl or python , execute the following command:
Or replace .py with .pl for Perl. you might want to put this in a script or just alias it in your .profile/ .bashrc.
And of course the policy xml file should exist at the path specified in the above command. Mine looks like this:
12345678910111213141516
<?xml version="1.0"?><!DOCTYPE cross-domain-policy SYSTEM "/xml/dtds/cross-domain-policy.dtd"><!-- Policy file for xmlsocket://socks.example.com --><cross-domain-policy><!-- This is a master socket policy file --><!-- No other socket policies on the host will be permitted --><site-controlpermitted-cross-domain-policies="master-only"/><!-- Instead of setting to-ports="*", administrator's can use ranges and commas --><!-- This will allow access to ports 123, 456, 457 and 458 --><allow-access-fromdomain="*"to-ports="1024"/><allow-access-fromdomain="*"to-ports="19028"/></cross-domain-policy>
The port number 19028 is the one I saw in the error message from the flash player when I first tried using the wiimote (the port 1024 is for something else, so you don’t need that line).
After that bit of yak-shaving, the test app from the video worked and also the nifty car demo in the papervision folder of the WiiFlash download. The .swf does not have to be on webserver, but can be run in the standalone player.
I’m still getting the hang of the API.. there’s a bug with the yaw property (y-rotation for non-aviators) - it doesn’t seem to work. So I substituted roll (X-rotation) for turning left or right. It was also very jittery as there seems to be some random variation from frame to frame. By averaging the previous and current frame values it gets much smoother. Here’s a demo of a swimming fish, which you should be able to control using a wiimote if you’ve followed the instructions… code follows.
Now this quickly becomes tricky to control, as it’s sometimes hard to determine the orientation of the fish, and ‘up’ / ‘down’ / ‘left’ / ‘right’ are relative to its own world-coordinates .. which a constantly being modified. I think this would be less problematic if the fish had an identifiable up and down-side.
The FishSwim is a modified version of the one in previous examples.
I’m really excited to have this new input device which seems to be perfect for controlling objects in 3D. I hope to generate some more examples soon, or better yet some games! I will endeavor to make things keyboard-/ mouse-compatible as well, but I think the wiimote offers a more intuitive control for 3D environments, if used correctly.
I was thrilled to discover that DreamHost has git installed, so I was able to use the above command to clone the repo onto my webserver.
At the moment there’s not much in it, other than an application I’m working on to generate textures and effects using the Perlin Noise code I developed a few months ago. Here’s where it’s at right now.
*edit* I’ve removed the demo of the app for the moment as its not working right now