- Timestamp:
- Dec 1, 2017, 8:40:42 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/AsteroidMining_HS17/src/modules/asteroidmining/AsteroidMinable.cc
r11609 r11615 43 43 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 44 44 45 KNACKPUNKTE: 45 KNACKPUNKTE: 46 47 o Floating point exception 48 o math.pi richtig einbinden. 46 49 47 50 OFFEN: … … 87 90 o Absturz beim Asteroidengenerieren: Health war auf 0 gesetzt! Wurde nur bei der xml-Variante definiert. 88 91 o Asteroiden fressen Pickups: Argument in Pawn, Test darauf in Tick() von PickupSpawner. 92 o Man kann keine Arrays der Groesse 0 initialisieren, aber auch nicht per IF 2x definieren. 93 o i++ einfach ganz verhindern, ++i stattdessen. 94 89 95 90 96 NOTIZEN: … … 110 116 #include "core/XMLPort.h" 111 117 #include "core/EventIncludes.h" 112 #include "network/NetworkFunction.h" 118 #include "network/NetworkFunction.h" 119 #include "util/Math.h" 120 113 121 114 122 // #include "infos/PlayerInfo.h" … … 128 136 #include "../objects/collisionshapes/SphereCollisionShape.h" 129 137 #include "../../orxonox/graphics/Model.h" 138 130 139 131 140 … … 328 337 329 338 330 void AsteroidMinable::spawnChildren(){ 339 void AsteroidMinable::spawnChildren(){// Spawn smaller Children 340 331 341 332 342 if (this->size <=1){return;} // Absicherung trivialer Fall 333 343 334 // Spawn smaller Children335 344 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 337 346 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); 400 426 401 427 if(child == nullptr){ … … 403 429 } 404 430 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; 423 433 } 424 434
Note: See TracChangeset
for help on using the changeset viewer.