[Orxonox-commit 6997] r11618 - code/branches/AsteroidMining_HS17/src/modules/asteroidmining

remartin at orxonox.net remartin at orxonox.net
Sun Dec 3 19:32:19 CET 2017


Author: remartin
Date: 2017-12-03 19:32:18 +0100 (Sun, 03 Dec 2017)
New Revision: 11618

Modified:
   code/branches/AsteroidMining_HS17/src/modules/asteroidmining/AsteroidMinable.cc
   code/branches/AsteroidMining_HS17/src/modules/asteroidmining/AsteroidMinable.h
Log:
Die uberkomplizierte Verteilung in SpawnChildren funktioniert recht fehlerfrei, ausser beim riesigen Asteroiden. Weiter testen. 

Modified: code/branches/AsteroidMining_HS17/src/modules/asteroidmining/AsteroidMinable.cc
===================================================================
--- code/branches/AsteroidMining_HS17/src/modules/asteroidmining/AsteroidMinable.cc	2017-12-03 17:02:22 UTC (rev 11617)
+++ code/branches/AsteroidMining_HS17/src/modules/asteroidmining/AsteroidMinable.cc	2017-12-03 18:32:18 UTC (rev 11618)
@@ -42,12 +42,23 @@
 Veraenderungstagebuch
 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
+FRAGEN:
+* Zeilen-Loeschung beim output
+* orxout()-Problem
+* dokumentieren mit @brief? 
+* Warnumg aus Asteroidenzerstoerung
+
+
 KNACKPUNKTE: 
 
-o Floating point exception
-o math.pi richtig einbinden. 
 
 OFFEN: 
+
+o Mass distribution -> weird for bigger asteroids
+--> more testing
+
+o SpicedAsteroidField fertigstellen, Mineraldichten-Parameter
+
 o Add custom pickup 'resources'. Weird template stuff -> Problems setting 'size' and other properties? setNumber of Resources. 
 --> PickupTemplateName_ in PickupSpawner ist ein string. Wird via getBaseClassIdentifier ausgelesen, daraus wird ein Pickupable fabriziert. 
 --> MunitionPickup erbt von Pickup erbt von collectiblePickup erbt von Pickupable
@@ -54,15 +65,13 @@
 ----> im MineralsPickup die isPickedUp()-Methode überschreiben? 
 --> data_extern/images/effects: PNG's für die Pickups und GUI-Dinger. 
 
+o inherit parent velocity
+o set collisionDamage? Just for static entities?
+o Dokumentieren mit @brief?
+
 o Explosion parts
 o custom HUD
-o asteroidField.lua anpassen, mit 'Mineraliendichten-Parameter'. 
 
-o Dokumentieren mit @brief?
-
-o replace ugly random stuff with math.rnd()
-o set collisionDamage? Just for static entities?
-
 HANDBUCH:
 o im Level-File includes/pickups.oxi importieren. 
 o Bei der XML-Variante wird beim ersten Aufruf der Tick-Methode ein neuer Asteroid generiert, 
@@ -91,6 +100,8 @@
 o Asteroiden fressen Pickups: Argument in Pawn, Test darauf in Tick() von PickupSpawner. 
 o Man kann keine Arrays der Groesse 0 initialisieren, aber auch nicht per IF 2x definieren. 
 o i++ einfach ganz verhindern, ++i stattdessen. 
+o Discusting mixup with array length, accessing element a[len]...
+o unitialised mass value -> obese asteroid, physics bug. 
 
 
 NOTIZEN:
@@ -162,7 +173,7 @@
         // Old from Pawn
         this->registerVariables();
 
-        orxout() << "AsteroidMining:: Pseudo-Konstruktor passiert!" << endl;
+        //orxout() << "AsteroidMining:: Pseudo-Konstruktor passiert!" << endl;
 
     }
 
@@ -180,10 +191,9 @@
 
         // Default Values
         this->generateSmaller = true; 
-        //this->setHealth(50);
         this->context = c;
-        this->size = size; // customSize
-        this->health_ = 15*size;// capped at 200 in pawn or smth?
+        this->size = size;
+        this->health_ = 15*size; 
         this->maxHealth_ = this->health_;
         this->acceptsPickups_ = false;
 
@@ -190,16 +200,12 @@
         this->setPosition(position);
         //this->roll = rand()*5; //etwas Drehung. richtige Variable
 
-        // // Fliegt davon, irgendwieso. Dies scheint auch nicht zu nuetzen. 
-        // this->setVelocity(Vector3(0,0,0));
 
-
-
         // Add Model    //<Model position="0,-40,40" yaw="90" pitch="-90" roll="0" scale="4" mesh="ast6.mesh" />
         Model* hull = new Model(this->context);
         // random one of the 6 shapes
         char meshThingy[] = "";
-        sprintf(meshThingy, "ast%.0f.mesh", (round((5*(double)rand()/(double)RAND_MAX))+1)); //    sprintf(str, "Value of Pi = %f", M_PI);
+        sprintf(meshThingy, "ast%.0f.mesh", round(5*rnd())+1); //    sprintf(str, "Value of Pi = %f", M_PI);
         hull->setMeshSource(meshThingy);
         hull->setScale(this->size);
         this->attach(hull);
@@ -214,7 +220,7 @@
 
         this->initialised=true; 
 
-        orxout() << "AsteroidMining:: Initialisierung Zweitkonstruktor abgeschlosssssen." << endl;
+        //orxout() << "AsteroidMining:: Initialisierung Zweitkonstruktor abgeschlosssssen." << endl;
 
     }
 
@@ -227,7 +233,7 @@
         // Just create a new asteroid to avoid Collision shape scale problems etc. 
         AsteroidMinable* reborn = new AsteroidMinable(this->context, this->size, this->getPosition());
         reborn->toggleShattering(true); // mainly here to avoid 'unused' warning. 
-        this->~AsteroidMinable(); // seems dangerous. Necessary for efficiency? 
+        this->~AsteroidMinable(); // seems dangerous, yields warning.  Necessary for efficiency? 
 
     }
 
@@ -248,18 +254,12 @@
 
     void AsteroidMinable::registerVariables()
     {
-        // registerVariable(this->bAlive_,            VariableDirection::ToClient);
-        // registerVariable(this->bVulnerable_,       VariableDirection::ToClient);
-        // registerVariable(this->health_,            VariableDirection::ToClient);
-        // registerVariable(this->maxHealth_,         VariableDirection::ToClient);
 
         registerVariable(this->size, VariableDirection::ToClient);
         registerVariable(this->generateSmaller, VariableDirection::ToClient);
 
         registerVariable(this->initialised, VariableDirection::ToClient);
-        //registerVariable(this->context, VariableDirection::ToClient); // can't link that since it's a context
 
-
             //         float size;
             // bool generateSmaller;
             // bool initialised;
@@ -269,15 +269,10 @@
 
     void AsteroidMinable::tick(float dt)
     {
-        if(!(this->initialised)){this->putStuff();}
+        if(!(this->initialised)){this->putStuff();} 
 
         if(this->health_ <=0){this->death();}
 
-        // if(this->initialised){
-        //     // this->setVelocity(Vector3(0,0,0));  // Funktioniert, scheint aber unsinnig. 
-        //     orxout() << "Flying at speed: " << this->getVelocity() << endl;
-        // }
-
     }
 
     void AsteroidMinable::setSize(float s){
@@ -327,9 +322,7 @@
         // orxout() << "AsteroidMining::Death(): Passed Pickup stuff!" << endl;
 
         // Smaller Parts = 'Children'
-        if(this->generateSmaller){
-            this->spawnChildren();
-        }
+        if(this->generateSmaller){this->spawnChildren();}
 
         // orxout() << "Wieder retour in death() geschafft. " << endl;
 
@@ -344,45 +337,49 @@
     int massRem = this->size-1; //some mass is lost
     int num = round(rnd()*(massRem-1)) + 1; // random number of children, at least one
     if(num > 10){num = 10;} // no max function in C!
-    int masses[num] = {1}; // Masses of the asteroids, at least one. 
-    massRem = massRem-num; 
+    int masses[num]; // Masses of the asteroids, at least one. 
+    orxout() << "SpawnChildren(): Passed basic stuff. num = " << num << "; massRem(total) = "<< massRem << endl;
 
-    orxout() << "SpawnChildren(): passed basic stuff. num = " << num << endl;
+    massRem = massRem-num; // mass is at least one, add again below. 
 
     // Randomnised spawning points for the new asteroids
     float phi[num] = {0.0}; // assuming that it gets initialised to 0. Add (= {0.0})?
     float theta[num] = {0.0};
-    float pi = 3.14159;//math.pi;
+    float piG = pi; // ist statisch oder so. 
 
-    float d_p = 2*pi/num;
-    float d_t = pi/num;
-    float p = d_p/2;
-    float t = d_t/2;
-    // float phiOffset = rnd()*2*pi; // Added everywhere to become independent of the coordinate system
+    float d_p = 2*piG/num;
+    float d_t = piG/num;
+    float p = d_p/2.0;
+    float t = d_t/2.0;
+    // float phiOffset = rnd()*2*pi; // Added everywhere to become independent of the coordinate system?
     // float thetaOffset = rnd()*pi;
-    float rScaling = tan(t); // scale radius to prevent asteroids from touching. (distance=AsteroidRadius/tan(sector/2))
+    float rScaling; // scale radius to prevent asteroids from touching. (distance=AsteroidRadius/tan(sector/2))
+    if(num == 1 ){
+        rScaling = 1; // avoid tan(90). Unused. 
+    }else{
 
-    int pos;
-    for(int it = 0; it<num; ++it){
-        orxout() << "SpawnChildren(): Entering For. " << endl;
+        rScaling = tan(t); 
 
-        pos = mod((int)(rnd()*num),num); 
-        orxout() << "SpawnChildren(): Trying position: " << pos << endl;
+        int pos; // insert at random position (linear probing) in array, to get some randomness.  
+        for(int it = 0; it<num; ++it){
 
-        while(phi[pos] != 0.0){// find empty spot in array
-            pos = (int)mod(++pos, num);
-            orxout() << "SpawnChildren(): Inside, testing position: " << pos << endl;
-        }
-        phi[pos] = p + it*d_p;// set angle there
+            pos = mod((int)(rnd()*num),num); 
+            while(phi[pos] != 0.0){// find empty spot in array
+                pos = (int)mod(++pos, num);
+            }
+            phi[pos] = p + it*d_p;// set angle there
 
-        pos = mod((int)(rnd()*num),num);
-        while(theta[pos] != 0.0){
-            pos = (int)mod(++pos, num);
+            pos = mod((int)(rnd()*num),num);
+            while(theta[pos] != 0.0){
+                pos = (int)mod(++pos, num);
+            }
+            theta[pos] = t + it*d_t;
         }
-        theta[pos] = t + it*d_t;
     }
 
-    orxout() << "SpawnChildren(): passed angle stuff. " << endl;
+    //orxout() << "SpawnChildren(): Phi: "; printArrayString(phi);
+    //orxout() << "SpawnChildren(): Theta: "; printArrayString(theta);
+    orxout() << "SpawnChildren(): Passed angle stuff. " << endl;
 
     // 'Triangular', discrete probability density with max at the expected value massRem/num at a. a+b = c
     if(massRem>0){ // Required to avoid array of size 0 or access problems
@@ -391,37 +388,60 @@
 
         int a = round(massRem/num);
         int b = c-a;
-        int m = 2/c; // Peak value, is reached at the average size. 
+        
         int z = 0;
-        for(z = 0; z<=a; ++z){probDensity[z] = m*z/a; } // rising part
-        for(z = a+1; z<c; ++z){probDensity[z] = m - m*(z-a)/b;} // falling part
+        float dProbA = 1.0/(a*a + 3.0*a + 2.0); // one 'probability unit' for discrete ramp function. Gauss stuff. 
+        for(z = 0; z<=a; ++z){probDensity[z] = (z+1)*dProbA; } // rising part
+
+        float dProbB = 1.0/(b*b +3.0*b + 2.0);
+        for(z = 0; z<b; ++z){probDensity[c-z] = (z+1)*dProbB;} // falling part
     
-        // Distributing the mass
-        for(int trav = 0; trav<num && massRem>0; ++trav){
+        // Testing
+        for(int globi = 0; globi<c; ++globi){
+            orxout() << "pDensity at [" << globi << "] is: " << probDensity[globi] << endl;
+        }
 
-            int result = 0;
-            float rVal = rnd();// between 0 and 1
-            float probSum = probDensity[0];
-            while(rVal>probSum){
-                probSum = probSum+probDensity[result+1];
+        // Distributing the mass to individual asteroids
+        int result;
+        float rVal;// between 0 and 1
+        float probSum;
+        for(int trav = 0; trav<num; ++trav){
+            result = 0;// reset
+            rVal = rnd();// between 0 and 1
+            orxout() << "Random Value picked: " << rVal << endl;
+            probSum = probDensity[0]; 
+            //orxout() << "Sum at start: " << probSum << endl;
+
+            while(rVal>probSum && result<massRem){// Not yet found && there-s smth to distribute (Incrementing once inside!)
+                if(result<(num-2)){probSum = probSum+probDensity[result+1];} // avoid logical/acess error
                 ++result;
+                //orxout() << "Sum so far: " << probSum << endl;
             }
-            if(result>massRem){result = massRem;}
 
             massRem = massRem-result;
-            masses[trav] = masses[trav]+result; 
+            masses[trav] = 1 +result; // at least one 
+            orxout() << "Mass chosen for child " << trav << " is: " << masses[trav] << endl;
 
         }
+    }else{// Everyone has mass 1. Initialising the array to 1 doesn-t seem to work. Hideous C language!
+        for(int schnaegg = 0; schnaegg<num; ++schnaegg){
+            masses[schnaegg] = 1;
+        }
     }
-        
-    orxout() << "SpawnChildren(): passed Mass stuff. " << endl;
 
-    for(int fisch = 0; fisch<num; ++fisch){// create the children
+    //orxout() << "SpawnChildren(): Masses: "; printArrayString(masses);
+    orxout() << "SpawnChildren(): Passed mass stuff. " << endl;
 
-        // Position offset:
-        float r = masses[fisch]/rScaling;
-        Vector3* pos = new Vector3(r*sin(theta[fisch])*cos(phi[fisch]), r*sin(theta[fisch])*sin(phi[fisch]), r*cos(theta[fisch]));
+    // Creating the 'chlidren':
+    for(int fisch = 0; fisch<num; ++fisch){
 
+        Vector3* pos = new Vector3(0,0,0); // Position offset
+        if(num > 1){// not required if there-s just one child
+            float r = masses[fisch]/rScaling;
+            pos = new Vector3(r*sin(theta[fisch])*cos(phi[fisch]), r*sin(theta[fisch])*sin(phi[fisch]), r*cos(theta[fisch])); // convert spheric coordinates to vector
+        }
+        
+        //orxout() << "Creating asteroid with mass " << masses[fisch] << " at relative postition " << *pos << endl;
         AsteroidMinable* child = new AsteroidMinable(this->context, masses[fisch], this->getPosition() + *pos);
 
         if(child == nullptr){
@@ -475,11 +495,59 @@
         // }
     }
 
+    void AsteroidMinable::printArrayString(float thingy[]){ // Don-t work!
 
+        orxout() << "[" ; //<< endl;  
+        char frag[] = "";
+        int len = (int)(sizeof(thingy)/sizeof(thingy[0]));
+        for(int m = 0; m< (len-2); ++m){
+            sprintf(frag, "%.5f, ", thingy[m]);
+            orxout() << frag << endl;//std::flush;
+        }
+        sprintf(frag, "%.5f]", thingy[len-1]);
+        orxout() << frag << endl; // Just print it here! No ugly passing. 
+    }
 
+    void AsteroidMinable::printArrayString(int thingy[]){
 
+        orxout() << "[" ; //<< endl; 
+        char frag[] = "";
+        int len = (int)(sizeof(thingy)/sizeof(thingy[0]));
+        for(int m = 0; m< (len-2); ++m){
+            sprintf(frag, "%.0i, ", thingy[m]);
+            orxout() << frag << endl;//std::flush;
+            printf("TEst");
+        }
 
+        sprintf(frag, "%.0i]", thingy[len-1]); // last element
+        orxout() << frag << endl; // Just print it here! No ugly passing. 
+    }
 
+
+    // void AsteroidMinable::printArrayString(int thingy[]){
+
+    //     char res[] = "[";
+    //     //strcat(res, "[");
+    //     char frag[] = "";
+
+    //     int len = (int)(sizeof(thingy)/sizeof(thingy[0]));
+    //     for(int m = 0; m< (len-1); ++m){
+    //         sprintf(frag, "%.0i, ", thingy[m]);
+    //         strcat(res, frag);
+    //     }
+    //     sprintf(frag, "%.0i]", thingy[len]);
+    //     strcat(res, frag); // last element
+
+    //     orxout() << res << endl; // Just print it here! No ugly passing. 
+
+    //     // static char result[(sizeof(res)/sizeof("")] = res; // define as static, would get deleted otherwise. 
+    //     // char *result = malloc(sizeof(res)/sizeof("") + 1); 
+    //     // *result = res;
+    //     // return result;
+    // }
+
+
+
 //Pawn:
     // void Pawn::hit(Pawn* originator, const Vector3& force, const btCollisionShape* cs, float damage, float healthdamage, float shielddamage)
     // {

Modified: code/branches/AsteroidMining_HS17/src/modules/asteroidmining/AsteroidMinable.h
===================================================================
--- code/branches/AsteroidMining_HS17/src/modules/asteroidmining/AsteroidMinable.h	2017-12-03 17:02:22 UTC (rev 11617)
+++ code/branches/AsteroidMining_HS17/src/modules/asteroidmining/AsteroidMinable.h	2017-12-03 18:32:18 UTC (rev 11618)
@@ -91,8 +91,13 @@
             void registerVariables();
             virtual void spawnChildren();
 
+            void printArrayString(float thingy[]);// Just for testing
+            void printArrayString(int thingy[]);// Just for testing
 
 
+
+
+
     }; // tolua_export
 } // tolua_export
 



More information about the Orxonox-commit mailing list