Sunday, April 1, 2012

Trees

One of the features of the game is expected to be the need to 'clear' land of trees before you can start digging.
I found some tree models and started to experiment (can't find a low-poly one at the moment, so using a higher-polly one at moment (without texture) that I'll eventually have to replace.




I still have to code them to fall in the right direction (the direction the dozer is facing) and get a lower-poly tree model that I can texture properly.....and the trees are actually under the surface after they fall..can't get them to disappear from the scene yet.....but....it's a start.


I might just tween the transparency of the tree until it disappears instead of melting it into the ground.
Code for this is pretty straightforward...use a ray pointing out front of the dozer to detect distance to tree....and then use some tweens (provided by Tween.js) to change rotation and translation of tree.  I ended up adding a soon-to-be-invisible cylinder under each tree to act as a 'hitbox'.  Note that in order to make them seemingly fall 'from the bottom', I made the cylinder hitbox mesh extend down through the earth and be 'attached' to the tree.  So when I rotate the 'group' object, it rotates from the middle..which is about ground level.




var matrix = new THREE.Matrix4();
        matrix.extractRotation( dozer.matrix );
        var direction = new THREE.Vector3( -1,0, 0 ); //out the front of the dozer..SHOULD have been Z axis but my model is sideways
        direction = matrix.multiplyVector3( direction );
        ray = new THREE.Ray();
        ray.direction = direction;
        ray.origin = dozer.position;
        intersects = ray.intersectObjects(trees);
        for(var i = 0;i<intersects.length;i++){
            log("pointing at tree "+intersects[i].distance);
            if(intersects[i].distance < 10 && intersects[i].object.parent.name=='unfelled'){
                log("removing tree");
                intersects[i].object.parent.name='felled'
               var fell=  new TWEEN.Tween(intersects[i].object.parent.children[0].rotation).to({
                     x: -1.5 //radians
                 }, 2000);
                 var melt=  new TWEEN.Tween(intersects[i].object.parent.children[0].position).to({
                     y: -100 //move it below the earth...far below.  Will remove it later
                 }, 10000).delay(2000) ;
               fell.chain(melt);
               fell.start();
            }
        }

No comments:

Post a Comment