Dec/082
Bridge Builder Game! (Concept)

Constraint based bridge simulator
Some of you might remember a game called pontifex or bridge builder if you don’t; it’s a game in which you construct a bridge with beams and than let a train or car drive over it to see whether it holds. It is really fun and quite educational at the same time (equals morally appropriate right?). After some more research into rigid body constraints I found out that pontifex probably used the same stuff like I used in the 2D constraint based rope. So with some minor modifications it was easy to create a bridge simulator and with a littlebit more code even a bridge builder. It is still far from a game suitable for release and profit but it is definatly a playable minigame. Take a look at the working example by clicking theĀ ‘Continue Reading’ link.
Sep/0817
Creating a PHP socket server
Socket server’s are great things for creating multiuse applications. Sockets are the most basic and thus efficient and quickest way for web applications Java, Flash, PHP, etc. to exchange data with eachother. With this you can create online multiplayer games, chat applications, mail and web servers, etc. Basically the only thing holding you back is your imagination. Most web dev’s however, don’t know how to create or use one. Well I found a way of creating a socket server with PHP. There’s not a lot of info about this on the web, but it is contrary to what many think a legal, possible and quite reliable use of PHP. It is actually even covered on the Zend Developer Zone. The code below is mainly based on that tutorial, but this one actually works in PHP5 and has been debugged a little further.
You will need three things for doing this tutorial;
- a PHP server enviroment at hand, because you will need to use your command line interface (don’t get affraid now, it’s easier than making web-apps)
- a way of creating PHP scripts; notepad, dreamweaver, etc.
- (if you want your socket server to run online) a free internet connected socket, which can pass through router, firewall, and is not beeing used by any other programs.
This is the debugged code, explanation is mainly in the comments and below;
<?php
// PHP SOCKET SERVER
error_reporting(E_ERROR);
// Configuration variables
$host = "127.0.0.1";
$port = 4041;
$max = 20;
$client = array();
// No timeouts, flush content immediatly
set_time_limit(0);
ob_implicit_flush();
// Server functions
function rLog($msg){
$msg = "[".date('Y-m-d H:i:s')."] ".$msg;
print($msg."\n");
}
// Create socket
$sock = socket_create(AF_INET,SOCK_STREAM,0) or die("[".date('Y-m-d H:i:s')."] Could not create socket\n");
// Bind to socket
socket_bind($sock,$host,$port) or die("[".date('Y-m-d H:i:s')."] Could not bind to socket\n");
// Start listening
socket_listen($sock) or die("[".date('Y-m-d H:i:s')."] Could not set up socket listener\n");
rLog("Server started at ".$host.":".$port);
// Server loop
while(true){
socket_set_block($sock);
// Setup clients listen socket for reading
$read[0] = $sock;
for($i = 0;$i<$max;$i++){
if($client[$i]['sock'] != null)
$read[$i+1] = $client[$i]['sock'];
}
// Set up a blocking call to socket_select()
$ready = socket_select($read,$write = NULL, $except = NULL, $tv_sec = NULL);
// If a new connection is being made add it to the clients array
if(in_array($sock,$read)){
for($i = 0;$i<$max;$i++){
if($client[$i]['sock']==null){
if(($client[$i]['sock'] = socket_accept($sock))<0){
rLog("socket_accept() failed: ".socket_strerror($client[$i]['sock']));
}else{
rLog("Client #".$i." connected");
}
break;
}elseif($i == $max - 1){
rLog("Too many clients");
}
}
if(--$ready <= 0)
continue;
}
for($i=0;$i<$max;$i++){
if(in_array($client[$i]['sock'],$read)){
$input = socket_read($client[$i]['sock'],1024);
if($input==null){
unset($client[$i]);
}
$n = trim($input);
$com = split(" ",$n);
if($n=="EXIT"){
if($client[$i]['sock']!=null){
// Disconnect requested
socket_close($client[$i]['sock']);
unset($client[$i]['sock']);
rLog("Disconnected(2) client #".$i);
for($p=0;$p<count($client);$p++){
socket_write($client[$p]['sock'],"DISC ".$i.chr(0));
}
if($i == $adm){
$adm = -1;
}
}
}elseif($n=="TERM"){
// Server termination requested
socket_close($sock);
rLog("Terminated server (requested by client #".$i.")");
exit();
}elseif($input){
// Strip whitespaces and write back to user
// Respond to commands
/*$output = ereg_replace("[ \t\n\r]","",$input).chr(0);
socket_write($client[$i]['sock'],$output);*/
if($n=="PING"){
socket_write($client[$i]['sock'],"PONG".chr(0));
}
if($n=="<policy-file-request/>"){
rLog("Client #".$i." requested a policy file...");
$cdmp="<?xml version=\"1.0\" encoding=\"UTF-8\"?><cross-domain-policy xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://www.adobe.com/xml/schemas/PolicyFileSocket.xsd\"><allow-access-from domain=\"*\" to-ports=\"*\" secure=\"false\" /><site-control permitted-cross-domain-policies=\"master-only\" /></cross-domain-policy>";
socket_write($client[$i]['sock'],$cdmp.chr(0));
socket_close($client[$i]['sock']);
unset($client[$i]);
$cdmp="";
}
}
}else{
//if($client[$i]['sock']!=null){
// Close the socket
//socket_close($client[$i]['sock']);
//unset($client[$i]);
//rLog("Disconnected(1) client #".$i);
//}
}
}
}
// Close the master sockets
socket_close($sock);
?>
Basically this is a functioning socket server (execute it via PHPs CLI) but to actually do something with it requires some minor modifications. One easy modification you can do, is create a command to send a message to all other clients;
...if($n=="PING"){
socket_write($client[$i]['sock'],"PONG".chr(0));
}
You can already use this script with Flash, but only locally. I will soon write how to properly prepare this server for Flash (e.g. make it send policy files) and how to use it with flash. And for those who cannot wait for another post please remember that you cannot test your ‘internet’ (so not local) connecting flash client on the same PC as your PHP server.
Aug/083
Bitmap skewing class
Finally, after two days of trial and error, I got my bitmap skewing class working. The class is based upon Flash and Math’s work, especially the algorithm for ‘triangulating’ (I know this is not the right word) distorted quadrilaterals. The class is quite complete there might be another function for simply rendering textures onto triangles instead of quadrilaterals. See the class in action below, adjust the “AA” level (I know it’s not really the same, but couldn’t think of a nicer term) to increase or decrease the level of precision. The higher the AA level the better it looks. Lower AA levels increase performance. The same counts for the noise reduction, which decreases the performance when turned on.
Download the skew.as class
The usage is fairly simple;
[sourcecode language='actionscript']import com.skew;
// First boolean is for noise reduction the second for debug mode
var sk:skew = new skew(false,false);
// For skews with a greater precision than 1
sk.AASkew(a,b,c,d,bitmap,sp,AAh,AAv);
// For skews with a precision of 1
sk.transformer(a,b,c,d,bitmap,sp,AAh,AAv);
// With a,b,c,d being arrays containing 2 dimensional points e.g. a = [10,10];
// And bitmap being a bitmapData object
// sp being a sprite or shape
// AAh and AAv respectively being the horizontal and vertical “AA” levels[/sourcecode]
The sk.transformer function is quite an increase in performance from the sk.AASkew function because it doesn’t require mapping the non distorted bitmap.
EDIT: Updated the skew class with a better variable name for the smoothing parameter and corrected the skew functions to smooth both halves. Thanks to Banana.
Aug/080
Proof of concept: Flexible 2D Particle System
Something very powerfull for creating reallistic and flexible effects is a particle system. This is a flexible and basic particle system I created about 2 months ago. Again this is just a proof of concept, currently the max. number of particles is about 400-500. But this can be greatly increased by removing the blur and replacing it with an image, this would however make it lose some of it’s flexibility. Below is a smoke demo, but the posibilities are endless, steam, fire, explosions to name a few…
Usage is as follows:
[sourcecode language='actionscript']import com.particles.particleSystem;
var scene:particleSystem = new particleSystem();
this.addChild(scene);
scene.pX = 40; // The x-position of the particle’s source
scene.pY = 50; // The y-position of the particle’s source
scene.vY = 0; // The speed on the y axis (can also be negative)
scene.vX = 4; // The speed on the x axis (can also be negative)
scene.deviation = 2; // The possible deviation from the standard speed
scene.transparency = 1; // The transparency of the particles (levels lower than 1 greatly deteriorate the performance
scene.color = 0×000000; // Colour of the particles
scene.lifetime = 5000; // Lifetime of the particles in milliseconds
scene.size = 10; // Size of the particles
scene.maxParticles = 300; // Maximum number of particles
scene.startBlur = 10; // The amount of blur
scene.bExplode = false; // Set this to true if you want to let the particles all explode at once
scene.start();[/sourcecode]
There is still quite some room for improvement, especially getting to know when to stop the particle system is a bit tricky.
Download the Particle System class
Aug/081
Proof of concept: Flash 3D engine
I tryed a few things out in AS2 before but without much succes. With AS3 however, the new OOP structure and performance increase greatly improved the possibilities for a 3D engine in flash. Now I know there are things like this (and obviously much better) out there like Papervision3D and Sandy3D, but sometimes I like to reinvent the wheel. This is just a proof of concept; performance is far from good and there is a great lack of important features. A real version might be just around the corner though…
The navigation is still somewhat ackward for the sake of simplicity;
- W,S let you move along the z-axis
- A,D let you move along the x-axis
- Numpad 2 and 8 let you move along the y-axis
- Left and right arrow keys let you rotate left and right
- Up and down arrow keys let you rotate up and down
On a side note; I used a few ‘must-have’ classes from others;
Senocular’s keyObject class – for detecting multiple keyboard presses at once
Lifeztream’s FPS class – for displaying the 3D engine performance
And of great inspiration were;
Flash and Math’s simple 3D tutorial
Kirupa’s 3D tutorial