int main(void) { TensorStructure s1(3,"ab"); TensorStructure s2(3,"b"); const Mesh m(IPoint(MV::fill,2,2)); Tensor<DataMesh> t1(s2,m,0.0); // will initialize all elements to zero Tensor<Tensor<DataMesh> > tt1(s1, t1); // will be a tensor of tensors, with all elements being zero Tensor<Tensor<DataMesh> > tt2(s1,s2, m, 0.0); // this gives the same result std::cout << "tt2(1,2)="<<tt2(1,2) <<"\n"; // will print out a Tensor<DataMesh> std::cout << "tt2(1,2)(0)="<<tt2(1,2)(0) <<"\n"; // will print out a single DatMesh // tt2(1,2) = 15; // will fail to compile tt2(1,2) = t1; // this is fine tt2(1,2)(0) = -3; // will be fine. return EXIT_SUCCESS; }
// Main int main(int argc, char *argv[]) { // Init GTK Gtk::Main kit(argc, argv); Gtk::Window window; window.set_size_request(450, 350); Gtk::Fixed fixed; window.add(fixed); // Lines //////// Lines lines; lines.set_size_request(500, 425); // FLTK lines.add_line(230, 45, 110, 115); lines.add_line(270, 45, 270, 90); lines.add_line(290, 45, 400, 165); lines.add_line(310, 40, 350, 60); // Point lines.add_line(110, 55, 110, 115); lines.add_line(150, 55, 210, 90); // Graph lines.add_line(110, 160, 90, 240); lines.add_line(110, 160, 170, 340); // Window lines.add_line(250, 135, 230, 190); lines.add_line(270, 135, 270, 265); lines.add_line(320, 135, 400, 165); // GUI lines.add_line(340, 210, 270, 265); lines.add_line(390, 210, 400, 240); // Simple lines.add_line(200, 310, 170, 340); // So difficult... >.< fixed.add(lines); // Nodes //////// TitledText tt1("Point.h", "struct Point { ... };"); fixed.put(tt1, 50, 25); TitledText tt2("Graph.h", "// graphing interface\nstruct Shape { ... };\n..."); fixed.put(tt2, 50, 100); TitledText tt3("Graph.cpp", "Graph code"); fixed.put(tt3, 25, 225); TitledText tt4("Window.h", "// window interface\nclass Window { ... };\n..."); fixed.put(tt4, 200, 75); TitledText tt5("Window.cpp", "Window code"); fixed.put(tt5, 150, 175); TitledText tt6("GUI.h", "// GUI interface\nstruct In_box { ... };\n..."); fixed.put(tt6, 325, 150); TitledText tt7("GUI.cpp", "GUI code"); fixed.put(tt7, 350, 225); TitledText tt8("Simple_window.h", "// window interface\nclass Simple_window { ... };\n..."); fixed.put(tt8, 175, 250); TitledText tt9("chapter12.cpp", "#include \"Graph.h\"\n#include \"Simple_window.h\"\nint main { ... }"); fixed.put(tt9, 75, 325); FramedText ft1("FLTK headers"); ShadowFrame<FramedText> sfa1(ft1); ShadowFrame<ShadowFrame<FramedText> > sfb1(sfa1, 2); fixed.put(sfb1, 225, 25); FramedText ft2("FLTK code"); ShadowFrame<FramedText> sfa2(ft2); ShadowFrame<ShadowFrame<FramedText> > sfb2(sfa2, 2); fixed.put(sfb2, 350, 50); // Done, run the application window.show_all_children(); Gtk::Main::run(window); return EXIT_SUCCESS; }
collisionMoveResult collisionMoveSimple(Map *map, IGameDef *gamedef, f32 pos_max_d, const aabb3f &box_0, f32 stepheight, f32 dtime, v3f &pos_f, v3f &speed_f, v3f &accel_f) { TimeTaker tt("collisionMoveSimple"); collisionMoveResult result; // If there is no speed, there are no collisions if(speed_f.getLength() == 0) return result; /* Calculate new velocity */ speed_f += accel_f * dtime; /* Collect node boxes in movement range */ std::vector<aabb3f> cboxes; std::vector<bool> is_unloaded; std::vector<bool> is_step_up; { TimeTaker tt2("collisionMoveSimple collect boxes"); v3s16 oldpos_i = floatToInt(pos_f, BS); v3s16 newpos_i = floatToInt(pos_f + speed_f * dtime, BS); s16 min_x = MYMIN(oldpos_i.X, newpos_i.X) + (box_0.MinEdge.X / BS) - 1; s16 min_y = MYMIN(oldpos_i.Y, newpos_i.Y) + (box_0.MinEdge.Y / BS) - 1; s16 min_z = MYMIN(oldpos_i.Z, newpos_i.Z) + (box_0.MinEdge.Z / BS) - 1; s16 max_x = MYMAX(oldpos_i.X, newpos_i.X) + (box_0.MaxEdge.X / BS) + 1; s16 max_y = MYMAX(oldpos_i.Y, newpos_i.Y) + (box_0.MaxEdge.Y / BS) + 1; s16 max_z = MYMAX(oldpos_i.Z, newpos_i.Z) + (box_0.MaxEdge.Z / BS) + 1; for(s16 x = min_x; x <= max_x; x++) for(s16 y = min_y; y <= max_y; y++) for(s16 z = min_z; z <= max_z; z++) { try { // Object collides into walkable nodes MapNode n = map->getNode(v3s16(x,y,z)); if(gamedef->getNodeDefManager()->get(n).walkable == false) continue; std::vector<aabb3f> nodeboxes = n.getNodeBoxes(gamedef->ndef()); for(std::vector<aabb3f>::iterator i = nodeboxes.begin(); i != nodeboxes.end(); i++) { aabb3f box = *i; box.MinEdge += v3f(x, y, z)*BS; box.MaxEdge += v3f(x, y, z)*BS; cboxes.push_back(box); is_unloaded.push_back(false); is_step_up.push_back(false); } } catch(InvalidPositionException &e) { // Collide with unloaded nodes aabb3f box = getNodeBox(v3s16(x,y,z), BS); cboxes.push_back(box); is_unloaded.push_back(true); is_step_up.push_back(false); } } } // tt2 assert(cboxes.size() == is_unloaded.size()); assert(cboxes.size() == is_step_up.size()); /* Collision detection */ /* Collision uncertainty radius Make it a bit larger than the maximum distance of movement */ f32 d = pos_max_d * 1.1; // A fairly large value in here makes moving smoother //f32 d = 0.15*BS; // This should always apply, otherwise there are glitches assert(d > pos_max_d); int loopcount = 0; while(dtime > BS*1e-10) { TimeTaker tt3("collisionMoveSimple dtime loop"); // Avoid infinite loop loopcount++; if(loopcount >= 100) { infostream<<"collisionMoveSimple: WARNING: Loop count exceeded, aborting to avoid infiniite loop"<<std::endl; dtime = 0; break; } aabb3f movingbox = box_0; movingbox.MinEdge += pos_f; movingbox.MaxEdge += pos_f; int nearest_collided = -1; f32 nearest_dtime = dtime; u32 nearest_boxindex = -1; /* Go through every nodebox, find nearest collision */ for(u32 boxindex = 0; boxindex < cboxes.size(); boxindex++) { // Ignore if already stepped up this nodebox. if(is_step_up[boxindex]) continue; // Find nearest collision of the two boxes (raytracing-like) f32 dtime_tmp; int collided = axisAlignedCollision( cboxes[boxindex], movingbox, speed_f, d, dtime_tmp); if(collided == -1 || dtime_tmp >= nearest_dtime) continue; nearest_dtime = dtime_tmp; nearest_collided = collided; nearest_boxindex = boxindex; } if(nearest_collided == -1) { // No collision with any collision box. pos_f += speed_f * dtime; dtime = 0; // Set to 0 to avoid "infinite" loop due to small FP numbers } else { // Otherwise, a collision occurred. const aabb3f& cbox = cboxes[nearest_boxindex]; // Check for stairs. bool step_up = (nearest_collided != 1) && // must not be Y direction (movingbox.MinEdge.Y < cbox.MaxEdge.Y) && (movingbox.MinEdge.Y + stepheight > cbox.MaxEdge.Y) && (!wouldCollideWithCeiling(cboxes, movingbox, cbox.MaxEdge.Y - movingbox.MinEdge.Y, d)); // Move to the point of collision and reduce dtime by nearest_dtime if(nearest_dtime < 0) { // Handle negative nearest_dtime (can be caused by the d allowance) if(!step_up) { if(nearest_collided == 0) pos_f.X += speed_f.X * nearest_dtime; if(nearest_collided == 1) pos_f.Y += speed_f.Y * nearest_dtime; if(nearest_collided == 2) pos_f.Z += speed_f.Z * nearest_dtime; } } else { pos_f += speed_f * nearest_dtime; dtime -= nearest_dtime; } // Set the speed component that caused the collision to zero if(step_up) { // Special case: Handle stairs is_step_up[nearest_boxindex] = true; } else if(nearest_collided == 0) // X { speed_f.X = 0; result.collides = true; result.collides_xz = true; } else if(nearest_collided == 1) // Y { speed_f.Y = 0; result.collides = true; } else if(nearest_collided == 2) // Z { speed_f.Z = 0; result.collides = true; result.collides_xz = true; } } } /* Final touches: Check if standing on ground, step up stairs. */ aabb3f box = box_0; box.MinEdge += pos_f; box.MaxEdge += pos_f; for(u32 boxindex = 0; boxindex < cboxes.size(); boxindex++) { const aabb3f& cbox = cboxes[boxindex]; /* See if the object is touching ground. Object touches ground if object's minimum Y is near node's maximum Y and object's X-Z-area overlaps with the node's X-Z-area. Use 0.15*BS so that it is easier to get on a node. */ if( cbox.MaxEdge.X-d > box.MinEdge.X && cbox.MinEdge.X+d < box.MaxEdge.X && cbox.MaxEdge.Z-d > box.MinEdge.Z && cbox.MinEdge.Z+d < box.MaxEdge.Z ) { if(is_step_up[boxindex]) { pos_f.Y += (cbox.MaxEdge.Y - box.MinEdge.Y); box = box_0; box.MinEdge += pos_f; box.MaxEdge += pos_f; } if(fabs(cbox.MaxEdge.Y-box.MinEdge.Y) < 0.15*BS) { result.touching_ground = true; if(is_unloaded[boxindex]) result.standing_on_unloaded = true; } } } return result; }