Dienstag, 17. September 2013

working on a vector based collision system. yesterday i did the prototyping for by using maya (which works). in my focus of things is a 3D tool way the best canvas to scetch up a mathematical procedere.

soon the LSD user can switch between a depthmap or a vector based collision method.
each procedere got his own positive impact.

the prototype collision procedere (maya mel):

// prototype collision response ball based on vectors
// dirk hochegger (c) 2013 
// fikken und mathe passt nicht zam.

proc vector[] ballCOLL_create(int $segCNT,float $rot){
    vector $pos_[];

    for($i=0;$i<$segCNT;$i++){
        float $rotation = (3.1459265/180*$rot);    
    
        float $x = sin(($i*(3.1459265/$segCNT)*2)+$rotation);         
        float $y = cos(($i*(3.1459265/$segCNT)*2)+$rotation); 

        $pos_[$i] = <<$x,$y,0>>;       
       
        polyCube -n "collPNTS0";
        xform -a -t $x $y 0;
        scale -a -xyz .2 .2 .2;
    }
    select -r "collPNTS*";
    group -n "collPOINTS";
    return $pos_;
}

proc groundCOLL_create(vector $pos,float $rot,float $size){

    curve -d 1 -p 0 0 0 -p 0 0 0 -k 0 -k 1 -n "line0";
    curve -d 1 -p 0 0 0 -p 0 0 0 -k 0 -k 1 -n "vec0";
    curve -d 1 -p 0 0 0 -p 0 0 0 -k 0 -k 1 -n "vec1";
    curve -d 1 -p 0 0 0 -p 0 0 0 -k 0 -k 1 -n "vec2";
    
    polyCube -n "groundCOLL0";
    scale -a -xyz .2 .2 .2;
    
    polyCube -n "groundCOLL1";
    scale -a -xyz .2 .2 .2;
    
    float $rotation = (3.1459265/180*($rot+45));
    
    float $hexPI = (3.1459265/6);
    
    float $testX0 = ((sin($rotation))+(cos($rotation)))*$size; 
    float $testY0 = ((cos($rotation))-(sin($rotation)))*$size; 
    float $testX1 = (-((sin($rotation))+(cos($rotation)))*$size);  
    float $testY1 = (-((cos($rotation))-(sin($rotation)))*$size); 
    
    $testX0 += $pos.x; 
    $testX1 += $pos.x;
    $testY0 += $pos.y;
    $testY1 += $pos.y;
    
    xform -a -t $testX0 $testY0 0 groundCOLL0;
    xform -a -t $testX1 $testY1 0 groundCOLL1;
    
    xform -a -t $testX0 $testY0 0 line0.cv[0];
    xform -a -t $testX1 $testY1 0 line0.cv[1]; 
}

proc vector[] resetBALL(vector $ballPOS_pnts_[],int $cnt){
    float $rX = -0;//3.6717299
    float $rY = 10;
    vector $newPOS_[];
    for($i=0;$i<$cnt;$i++){
        string $collPNT = "collPNTS"+$i;
        $test = $ballPOS_pnts_[$i];
        float $nwX = $test.x+$rX;
        float $nwY = $test.y+$rY;
        xform -a -t $nwX $nwY 0 $collPNT;
        $newPOS_[$i] = <<$nwX,$nwY,0>>;
    }
    return $newPOS_;
    
}

proc animate(vector $ballPOS_pnts_[], int $len,int $cnt){
    float $stepSIZE = 30.0/$len;
    float $fallY = 0;
    int $collACTIVE = 0;
    for($i=0;$i<$len;$i++){
        if($collACTIVE == 0){for($ii=0;$ii<$cnt;$ii++){
            string $collPNT = "collPNTS"+$ii;
            $pos = $ballPOS_pnts_[$ii];
            float $newX = $pos.x + $fallY;
            float $newY = $pos.y - $fallY;
                
            xform -a -t $newX $newY 0 $collPNT;
                
            vector $newPOS = <<$newX,$newY,0>>;
            $collACTIVE = checkCOLL($newPOS);
            if($collACTIVE == 1){$i = 1000;}
            }
            $fallY += $stepSIZE;
            currentTime $i;  
        }
    }
}

proc vector checkCOLL(vector $bcp){

    vector $gcp0 = `xform -q -a -t "groundCOLL0"`;
    vector $gcp1 = `xform -q -a -t "groundCOLL1"`; 
    
    vector $worldX = <<1,0,0>>;
    vector $worldY = <<0,1,0>>;
    vector $worldZ = <<0,0,-1>>;
    
    //// Z collision
    vector $vgc0 = $gcp1 - $gcp0;
    vector $vgc1 = $gcp1 - $worldZ;
    
    xform -a -t ($gcp1.x) ($gcp1.y) 0 vec0.cv[0]; 
    xform -a -t ($vgc1.x) ($vgc1.y) ($vgc1.z) vec0.cv[1]; 
    
    vector $vbc0 = $bcp - $gcp1;
    vector $vbc1 = $gcp1 - $worldZ;

    vector $nrmlGY0 = cross($vgc0,$vgc1);    
    vector $nrmlBY0 = cross($vbc0,$vgc0);
    
    float $lenY0 = dot($nrmlBY0, $nrmlGY0);
    
    $nrmlGY0 = <<(-.5+$nrmlGY0.x)*2.0,(-.5+$nrmlGY0.y)*2.0,0>>;
    $nrmlBY0 = <<(-.5+$nrmlBY0.x)*2.0,(-.5+$nrmlBY0.y)*2.0,0>>;
    
    normalize($nrmlGY0);
    normalize($nrmlBY0);
    
    xform -a -t (-$nrmlGY0.x) (-$nrmlGY0.y) 0 vec1.cv[0]; 
    xform -a -t ($gcp1.x) ($gcp1.y) 0 vec1.cv[1]; 
    
    xform -a -t (-$nrmlBY0.x) (-$nrmlBY0.y) 0 vec2.cv[0]; 
    xform -a -t ($bcp.x) ($bcp.y) 0 vec2.cv[1]; 

    
    //// X collision 0 
    $vgc0 = $gcp1 - $gcp0;
    $vbc0 = $bcp - $gcp1;

    vector $nrmlGX0 = cross($vgc0,$worldY);    
    vector $nrmlBX0 = cross($vbc0,$worldY);

    float $lenX0 = dot($nrmlBX0, $nrmlGX0);
    //// X collision1 
    $vgc0 = $gcp1 - $gcp0;
    $vbc0 = $bcp - $gcp0;

    vector $nrmlGX0 = cross($vgc0,$worldY);    
    vector $nrmlBX0 = cross($vbc0,$worldY);

    float $lenX1 = dot($nrmlBX0, $nrmlGX0);
    ////
    
    int $insideY = 0;
    int $insideX = 0;

    if($lenY0 >= 0){ $insideY = 1;}
    if(($lenX0 <= 0)&&($lenX1 >= 0)){$insideX = 1;}
    
    if(($insideY == 1)&&($insideX == 1)){return 1;}else{return 0;} 
}

proc reset(){
    select -r "collPO*";
    delete;
    select -r "ground*";
    delete;
    select -r "line*";
    delete;
    select -r "vec*";
    delete;
}

proc startUP(){
    reset();
    currentTime 0;
    vector $ballPOS_pnts_[];
    int $segCNT = 16;
    $ballPOS_pnts_= ballCOLL_create($segCNT,10);
    groundCOLL_create(<<0,3,0>>,-45,5);
    $ballPOS_pnts_= resetBALL($ballPOS_pnts_,$segCNT);
    animate($ballPOS_pnts_,50,$segCNT);
}

startUP();

stay tuned..
greetz
dirk

ps: GLE (graz lost everything)

Keine Kommentare:

Kommentar veröffentlichen