Flimsy 0.2 Release

… not to mention late.  I was making promises for this release that I didn’t keep.  I was hoping to finish some other functions, but for now all I was able to do was complete PVector.  Hence, the name of this new post being “flimsy”.  Basically, I was stuck and I didn’t follow through on Dave’s teachings “to trudge through these times”.  I could’ve kept going and found some other functions to work on, but instead I just stopped working.  This can be seen by the fact that I haven’t made a blog post in almost a month.  Well, enough about berating myself over the past.  What’s important is learning from it and using it in the future; which I will do by getting a head start on my 0.3 release.  I pushed back my camera releases to get a better handle on it and the fact that there was no easy way to test 3d features before the 0.2 release.  This was all thanks to Andor and his work.  I’ll be looking at a whole bunch of 3D functions for 0.3 and hopefully I can follow through this time.

Now for PVector… reinvented!

That’s right, it’s brand spanking new!  The images above are examples of Processing PVector along with it’s JS Counterpart.  If you wish to see the new PVector, click here.  And just so you can compare the two, click here for the old PVector.  I was also actually stuck on a problem for PVector.  It was solved the Monday/Tuesday of the 0.2 release week.  I was able to finally sit down and talk to Al (aka F1LT3R).  He was able to show me the way to get myself unstuck from my problem.

Let me, first, describe to you what was happening.  We’ll focus on the add() method of PVector.  Here’s a copy of the old PVector add method below:

p.PVector.prototype.add = function add(){

	if (arguments.length == 3){
		this.x += arguments[0];
		this.y += arguments[1];
		this.z += arguments[2];
	}
	else if (arguments.length == 1){
		this.x += arguments[0].x;
		this.y += arguments[0].y;
		this.z += arguments[0].z;
	}
	else if (arguments.length == 2){
        var a = arguments;
        return new p.PVector( a[0].x + a[1].x,
                              a[0].y + a[1].y,
                              a[0].z + a[1].z );

	}
};

Now, above is a depiction of handling the three different uses of the add method; which can be found here.  The problem I was having was the prototyping function within Javascript’s objects.  I was using prototype to link this method in a clean way to the original object.  Now, the problem occurs when using PVector.add(v1, v2).  The method simply doesn’t exist due to the nature of prototyping.  To be able to use it that way, I’d have to type PVector.prototype.add(v1, v2); which is definitely a problem because this is a port of an existing application.  The number one rule of “ports” is to make it functional in the same way as the original.

Now you would think it’d be easily solvable by removing prototype, but another problem occurs due to the breaking of another object operator called “this”.  Removing prototype breaks the link to the original object, which in turn, breaks the functionality.  It wasn’t an easy problem to solve but Al, with his JS prowess, showed me a way to make PVector a wrapper and be able to use the functional language properly.

function PVectorAdd(){
    var a = arguments;
    return p.PVector( a[0].x + a[1].x, a[0].y + a[1].y, a[0].z + a[1].z );
}

function vectorStuff(){
    arguments = arguments[0];
    this.x = arguments[ 0 ] || 0;
    this.y = arguments[ 1 ] || 0;
    this.z = arguments[ 2 ] || 0;

    this.add = function(){
        if (arguments.length == 3){
            this.x += arguments[0];
            this.y += arguments[1];
            this.z += arguments[2];
        }
        else if (arguments.length == 1){
            this.x += arguments[0].x;
            this.y += arguments[0].y;
            this.z += arguments[0].z;
        }
    };
}

p.PVector = function PVector(){
      return new vectorStuff( arguments );
}

p.PVector.add = PVectorAdd;

I realized that there was room for redundancy removal and that specific calls went through specifically to how the method was used and split some of the code.  It might be a little less cleaner looking but the necessary checks are removed and I feel it’s cleaner overall.

The following link are the tests for PVector working properly (feel free to go through them all):  click here!

Also, I’m willing to give contrib credit to anyone in the class wanting to test the object extensively and give me some feedback!  Just leave a post!