Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 1, 2017, 8:40:42 PM (7 years ago)
Author:
remartin
Message:

Gebastel in spawnChildren, hat immer noch Fehler drin.

Location:
code/branches/AsteroidMining_HS17/src/modules/asteroidmining
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • code/branches/AsteroidMining_HS17/src/modules/asteroidmining/AsteroidMinable.cc

    r11609 r11615  
    4343++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    4444
    45 KNACKPUNKTE:
     45KNACKPUNKTE:
     46
     47o Floating point exception
     48o math.pi richtig einbinden.
    4649
    4750OFFEN:
     
    8790o Absturz beim Asteroidengenerieren: Health war auf 0 gesetzt! Wurde nur bei der xml-Variante definiert.
    8891o Asteroiden fressen Pickups: Argument in Pawn, Test darauf in Tick() von PickupSpawner.
     92o Man kann keine Arrays der Groesse 0 initialisieren, aber auch nicht per IF 2x definieren.
     93o i++ einfach ganz verhindern, ++i stattdessen.
     94
    8995
    9096NOTIZEN:
     
    110116#include "core/XMLPort.h"
    111117#include "core/EventIncludes.h"
    112 #include "network/NetworkFunction.h"
     118#include "network/NetworkFunction.h"
     119#include "util/Math.h"
     120
    113121
    114122// #include "infos/PlayerInfo.h"
     
    128136#include "../objects/collisionshapes/SphereCollisionShape.h"
    129137#include "../../orxonox/graphics/Model.h"
     138
    130139
    131140
     
    328337
    329338
    330 void AsteroidMinable::spawnChildren(){
     339void AsteroidMinable::spawnChildren(){// Spawn smaller Children
     340
    331341   
    332342    if (this->size <=1){return;} // Absicherung trivialer Fall
    333343
    334     // Spawn smaller Children
    335344    int massRem = this->size-1; //some mass is lost
    336     int num = round((massRem-1)*(double)rand() / (double)RAND_MAX)+1; // random number of children, at least one // Tweak towards bigger junks?
     345    int num = round(rnd()*(massRem-1)) + 1; // random number of children, at least one
    337346    if(num > 10){num = 10;} // no max function in C!
    338 
    339     // num = 1; // zum Testen mal nur einen generieren.
    340 
    341     // // TODO: Check this draft for edge cases etc.
    342     // // 'Triangular', discrete probability density with max at massRem/num
    343     // // Max at a, value 1, subintervals a+b = c
    344     // int c = massRem-num;
    345     // float probDensity = [c]
    346     // int a = round(massRem/num);
    347     // int b = c-a;
    348     // for(int z = 0; z<=a; z++){probDensity[z] = m*z/a; } // rising part
    349     // for(z = a+1; z<c; z++){probDensity[z] = m - m*(z-a)/b;} // falling part
    350 
    351     // double rand = (double)rand() / (double)RAND_MAX; // between 0 and 1
    352     // double probSum = 0;
    353     // int result = 0;
    354     // while(rand>probSum){
    355     //     probSum = probSum+probDensity[result];
    356     //     result++:
    357     // }
    358 
    359 
    360     massRem = massRem-num;   
    361 
    362     int extra = 0;
    363 
    364     //orxout() << "Number of Children: " << num << endl;
    365 
    366 
    367     for(int fisch=num; fisch>=1; --fisch){
    368         // to distribute remaining mass
    369 
    370         //orxout() << "AsteroidMining::spawnChildren(): Fisch-Variable: " << fisch << endl;
    371 
    372         if(fisch==1){
    373             extra = massRem;
    374         }else{
    375             extra = round(massRem*(double)rand() / (double)RAND_MAX);
    376             massRem = massRem-extra;
    377 
    378         }
    379 
    380         //orxout() << "Mass chosen: " << extra+1 << endl;
    381 
    382         //orxout() << "AsteroidMining::spawnChildren(): Inside for-loop." << endl;
    383 
    384         //Spawn this child  Game crashes!
    385         //AsteroidMinable* child = new AsteroidMinable(this->context);
    386         int newMass = extra+1;
    387         AsteroidMinable* child = new AsteroidMinable(this->context, newMass, this->getPosition() + Vector3(fisch*newMass*2.5, 0, 0));
    388         // if(!(child == nullptr)){//Errorgebastel
    389 
    390         // Position zu spaet gesetzt? Tick nicht atomar -> falsche Groesse evtl?
    391 
    392         // child->setSize(extra + 1);
    393                
    394         //     //OFFEN:Kollision der Kinder verhindern
    395         //     //Relativ zu Elternteil automatisch?
    396         //     //Typ position:rand()*Vektoriwas?
    397         //     //pawn->getWorldPosition() + Vector3(30,0,-30);
    398         // child->setPosition(this->getPosition() + Vector3(num*5, 0, 0));
    399         // //child->setPosition(Vector3(0,0,0));
     347    int masses[num] = {1}; // Masses of the asteroids, at least one.
     348    massRem = massRem-num;
     349
     350    orxout() << "SpawnChildren(): passed basic stuff. num = " << num << endl;
     351
     352    // Randomnised spawning points for the new asteroids
     353    float phi[num] = {0.0}; // assuming that it gets initialised to 0. Add (= {0.0})?
     354    float theta[num] = {0.0};
     355    float pi = 3.14159;//math.pi;
     356
     357    float d_p = 2*pi/num;
     358    float d_t = pi/num;
     359    float p = d_p/2;
     360    float t = d_t/2;
     361    // float phiOffset = rnd()*2*pi; // Added everywhere to become independent of the coordinate system
     362    // float thetaOffset = rnd()*pi;
     363    float rScaling = tan(t); // scale radius to prevent asteroids from touching. (distance=AsteroidRadius/tan(sector/2))
     364
     365    int pos;
     366    for(int it = 0; it<num; ++it){
     367        orxout() << "SpawnChildren(): Entering For. " << endl;
     368
     369        pos = mod((int)(rnd()*num),num);
     370        orxout() << "SpawnChildren(): Trying position: " << pos << endl;
     371
     372        while(phi[pos] != 0.0){// find empty spot in array
     373            pos = (int)mod(++pos, num);
     374            orxout() << "SpawnChildren(): Inside, testing position: " << pos << endl;
     375        }
     376        phi[pos] = p + it*d_p;// set angle there
     377
     378        pos = mod((int)(rnd()*num),num);
     379        while(theta[pos] != 0.0){
     380            pos = (int)mod(++pos, num);
     381        }
     382        theta[pos] = t + it*d_t;
     383    }
     384
     385    orxout() << "SpawnChildren(): passed angle stuff. " << endl;
     386
     387    // 'Triangular', discrete probability density with max at the expected value massRem/num at a. a+b = c
     388    if(massRem>0){ // Required to avoid array of size 0 or access problems
     389        int c = massRem;
     390        float probDensity[c] = {0.0};
     391
     392        int a = round(massRem/num);
     393        int b = c-a;
     394        int m = 2/c; // Peak value, is reached at the average size.
     395        int z = 0;
     396        for(z = 0; z<=a; ++z){probDensity[z] = m*z/a; } // rising part
     397        for(z = a+1; z<c; ++z){probDensity[z] = m - m*(z-a)/b;} // falling part
     398   
     399        // Distributing the mass
     400        for(int trav = 0; trav<num && massRem>0; ++trav){
     401
     402            int result = 0;
     403            float rVal = rnd();// between 0 and 1
     404            float probSum = probDensity[0];
     405            while(rVal>probSum){
     406                probSum = probSum+probDensity[result+1];
     407                ++result;
     408            }
     409            if(result>massRem){result = massRem;}
     410
     411            massRem = massRem-result;
     412            masses[trav] = masses[trav]+result;
     413
     414        }
     415    }
     416       
     417    orxout() << "SpawnChildren(): passed Mass stuff. " << endl;
     418
     419    for(int fisch = 0; fisch<num; ++fisch){// create the children
     420
     421        // Position offset:
     422        float r = masses[fisch]/rScaling;
     423        Vector3* pos = new Vector3(r*sin(theta[fisch])*cos(phi[fisch]), r*sin(theta[fisch])*sin(phi[fisch]), r*cos(theta[fisch]));
     424
     425        AsteroidMinable* child = new AsteroidMinable(this->context, masses[fisch], this->getPosition() + *pos);
    400426
    401427        if(child == nullptr){
     
    403429        }
    404430
    405 
    406 
    407         //orxout() << "Done: Creating new Asteroid" << endl;
    408 
    409         // }
    410 
    411 
    412                 //         Pawn* pawn = this->carrierToPawnHelper();
    413                 // if(pawn == nullptr) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed.
    414                 //     this->Pickupable::destroy();
    415 
    416                 // //Attach to pawn
    417                 // Drone* drone = new Drone(pawn->getContext()); // this is neccessary because the projectiles fired need a valid creator for the particlespawner (when colliding against something)
    418                 // drone->addTemplate(this->getDroneTemplate());
    419 
    420 
    421     }
    422     // orxout() << "Leaving spawnChildren() method. " << endl;
     431    }
     432    orxout() << "Leaving spawnChildren() method. " << endl;
    423433}
    424434
  • code/branches/AsteroidMining_HS17/src/modules/asteroidmining/SpicedAsteroidField.cc

    r11610 r11615  
    4242
    4343#include "SpicedAsteroidField.h"
     44#include "AsteroidMinable.h"
    4445
    4546#include <algorithm>
     
    5051#include "core/EventIncludes.h"
    5152#include "network/NetworkFunction.h"
     53#include "util/Math.h"
    5254
    5355// #include "infos/PlayerInfo.h"
     
    6163// #include "controllers/FormationController.h"
    6264
    63 #include "../pickup/items/HealthPickup.h"
    64 #include "../pickup/PickupSpawner.h"
    65 #include "../pickup/Pickup.h"
     65
    6666//#include "../pickup/pickup ..... pickupable
    6767#include "../objects/collisionshapes/SphereCollisionShape.h"
     
    110110        SUPER(SpicedAsteroidField, XMLPort, xmlelement, mode);
    111111        //        XMLPortParam(PickupSpawner, "pickup", setPickupTemplateName, getPickupTemplateName, xmlelement, mode);
    112         XMLPortParam(SpicedAsteroidField, "size", setSize, getSize, xmlelement, mode);
     112        XMLPortParam(SpicedAsteroidField, "count", setCount, getCount, xmlelement, mode);
     113        XMLPortParam(SpicedAsteroidField, "mDensity", setMineralDensity, getMineralDensity, xmlelement, mode);
     114        XMLPortParam(SpicedAsteroidField, "position", setPosition, getPosition, xmlelement, mode);
     115        XMLPortParam(SpicedAsteroidField, "maxSize", setMaxSize, getMaxSize, xmlelement, mode);
     116        XMLPortParam(SpicedAsteroidField, "minSize", setMinSize, getMinSize, xmlelement, mode);
     117        XMLPortParam(SpicedAsteroidField, "radius", setRadius, getRadius, xmlelement, mode);
     118        XMLPortParam(SpicedAsteroidField, "foggy", setFog, isFoggy, xmlelement, mode);
     119
    113120
    114121    }
     122
     123
     124
    115125
    116126    void SpicedAsteroidField::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
     
    151161    }
    152162
    153     void SpicedAsteroidField::setSize(float s){
    154         this->size = s;
    155     }
    156163
    157     float SpicedAsteroidField::getSize(){
    158         return this->size;
    159     }
    160164
    161165
  • code/branches/AsteroidMining_HS17/src/modules/asteroidmining/SpicedAsteroidField.h

    r11610 r11615  
    5858
    5959
     60            inline void setCount(float s){this->count = s;}
     61            inline float getCount(){return this->count;}
    6062
    61             virtual void setSize(float f);
    62             virtual float getSize();
     63            inline void setMineralDensity(float d){this->mDensity = d;}
     64            inline float getMineralDensity(){return this->mDensity;}
    6365
     66            inline void setPosition(Vector3 v){this->position = v;}
     67            inline Vector3 getPosition(){returrn this->position;}
     68
     69            inline void setMaxSize(int i){this->maxSize = i;}
     70            inline int getMaxSize(){return this->maxSize;}
     71
     72            inline void setMinSize(int i){this->minSize = i;}
     73            inline int getMinSize(){return this->minSize;}
     74
     75            inline void setRadius(float r){this->radius = r;}
     76            inline int getRadius(){return this->radius;}
     77
     78            inline void setFog(bool f){this->foggy = f;}
     79            inline bool isFoggy(){return this->foggy;}
    6480
    6581        protected:
     
    6783            //float asteroidVersion; // Bodenstrich-Konvention?,
    6884            // Wert zwischen 1 und 6 (Spezialdinger?), die Mesh-Form
    69             float size;
    70             bool generateSmaller;
     85            float count;
     86            float mDensity; // Mineral density, between 0 and 1;
     87
     88            Vector3 position;
    7189            bool initialised;
     90            Context* context;
     91            int maxSize;
     92            int minSize;
    7293
    73             Context* context;
     94            float radius;
     95            bool foggy;
    7496
    7597            virtual void create();
Note: See TracChangeset for help on using the changeset viewer.