Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Dec 2009
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Unhappy my collision detection fails!!!

    i cant get collission detection to work. i tryed but it always caused an infinite loop. heres the code.
    Code:
    <html>
    <!-- main file.-->
    <head>
    
    <title>lightning generator</title>
    
    </head>
    
    <body>
    <canvas id='world' width='500' height='500' style='border: 1px solid black; padding:0;'></canvas>
    <script type="text/javascript" src='world.js'></script>
    <script type='text/javascript' src='eC.js'></script>
    <script type='text/javascript' src='world.ground.js'></script>
    <script type='text/javascript'>
    world.bolt = {
    }
    ;
    world.bolt.paths = [];
    // main obj inits
    
    world.bolt.draw = function(){
    
       var x, y;
       for( var id = 0; id <= world.bolt.draw.num; id ++ ){
       
          world.ctx.moveTo( world.bolt.paths[id].x[world.bolt.paths[id].x.length-1],  world.bolt.paths[id].y[world.bolt.paths[id].y.length-1] );
          var mdx=Math.floor(Math.random()*world.bolt.paths[id].mdx);
          x = world.bolt.paths[id].x[world.bolt.paths[id].iters] + ((Math.random()<Math.random())?mdx:-mdx);
          y = ++ world.bolt.paths[id].iters;
          world.bolt.paths[id].x.push( x );
          world.bolt.paths[id].y.push( y );
          world.ctx.strokeStyle = world.bolt.paths[id].color;
          world.ctx.lineTo( x,  y );
          
       
       world.ctx.stroke();
       //world.ctx.beginPath();
       }   if(x%3==0){ world.ctx.fillStyle=world.skyColor;
        world.ctx.fillRect(0,0,world.w,world.h);}
    }
    world.bolt.draw.num=-1;
    world.bolt.cpath = function( x, y, mdx, srate, id, color ){
       world.bolt.paths[id] = {
       }
       ;
       world.bolt.paths[id].iters = 0;
       world.bolt.paths[id].x = [];
       world.bolt.paths[id].y = [];
       world.bolt.paths[id].x[0] = x;
       world.bolt.paths[id].y[0] = y;
       world.bolt.paths[id].mdx = mdx;
       world.bolt.paths[id].srate = srate;
       world.bolt.paths[id].color = color;
       world.bolt.draw.num += 1;
    }
    world.bolt.cpath(250,0, 5,5,0,'rgba(255,255,150,.5)');
    window.setInterval(world.bolt.draw,100)
    window.setTimeout('world.bolt.cpath(125,0,5,5,1,"rgba(255,10,0,.5)")',1000);
    </script>
    </body>
    </html>
    Code:
    //world.js
    var world = {
    skyColor:'rgba(0,0,100,.009)'  
    }
    ;
    world.canvas = document.getElementById( 'world' )
    world.canvas.cstyle = document.defaultView.getComputedStyle( world.canvas, null )
    world.w = parseInt( world.canvas.cstyle.width, 10 );
    world.h = parseInt( world.canvas.cstyle.height, 10 );
    world.ctx = world.canvas.getContext( '2d' );
    finally,
    Code:
    // world.ground.js
    
    world.ground = {
    }
    ;
    world.ground.level = [];
    
    
    world.ground.make = function( GArray, length ){
    
       world.ctx.strokeStyle = '#000000';
       world.ground.level = [];
       world.ctx.fillStyle = '#FFF';
       world.ctx.fillRect( 0, 0, world.w, world.h );
       world.ground.level = GArray;
       for ( world.ground.make.i = 0; world.ground.make.i <= length; world.ground.make.i ++ ){
          if ( GArray[world.ground.make.i + 1] === undefined ){
             GArray[world.ground.make.i + 1] = GArray[world.ground.make.i];
          }
    
          world.ctx.moveTo( world.ground.make.i, ( world.h ) );
          world.ctx.lineTo( world.ground.make.i, ( world.h - GArray[world.ground.make.i] ) );
       }
       world.ctx.stroke();
       world.ctx.beginPath();
    }
    ;
    
    world.ground.create = function( func, width ){
       func = ec( func );
       if( func === false ){
          alert( 'Pass equation as a string without the "y=", "f(x)=", "g(x)=" or similar.' );
          return world;
       }
       if( func == 'perspicaciousness' ){
          return world;
       }
       var temp = [];
       for( var x = 0; x <= width; x ++ ){
          temp[x] = eval( func );
       }
       world.ground.make( temp, width );
    }
    ;
    world.ground.create.test='4';
    world.ground.create( world.ground.create.test, 500 );

    in the end of world.bolt.cpath,there should be a way to use the local varis X and Y, and world.ground.js's world.ground.level[] to detect it. sorry for the gigantic length. thanks in advanced. the jsnerd

  • #2
    New Coder
    Join Date
    Dec 2009
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts
    nvm.i found the error in my old one. i h8 infinite loops...
    Open source. DO IT!!! SUPPORT IT!!! PREACH IT!!!
    -- The JSNerd


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •