[C++] struct in zweidimensionalem Array
-
Hi!
Ich bin an der Grenze zur Verzweiflung. Ich sitze seit einer Woche an einer Sache und komme trotzdem nicht weiter... Ich hab vorher fast zwei Jahre in Java programmiert und bin nun wegen opengl auf c++ umgestiegen.
Das Problem:
Ich benutze folgende Strukturen:typedef struct{ float x; float y; f float z; } Vector; typedef struct{ Vector position; Vector normals[4]; Vector coord[4]; float height; } Area;Ich speichere die "areas" in einem Zweidimensionalem Array:
Area world[worldi][worldj]
Ausserdem habe ich ein zweites Array, wo die areas nach gewissen transformationen gehalten werden:
Area transWorld[worldi][worldj];
Initieert wird das worldArray folgendermassen:
GLvoid InitWorld(){ InitRay(myRay); Area *tmpArea = NULL; Vector *tmpVector = NULL; for(int i = 0; i < worldi; i++){ for(int j = 0; j < worldj; j++){ tmpArea = &world[i][j]; if(map[i][j] == 1){ tmpArea->position.x = i * areaSide; tmpArea->position.y = 0.0; tmpArea->position.z = j * areaSide; tmpVector = &tmpArea->normals[0]; tmpVector->x = 0.0f; tmpVector->y = 0.0f; tmpVector->z = -1.0f; tmpVector = &tmpArea->normals[1]; tmpVector->x = 1.0; tmpVector->y = 0.0; tmpVector->z = 0.0; tmpVector = &tmpArea->normals[2]; tmpVector->x = 0.0; tmpVector->y = 0.0; tmpVector->z = 1.0; tmpVector = &tmpArea->normals[3]; tmpVector->x = -1.0; tmpVector->y = 0.0; tmpVector->z = 0.0; tmpVector = &tmpArea->coord[0]; tmpVector->x = i * areaSide; tmpVector->y = 0.0; tmpVector->z = j * areaSide; tmpVector = &tmpArea->coord[1]; tmpVector->x = i * areaSide + areaSide; tmpVector->y = 0.0; tmpVector->z = j * areaSide; tmpVector = &tmpArea->coord[2]; tmpVector->x = i * areaSide + areaSide; tmpVector->y = 0.0; tmpVector->z = j * areaSide + areaSide; tmpVector = &tmpArea->coord[3]; tmpVector->x = i * areaSide; tmpVector->y = 0.0; tmpVector->z = j * areaSide + areaSide; } } } }Vor jeder gezeichenter Frame erfolgt ein update der Welt mittels:
GLvoid updateWorldCoord(){ Area *tmpArea = NULL; Area *transArea = NULL; Vector *tmpVector = NULL; for(int i = 0; i < worldi; i++){ for(int j = 0; j < worldj; j++){ if(map[i][j] == 1){ tmpArea = &world[i][j]; transArea = &transWorld[i][j]; //tmpVector = &(tmpArea->position); transArea->position = rotateVector((tmpArea->position), (GLfloat)(360.0f - yrot), 1); transArea->position = translateVector((tmpArea->position), -xpos, 0.0, -zpos); for(int k = 0; k < 4; k++){ transArea->coord[k] = rotateVector((tmpArea->coord[k]), 360.0f - yrot, 1); transArea->coord[k] = translateVector((tmpArea->coord[k]), -xpos, 0.0, -zpos); transArea->normals[k] = rotateVector((tmpArea->normals[k]), 360.0f - yrot, 1); transArea->normals[k] = translateVector((tmpArea->normals[k]), -xpos, 0.0, -zpos); } } } } }Wenn ich nun nach dem Update die Daten zwecks collision-detection auslesen will, mit:
(...)Vector tempCoord; Vector tempNormal; Vector tempCoord2; Vector tempNormal2; for(int ic = 0; ic < worldi; ic++){ for(int jc = 0; jc <worldj; jc++){ tmpArea = transWorld[ic][jc]; for(int kc = 0; kc < 4; kc++){ tempCoord = tmpArea.coord[kc]; tempNormal = tmpArea.normals[kc]; tempCoord2 = tmpArea.coord[(kc+1)%4]; tempNormal2 = tmpArea.normals[(kc+1)%4];(...)
kommen total verwirrte Sachen raus!
Ich hab mir einen Breakpoint gesetzt und mir die Daten aus dem Debugger ausgelesen:
Wenn ich mit der Maus über ic geh' - steht da "ic = 10", wenn ich aber über jc geh' - steht da "int jc" !!!
Als nächstes geh ich über tempCoord - da steht zB {123.4, 1.2, 12.3}
im tempCoord2 steht das selbe!!!
Und das beste ist: wenn ich mir angucke was im tempArea steht - ist alles auf 0.0 gesetzt!!!
Da frag ich mich wie er dann auf die tempCoord Werte gekommen ist?!?! Wenn in der tmpArea nur 0.0 stehen?Ich dahte das hätte was mit den call-by-value und call-by-refference zu tun, aber ich habe den Code nochaml umgeschrieben, damit es davon nicht abhängt - aber es geht weiter nicht...
-
einen Fehler hab ich schon gefunden und behoben - nähmlich:
transArea->coord[k] = rotateVector((tmpArea->coord[k]), 360.0f - yrot, 1);
transArea->coord[k] = translateVector((tmpArea->coord[k]), -xpos, 0.0, -zpos);die zweite Anweisung hat die Werte der ersten überschrieben.
Das Problem mit den komischen Zahlen besteht immer noch...
Ich sehe nicht welche Anweisung mir die Daten auf Null stellen sollte.
Translate und Rotate funktionieren auf jeden Fall richtig.
Kann mir vlt. jemand sagen ob die Pointer richtig verwendet werden? Bin in diesem Thema noch nicht so richtig sicher.EDIT: LÖÖÖÖÖÖL... hab den breakpoint ausserhalb der Schleife plaziert, wo tempArea initialsiert wird... <blödman>

Collision funtzt zwar weiter nicht aber jetzt seh' ich auch die Daten zum debuggen
