Faster Javascript loops

Consider this very standard construct:

//assume arr is an array
for (var i = 0, i < arr.length; i++)
{
...
}

This is extemely inefficient because the "length" property needs to be looked up on each pass through the loop. One easy fix is to first define a variable for the length, like this:

var l = arr.length;
for (var i = 0; i < l; i++)

This is an improvement, but this particular loop is so simple that it could be further simplified into a while loop, which will run much faster again. Since we don't really care whether we go through the array from its beginning to end or vice versa:

var i = arr.length;
while (i--)
{
...
}

This little tip applies to Javascript in general but will be highly effective with DXStudio scripting. Also, I cannot take credit for this discovery. I read it on Sitepoint.

Leave a Comment

Debug Print [with no lingering effects]

DXStudio has implemented Javascript as its control language. Of course there are a few departures from the pure ECMA Javascript implementation. Many are to directly control the 2D and 3D assets in your project. A few were added to aid in development.

The “print()” command is extremely useful. If you have properly enabled it in your Document Properties (see image), the ‘console’ can display useful debug information.

DX Document Properties

DX Document Properties


In the scripting code you can reveal the value of variables (and objects) by sending output to the console with the “print()” command. The trouble comes when you wish to release your work to the public. Even if you are testing or sharing with a small group of fellow developers, numerous print commands can produce a very annoying result by displaying lots of superfluous information. The console obscures part of the display, often times when you are trying to SEE something important.
Removing, or adding remarks to disable, the many “print()” commands is tedious and troublesome. Remembering where they all exist can be a tremendous task in itself on a large project that includes many scenes and/or objects with script in them.
By substituting the internal “print()” command in DXStudio with a simple function, you can toggle the debug display by modifying ONE value in ONE place in your code! Here’s how to do it.
In the Document script add the lines shown. The DEBUG constant is the key to this. Changing it will affect all instances of a console print command at once.

Document Script commands

Document Script commands

Then anywhere in your scripts that you wish to output DEBUG information to the console display.

Example of DEBUG output - in place of a simple 'print()' command

Example of DEBUG output - in place of a simple 'print()' command

But, whenever you wish to suppress the DEBUG output, simply change the value of the constant “DEBUG” to false!

There are many enhancements that could be made to this simple idea. Even an independent class could be created that adds some other useful “debug” operations. I will leave those enhancements up to the creative and adventuresome reader. But PLEASE send me your favorite changes and they will appear here in an update to this post.

Leave a Comment

Handy Conversion Functions

Here is a small set of handy utilities from David Shute.

// ******************************************************************* 

function vecToStr(vect, dp)
{
    //returns the given vector as a string in the form "(x, y, z)"
    //Also accepts an optional parameter representing the number of
    //decimal places to display the results to (int) 

    if (dp != null)
        return "(" + vect.x.toFixed(dp) + ", " + vect.y.toFixed(dp) + ", "
                                                               + vect.z.toFixed(dp) + ")";
    else
        return "(" + vect.x + ", " + vect.y + ", " + vect.z + ")";
} 

// ******************************************* 

function rotToStr(rot, dp)
{
    //returns the given rotation as a string in the quaternion form "(x, y, z, w)"
    //Also accepts an optional parameter representing the number of
    //decimal places to display the results to (int).
    //To create a string using the Euler values, please use rotToStrEuler() 

    if (isNaN(dp))
        return "(" + rot.x + ", " + rot.y + ", " + rot.z + ", " + rot.w + ")";
    else
        return "(" + rot.x.toFixed(dp) + ", " + rot.y.toFixed(dp) + ", "
                          + rot.z.toFixed(dp)  + ", " + rot.w.toFixed(dp)+ ")";
}     

// ******************************************* 

function rotToStrEuler(rot, dp)
{
    //returns the given rotation as a string in the Euler form "(x, y, z)"
    //Also accepts an optional parameter representing the number of
    //decimal places to display the results to (int).
    //To create a string using the quaternion values, please use rotToStr() 

    if (dp != null)
        return "(" + rot.eulerX.toFixed(dp) + ", " + rot.eulerY.toFixed(dp)
                                                   + ", " + rot.eulerZ.toFixed(dp) + ")";
    else
        return "(" + rot.eulerX + ", " + rot.eulerY + ", " + rot.eulerZ + ")";
}     

// *******************************************

function clamp(value, min, max)
{
    //returns a number representing the given value clamped
    //between the specified high and low bounds. 

    return Math.min(Math.max(value, min), max);
} 

// *******************************************


Special thanks to David for sharing these useful tidbits.

Leave a Comment

3ds Max to DXStudio Perfect scale export

There has been a lot of discussion and experimentation surrounding the conversion factor from 3DS Max to DXStudio.
Here is a visual tutorial that solves the problem.

3DS Max 1:1 Export

3DS Max 1:1 Export


In our case we using the .DXMesh exporter with a ratio of 1 Max unit to 10 centimeters.
Exporting under these conditions provides a 1:1 import into DXStudio.

Leave a Comment

3D Computer Game of Life

The standard by which most computer languages are measured by the simplest of functions, “Hello World”.

In the 1960’s a mathematician name John Horton Conway devised a computer simulation to mimic the essence of Life.  Three simple rules control the health or demise of each ‘cell’.

As the power and capabilities of modern computers grew, this became another staple of software comparison.

DXStudio provides incredible tools to create any 3D world.  This version of Life takes the simple rules and extends them  into the 3rd dimension.

Life takes place on a grid.  Each cell is evaluated to live or die by these three simple rules:

  • If a cell has one or no living neighbours, it will die of loneliness
  • If it has too many neighbours – four or more – it will die from overcrowding
  • New cells are “born” whenever an empty square has exactly three living neighbors

Play 3D Life now


Designer Notes

Leave a Comment

Aman ‘Rah – Mystery of Egypt

The people of Ancient Egypt have a reputation for devising very complex puzzles to keep would-be robbers out of their elaborate tombs.  Afterall, the Pharoh must be kept safe until his meeting with the gods!

I created Aman ‘Rah  in that vein.  You are faced with a complex array of heiroglyphs.  The key to unlocking the secret (a high score) lies in how cleverly you combine matching glyphs.

Aman Rah - Tile Matching Game

Aman ‘Rah is a game of matching. You must match two tiles that share either a Glyph or Color (or both) by clicking on one and then the other with the mouse. The amount of score you receive is related to what type of match you make:

  • Match Color and Glyph – Maximum
  • Match only Glyph or only Color

The four tiles across the bottom are called the tray.  Using a tile from the tray for a match incurs a penalty to your score.

The other tiles are the board. Notice they are arranged in a pyramid. You cannot access a tile unless it is completely uncovered. It requires skill and careful planning to successfully match all the tiles on the board. There is a bonus awarded for clearing the board.

Can you achieve a high score and protect the King?

Play Aman ‘Rah now.


Designer Notes

I have been developing software for more than 30 years. DXStudio provides wonderful GUI tools for creating 3D (and 2D) environments. But, as a software developer, I created Aman ‘Rah entirely from code.

Leave a Comment

The best 3D Game Engine

Yes.  I am prejudiced.  And perhaps you don’t really care about my opinion.  That’s fine.  Stop reading.

Good.  You are (at least a little) interested.  Or maybe just curious.

I spent many months doing what I believe was diligent research, in search of the best 3D game development system.  There are many choices.  And the list grows everyday.  When considering all features, and the price, there is no match for DXStudio.

Some other systems, with comparable features, lack decent support.  The DXStudio community is outstanding.

The feature list grows almost every week.  The support group is unrivalled.

(to be continued)

Leave a Comment