From 3efbd47dc5ebdab79de90552769af03ea935f50c Mon Sep 17 00:00:00 2001 From: Tiandians Date: Tue, 13 Jul 2021 14:47:08 +0800 Subject: [PATCH] experiment --- OpenGLProject/Collision.cpp | 55 ++++++++++---- OpenGLProject/Collision.h | 8 +- OpenGLProject/Object.cpp | 77 +++++++++----------- OpenGLProject/Object.h | 7 +- OpenGLProject/OpenGLProject.vcxproj | 18 +---- OpenGLProject/OpenGLProject.vcxproj.filters | 42 +---------- OpenGLProject/gen_balls | Bin 20064 -> 0 bytes OpenGLProject/gen_gas.cpp | 19 +++-- OpenGLProject/main.cpp | 70 +++++++++++++----- OpenGLProject/main_test.cpp | 20 +++-- OpenGLProject/test.txt | 61 ---------------- essay.md | 12 ++- 12 files changed, 176 insertions(+), 213 deletions(-) delete mode 100644 OpenGLProject/gen_balls delete mode 100644 OpenGLProject/test.txt diff --git a/OpenGLProject/Collision.cpp b/OpenGLProject/Collision.cpp index 62ed321..e869fd9 100644 --- a/OpenGLProject/Collision.cpp +++ b/OpenGLProject/Collision.cpp @@ -78,23 +78,32 @@ void CollisionSystem::run(const float t) { for (auto j = i + 1; j != balls.cend(); j++) { - sumexam++; + //sumexam++; if ((**i).examine(**j)) { //std::cout << "log:###ball bounce\t当前系统时间" << currentTime <<"\ndetail:\t" << (**i).num() << '\t' << (**j).num() << std::endl;// (**i).bounce(**j); - sumbounce++; + //sumbounce++; } } if (!walls.empty()) for (auto j = walls.cbegin(); j != walls.cend(); j++) { - sumexam++; + //sumexam++; if ((**i).examine(**j)) { - std::cout << "log:###wall bounce\t当前系统时间" << currentTime << "\ndetail:\t" << (**i).num() << '\t' << (**j).num() << std::endl; // (**i).bounce(**j); - sumbounce++; + //sumbounce++; + } + } + if(!containers.empty()) + for (auto j = containers.cbegin(); j != containers.cend(); j++) + { + //sumexam++; + if ((**i).examine(**j)) + { + (**i).bounce(**j); + //sumbounce++; } } } @@ -112,9 +121,9 @@ void CollisionSystem::run(const float t) }//逻辑:第一次进入必定有效,不检测。循环尾清除后必定有效,不检测。因此这个模块可以删除*/ //std::cout << "log:事件队列处理前" << eventQueue;// move(eventQueue.top().t() - currentTime); //有效,跳转到事件发生时间 - Event tempEvent = eventQueue.top(); //提出放置,因为需要检测对应物体 + tempEvent = eventQueue.top(); //提出放置,因为需要检测对应物体 eventQueue.pop(); - sumbounce++; // + //sumbounce++; // //std::cout << "当前系统时间:" << currentTime << std::endl;// tempEvent.handle(); //处理事件,处理时小球自动递增计数器 @@ -125,11 +134,11 @@ void CollisionSystem::run(const float t) if (!hidden_balls.empty()) for (auto const &i : hidden_balls) if ((temp = tempEvent.ball->predict(*i)) >= 0) //predict内置防重复 - eventQueue.push(Event(tempEvent.ball, i, temp + currentTime)); + eventQueue.push(Event(tempEvent.ball, i, temp + currentTime));/* if (!walls.empty()) for (auto const &i : walls) if ((temp = tempEvent.ball->predict(*i)) >= 0) //平面无需防重复 - eventQueue.push(Event(tempEvent.ball, i, temp + currentTime)); + eventQueue.push(Event(tempEvent.ball, i, temp + currentTime));/**/ if (!containers.empty()) for (auto const &i : containers) if ((temp = tempEvent.ball->predict(*i)) >= 0) //容器无需防重复 @@ -145,11 +154,11 @@ void CollisionSystem::run(const float t) if (!hidden_balls.empty()) for (auto const &i : hidden_balls) if ((temp = tempball->predict(*i)) >= 0) //predict内置防重复 - eventQueue.push(Event(tempball, i, temp + currentTime)); + eventQueue.push(Event(tempball, i, temp + currentTime));/*/ if (!walls.empty()) for (auto const &i : walls) if ((temp = tempball->predict(*i)) >= 0) - eventQueue.push(Event(tempball, i, temp + currentTime)); + eventQueue.push(Event(tempball, i, temp + currentTime));//*/ if (!containers.empty()) for (auto const &i : containers) if ((temp = tempball->predict(*i)) >= 0) @@ -168,6 +177,7 @@ void CollisionSystem::run(const float t) flag = -1; }*/ #endif + //std::cout << "当前系统时间:" << currentTime << std::endl;// } void CollisionSystem::reverse() @@ -203,18 +213,35 @@ void CollisionSystem::init() eventQueue.push(Event(*i, *j, temp + currentTime)); } if (!hidden_balls.empty()) - for (auto const &j : hidden_balls) + for (auto const& j : hidden_balls) if ((temp = (**i).predict(*j)) >= 0) eventQueue.push(Event(*i, j, temp + currentTime)); if (!walls.empty()) - for (auto const &j : walls) + for (auto const& j : walls) if ((temp = (**i).predict(*j)) >= 0) eventQueue.push(Event(*i, j, temp + currentTime)); if (!containers.empty()) - for (auto const &j : containers) + for (auto const& j : containers) if ((temp = (**i).predict(*j)) >= 0) eventQueue.push(Event(*i, j, temp + currentTime)); } + if(!hidden_balls.empty()) + for (auto i = hidden_balls.cbegin(); i != hidden_balls.cend(); i++) + { + for (auto j = i + 1; j != hidden_balls.cend(); j++) + { + if (((temp = (**i).predict(**j)) >= 0)) + eventQueue.push(Event(*i, *j, temp + currentTime)); + } + if (!walls.empty()) + for (auto const& j : walls) + if ((temp = (**i).predict(*j)) >= 0) + eventQueue.push(Event(*i, j, temp + currentTime)); + if (!containers.empty()) + for (auto const& j : containers) + if ((temp = (**i).predict(*j)) >= 0) + eventQueue.push(Event(*i, j, temp + currentTime)); + } std::cout << "log:当前使用事件驱动方式:" << eventQueue.size() << std::endl; //std::cout << eventQueue << std::endl; #endif diff --git a/OpenGLProject/Collision.h b/OpenGLProject/Collision.h index ab66ec2..768ed19 100644 --- a/OpenGLProject/Collision.h +++ b/OpenGLProject/Collision.h @@ -13,7 +13,7 @@ extern int sumbounce; extern int sumexam; #ifdef TIME_DRIVEN -constexpr auto DELTATIME = 1.0f / 360.0f; //时间驱动专用 +constexpr auto DELTATIME = 1.0f / 120.0f; //时间驱动专用 #endif #ifdef EVENT_DRIVEN @@ -42,6 +42,7 @@ class Event public: //construct Event(std::shared_ptr b, std::shared_ptr o, const float t) : ball(b), object(o), time(t), countBall(b->cnt()), countObject(o->cnt()) {} + Event() = default; //copy move destruct Event(const Event &) = default; @@ -97,7 +98,9 @@ class CollisionSystem std::vector> &hb() { return hidden_balls; } std::vector> &w() { return walls; } std::vector> &c() { return containers; } - std::priority_queue> &e() { return eventQueue; } +#ifdef EVENT_DRIVEN + std::priority_queue>& e() { return eventQueue; } +#endif // EVENT_DRIVEN private: void move(float); @@ -105,6 +108,7 @@ class CollisionSystem #ifdef EVENT_DRIVEN std::shared_ptr tempball; //temp:副小球检测 + Event tempEvent; #endif std::vector> balls, hidden_balls; std::vector> walls; diff --git a/OpenGLProject/Object.cpp b/OpenGLProject/Object.cpp index d5a0804..953ef2e 100755 --- a/OpenGLProject/Object.cpp +++ b/OpenGLProject/Object.cpp @@ -8,11 +8,15 @@ #include //definition - unsigned int Wall::sum = 0; unsigned int Ball::sum = 0; unsigned int Container::sum = 0; +//temp +glm::vec3 dp, dv; +float p_n, v_n, a, b, c, delta, x1, x2, temp; +float v10, v20, v1, v2; + //tools--------------------------------------------------------------------------------------------- std::ostream &operator<<(std::ostream &os, const glm::vec3 &v) { @@ -32,7 +36,7 @@ std::istream &operator>>(std::istream &is, glm::vec3 &v) float positive_min(float a, float b, float c) { - float temp = INFINITY; + temp = INFINITY; if ((a > 0) && (a < temp)) temp = a; if ((b > 0) && (b < temp)) @@ -86,25 +90,15 @@ Ball::Ball(std::istream &is) : Ball() float Ball::predict(const Wall &wall) //tochk { - std::cout << "predict:墙体" << this->number << "|" << wall.num() << std::endl; // - glm::vec3 p = location - wall.loc(); - float p_n = glm::dot(p, wall.norm()); //球到平面的垂直距离 - float v_n = glm::dot(velocity, wall.norm()); //速度在平面法向量方向上的分量 + //std::cout << "predict:墙体" << this->number << "|" << wall.num() << std::endl; // + dp = location - wall.loc(); + p_n = glm::dot(dp, wall.norm()); //球到平面的垂直距离 + v_n = glm::dot(velocity, wall.norm()); //速度在平面法向量方向上的分量 if (v_n * p_n >= 0) return -1.0f; - std::cout << "pass" << std::endl; + //std::cout << "pass" << std::endl; return -p_n / v_n; - //another solution - /* - glm::vec3 r = location - wall.loc(); - glm::vec3 chk_nor = (glm::dot(r, wall.norm())) > 0 ? wall.norm() : -wall.norm(); //选择一个背向球的法向量 - float v_l = glm::dot(velocity, chk_nor);//速度在平面法向量方向上的分量 - if (v_1 < 0) - return -1.0f; //背向运动 - float r_l = glm::dot(r, chk_nor); //球到平面的垂直距离 - return r_l / v_l; - /*/ } float Ball::predict(const Ball &ball) //tochk @@ -114,24 +108,24 @@ float Ball::predict(const Ball &ball) //tochk if (number == ball.number) //防止自预测 return -1.0f; - glm::vec3 dp = location - ball.location, //以ball为中心 - dv = velocity - ball.velocity; - if (this->back(ball)) return -1.0; + dp = location - ball.location; //以ball为中心 + dv = velocity - ball.velocity; + //std::cout << "predict:计算信息 dv dp r1 r2\n" << dv << dp << this->radius << '\t' << ball.radius << std::endl;// - float a = square(glm::length(dv)); - float b = 2.0 * glm::dot(dv, dp); - float c = square(glm::length(dp)) - square(radius + ball.radius); - float delta = square(b) - 4.0 * a * c; + a = square(glm::length(dv)); + b = 2.0f * glm::dot(dv, dp); + c = square(glm::length(dp)) - square(radius + ball.radius); + delta = square(b) - 4.0f * a * c; if (delta < 0.0) return -1.0; - float x1 = ((-b + std::sqrt(delta)) / (2.0 * a)); - float x2 = ((-b - std::sqrt(delta)) / (2.0 * a)); + x1 = ((-b + std::sqrt(delta)) / (2.0f * a)); + x2 = ((-b - std::sqrt(delta)) / (2.0f * a)); if (x2 < 0 || x1 < 0) //事实上,这里小球已经相交 return -1.0; @@ -220,18 +214,16 @@ void Ball::bounce(Ball &ball) //if (this->back(ball)) // return; - glm::vec3 r = glm::normalize(location - ball.location); + dp = glm::normalize(location - ball.location); - float v10 = glm::dot(r, velocity), - v20 = glm::dot(r, ball.velocity), - m1 = mass, - m2 = ball.mass; + v10 = glm::dot(dp, velocity); + v20 = glm::dot(dp, ball.velocity); - float v1 = ((m1 - m2) * v10 + 2 * m2 * v20) / (m1 + m2), - v2 = ((m2 - m1) * v20 + 2 * m1 * v10) / (m1 + m2); + v1 = ((mass - ball.mass) * v10 + 2 * ball.mass * v20) / (mass + ball.mass); + v2 = ((ball.mass - mass) * v20 + 2 * mass * v10) / (mass + ball.mass); - velocity += (v1 - v10) * r; - ball.velocity += (v2 - v20) * r; + velocity += (v1 - v10) * dp; + ball.velocity += (v2 - v20) * dp; count++; ball.count++; //std::cout << "info:碰撞处理后\n主小球:\t" << *this << "\n副小球:\t" << ball << std::endl;// @@ -308,17 +300,18 @@ bool Ball::examine(const Wall &wall) bool Ball::examine(const Container &container) //tochk { - return ((location.x + radius < container.x_p()) && (location.x - radius > container.x_n())) && ((location.y + radius < container.y_p()) && (location.y - radius > container.y_n())) && ((location.z + radius < container.z_p()) && (location.z - radius > container.z_n())); + return ((location.x + radius > container.x_p()) + || (location.x - radius < container.x_n())) + || ((location.y + radius > container.y_p()) + || (location.y - radius < container.y_n())) + || ((location.z + radius > container.z_p()) + || (location.z - radius < container.z_n())); } bool Ball::back(const Ball &ball) { - if ((glm::dot(-(location - ball.location), velocity) < 0) && (glm::dot(location - ball.location, ball.velocity) < 0)) - { - //std::cout << "error:背向运动" << std::endl; - return true; - } - return false; + return ((glm::dot(-(location - ball.location), velocity) < 0) + && (glm::dot(location - ball.location, ball.velocity) < 0)); } //io------------------------------------------------------------------------------------------------------------ diff --git a/OpenGLProject/Object.h b/OpenGLProject/Object.h index 0b8bc38..f732c9c 100755 --- a/OpenGLProject/Object.h +++ b/OpenGLProject/Object.h @@ -24,6 +24,8 @@ enum class Object_type std::ostream &operator<<(std::ostream &, const glm::vec3 &); std::istream &operator>>(std::istream &, glm::vec3 &); + + class Object //protected:location { public: @@ -101,8 +103,8 @@ class Ball final : public Object //velocity,mass ~Ball() = default; //information + glm::vec3 v() const { return velocity; } float r() const { return radius; } - glm::vec3 vel() const { return velocity; } float m() const { return mass; } float ek() const { return 0.5f * mass * square(glm::length(velocity)); } unsigned int cnt() const { return count; } @@ -128,9 +130,10 @@ class Ball final : public Object //velocity,mass bool examine(const Wall &); bool examine(const Ball &); bool examine(const Container &); - bool back(const Ball &); + private: + bool back(const Ball&); glm::vec3 velocity; float mass, radius; static unsigned int sum; //extern diff --git a/OpenGLProject/OpenGLProject.vcxproj b/OpenGLProject/OpenGLProject.vcxproj index 6433cc1..7b71338 100755 --- a/OpenGLProject/OpenGLProject.vcxproj +++ b/OpenGLProject/OpenGLProject.vcxproj @@ -20,29 +20,13 @@ - + - - - - - - - - - - - - - - - - 16.0 Win32Proj diff --git a/OpenGLProject/OpenGLProject.vcxproj.filters b/OpenGLProject/OpenGLProject.vcxproj.filters index 0bb3dd9..63e931d 100755 --- a/OpenGLProject/OpenGLProject.vcxproj.filters +++ b/OpenGLProject/OpenGLProject.vcxproj.filters @@ -21,13 +21,7 @@ 源文件 - - 源文件 - - - 源文件 - - + 源文件 @@ -39,38 +33,4 @@ 头文件 - - - 源文件 - - - - - 源文件 - - - 源文件 - - - 源文件 - - - 源文件 - - - 源文件 - - - 源文件 - - - 源文件 - - - 源文件 - - - 源文件 - - \ No newline at end of file diff --git a/OpenGLProject/gen_balls b/OpenGLProject/gen_balls deleted file mode 100644 index b6ca86720e572775c89a87f4b2fc6ac2be1d73d8..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 20064 zcmeHPe{@^beZR6|Vt&{%Z9+|;M1i&^fm*Q>$2f3O zT0}Lzy<)LA3-~-qGx>fKfNQ2>6~(Yh(cOTgS4Wu=@M4oJ7;+5>l3uyga<0i?C~8fd z^eQQWM zSuo`CJ_9}S%jqWW=5AG9xqiqge=?;0rnG!Gy0y7!`Ea;?I2uon){nL{*Ecu$QVHJ* z$)|cz8PxsPZs-#*lO-B=C*>C?ocNDE_vM9uzwP;d{L~wl)ZTE}i;>v}G#}fbGD(IK z>8mP9obqIcWX$&8CFw+?bTsmO%*>Vk{szb&D#3ab220VuU55UfGW3};_VqIIFO;EE z>r2(Ap^SW88M?a+{lPN!7lZD_$C(b6u~UT(DOFF31+UWx@Ll+nlD`^sFFwxnuOO6? z{{!lCsZ*G6#A(V1hpxDSVzXK3`d};(*HcC?Y3RDp+j~3pa3mQSjHZl8vbS^Ha3UV* z4Q?Hdn7q=Q8+(l5RC|xn*cciLCUqkjj2fx-P$19^+RClLR5YX~Q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100644 --- a/OpenGLProject/gen_gas.cpp +++ b/OpenGLProject/gen_gas.cpp @@ -4,8 +4,8 @@ using namespace std; -#define NUM 25//边个数 -#define DISTANCE 2//间距 +#define NUM 16//边个数 +#define DISTANCE 0.5//间距 #define MASS 1//质量 #define RADIUS 0.5//半径 #define MEAN 0//均值 @@ -15,15 +15,22 @@ int main() { ofstream ofstrm("testdata.txt"); std::default_random_engine e; //引擎 - std::normal_distribution n(MEAN,DELTA); + std::normal_distribution n(MEAN,DELTA); + e.seed(time(0)); + double origin = -(RADIUS + DISTANCE / 2.0) * (NUM - 1); + double distance = 2 * RADIUS + DISTANCE; + ofstrm << "C 1" << endl + << 0 << '\t' << 0 << '\t' << 0 << endl + << 1.5 * (NUM + 1) << '\t' << 1.5 * (NUM + 1) << '\t' << 1.5 * (NUM + 1) << endl; + ofstrm << "B " << NUM * NUM * NUM << endl; for (int i = 0; i != NUM; i++) for (int j = 0; j != NUM; j++) for (int k = 0; k != NUM; k++) { ofstrm - << i * DISTANCE + DISTANCE / 2.0 << '\t' - << j * DISTANCE + DISTANCE / 2.0 << '\t' - << k * DISTANCE + DISTANCE / 2.0 << '\t' + << origin + i * distance << '\t' + << origin + j * distance << '\t' + << origin + k * distance << '\t' << n(e) << '\t' << n(e) << '\t' << n(e) << '\t' diff --git a/OpenGLProject/main.cpp b/OpenGLProject/main.cpp index ad7a6dd..13330ff 100755 --- a/OpenGLProject/main.cpp +++ b/OpenGLProject/main.cpp @@ -1,18 +1,21 @@ -//################ +//#define OPENGL_CLOSE +//################ //头文件 //################ //OpenGL +#ifndef OPENGL_CLOSE #include #include -#include -#include -#include -#include +//#include +#include "Utils.h" +#include "Sphere.h" +#endif #include #include // glm::value_ptr #include // glm::translate, glm::rotate, glm::scale, glm::perspective -#include "Sphere.h" -#include "Utils.h" +#include +#include +#include using namespace std; //自选用的头文件 #include @@ -31,6 +34,10 @@ using namespace chrono; //################ //预分配变量 //################ +float aspect, rot_v, amt = 0.0f; +glm::vec3 cameraLoc, lookAt; + +#ifndef OPENGL_CLOSE //OpenGL数据 #define numVAOs 1 GLuint vao[numVAOs]; @@ -47,8 +54,6 @@ float lightPos[3]; glm::vec3 currentLightPos, transformed; // 窗口、视角、位置、变换 int width, height; -float aspect, rot_v, amt = 0.0f; -glm::vec3 cameraLoc, lookAt; glm::mat4 pMat, mMat, mvMat, lMat, invTrMat, rMat, vMat; //OpenGL材质和模型 // white light @@ -63,10 +68,12 @@ float *matSpe = Utils::goldSpecular(); float matShi = Utils::goldShininess(); // 球体模型 Sphere mySphere = Sphere(48); +#endif //################ //小工具函数 //################ +#ifndef OPENGL_CLOSE float toRadians(float degrees) { return (degrees * 2.0f * 3.14159f) / 360.0f; } glm::mat4 buildRotate(glm::vec3 vectorBefore, glm::vec3 vectorAfter) @@ -238,7 +245,6 @@ void setupVert_container(std::vector> const &containe glBindBuffer(GL_ARRAY_BUFFER, containerVbo[2]); glBufferData(GL_ARRAY_BUFFER, scale.size() * sizeof(float), &scale[0], GL_STATIC_DRAW); - cout << "log:OpenGL初始化成功container" << endl; } //############ @@ -403,7 +409,7 @@ void draw_container(vector> const &containers) void display(GLFWwindow *window, double currentTime, CollisionSystem &system) { glClear(GL_DEPTH_BUFFER_BIT); - glClearColor(0.0, 0.0, 0.0, 1.0); + glClearColor(1.0, 1.0, 1.0, 1.0); glClear(GL_COLOR_BUFFER_BIT); amt += rot_v; //配置旋转 @@ -418,6 +424,7 @@ void display(GLFWwindow *window, double currentTime, CollisionSystem &system) draw_container(system.c()); //cout << "OpenGL render success" << endl;// } +#endif //############ //主程序 @@ -425,6 +432,7 @@ void display(GLFWwindow *window, double currentTime, CollisionSystem &system) int main(void) { //OpenGL初始化 +#ifndef OPENGL_CLOSE if (!glfwInit()) exit(EXIT_FAILURE); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); @@ -435,30 +443,43 @@ int main(void) exit(EXIT_FAILURE); glfwSwapInterval(1); //垂直同步 glfwSetWindowSizeCallback(window, window_size_callback); +#endif //输入输出 //cout << "请输入您的数据文件名(其它路径记得转义):"; //string file; //cin >> file; //freopen("event_out.txt", "w", stdout); - ifstream ifstrm("E:\\Coding\\testdata\\605ball.txt"); + ifstream ifstrm("testdata.txt"); //创建碰撞系统 - ifstrm >> cameraLoc >> lookAt >> rot_v; + //ifstrm >> cameraLoc >> lookAt >> rot_v; + cameraLoc = glm::vec3(10, 10, 5); + lookAt = glm::vec3(0.0f); + rot_v = 0.05f; CollisionSystem system(ifstrm); + //ofstream ofstrm("out.txt"); //cout << system; - init(window, system); //提供system的相关信息为OpenGL绘制预先存储数据 +#ifndef OPENGL_CLOSE + init(window, system); //提供system的相关信息为OpenGL绘制预先存储数据 +#endif //*帧率 auto last = system_clock::now(); auto current = system_clock::now(); auto duration = duration_cast(current - last); unsigned int count = 0;//*/ + //程序主循环 +#ifndef OPENGL_CLOSE while (!glfwWindowShouldClose(window)) +#endif +#ifdef OPENGL_CLOSE + while(1) +#endif { - //*/帧率 + /*帧率 if (count++ == 30) { last = current; current = system_clock::now(); @@ -466,7 +487,7 @@ int main(void) cout << "fps::" << 30.0/(double(duration.count()) * microseconds::period::num / microseconds::period::den) << endl - << "bounce persecond::" + << "每秒碰撞::" << sumbounce / (double(duration.count()) * microseconds::period::num / microseconds::period::den) //<< endl //<< "exam persecond::" @@ -474,21 +495,30 @@ int main(void) << endl; count = 0; sumbounce = 0; + cout << "系统动能:" << system.ek() << endl; + //cout << "事件队列长度:" << system.e().size() << endl; + cout << "当前系统时间:" << system.time() << std::endl;// //sumexam = 0; }//*/ - system.run(1.0f/6); - //cout << system.ek()< 30) + //break; //显示 + #ifndef OPENGL_CLOSE display(window, glfwGetTime(), system); glfwSwapBuffers(window); glfwPollEvents(); + #endif } - //cout << system; + //ofstrm << system; +#ifndef OPENGL_CLOSE glfwDestroyWindow(window); glfwTerminate(); +#endif exit(EXIT_SUCCESS); } \ No newline at end of file diff --git a/OpenGLProject/main_test.cpp b/OpenGLProject/main_test.cpp index e8d9977..5eac07a 100644 --- a/OpenGLProject/main_test.cpp +++ b/OpenGLProject/main_test.cpp @@ -2,21 +2,23 @@ //#include //#include //#include -#include +//#include #include #include +#include //#include //#include // glm::value_ptr //#include // glm::translate, glm::rotate, glm::scale, glm::perspective //#include "Sphere.h" //#include "Utils.h" using namespace std; +using namespace chrono; //自己选用的头文件 -#include +//#include //#include //#include -//#include + //#include //#include //#include @@ -25,10 +27,14 @@ using namespace std; int main() { -// ifstream ifstrm("E:\\Coding\\testdata\\test.txt"); + ifstream ifstrm("testdata.txt"); + - std::vector> balls; - balls.push_back(std::make_shared()); - cout << balls; + CollisionSystem system(ifstrm); + auto start = system_clock::now(); + system.run(1.0f); + auto end = system_clock::now(); + auto duration = duration_cast(end - start); + cout << duration.count(); return 0; } \ No newline at end of file diff --git a/OpenGLProject/test.txt b/OpenGLProject/test.txt deleted file mode 100644 index 7b39ac3..0000000 --- a/OpenGLProject/test.txt +++ /dev/null @@ -1,61 +0,0 @@ -20 20 20 -10 10 10 -0.05 -C 1 -8 8 8 17 17 17 -B 55 -0.00 0.00 0.00 0.614450262 0.293095877 0.385347905 1 0.5 -0.00 0.00 1.50 0.091479608 0.683440629 0.974326978 1 0.5 -0.00 0.00 3.00 0.298252093 0.063127826 0.290710147 1 0.5 -0.00 0.00 4.50 0.233321964 0.836288101 0.791124125 1 0.5 -0.00 0.00 6.00 0.768545936 0.456192545 0.05196028 1 0.5 -0.00 0.00 7.50 0.992870028 0.728145692 0.006612146 1 0.5 -0.00 0.00 9.00 0.146667305 0.662678273 0.066333957 1 0.5 -0.00 0.00 10.50 0.618729207 0.399256912 0.57091019 1 0.5 -0.00 0.00 12.00 0.700542333 0.097362153 0.755235734 1 0.5 -0.00 0.00 13.50 0.494208694 0.086728613 0.620207239 1 0.5 -0.00 0.00 15.00 0.937624651 0.14962105 0.870372316 1 0.5 -0.00 1.50 0.00 0.243459597 0.454503497 0.225506313 1 0.5 -0.00 1.50 1.50 0.220228194 0.114888355 0.654089112 1 0.5 -0.00 1.50 3.00 0.062488965 0.882474075 0.723845097 1 0.5 -0.00 1.50 4.50 0.722752813 0.344886689 0.120403141 1 0.5 -0.00 1.50 6.00 0.034846027 0.48812986 0.300886304 1 0.5 -0.00 1.50 7.50 0.878012169 0.27485072 0.312477649 1 0.5 -0.00 1.50 9.00 0.529514648 0.49940282 0.809978783 1 0.5 -0.00 1.50 10.50 0.628146932 0.235205438 0.220514564 1 0.5 -0.00 1.50 12.00 0.991342176 0.244453422 0.498462566 1 0.5 -0.00 1.50 13.50 0.670606443 0.286810806 0.849284342 1 0.5 -0.00 1.50 15.00 0.289989523 0.652067101 0.411320076 1 0.5 -0.00 3.00 0.00 0.236339759 0.573343715 0.499624908 1 0.5 -0.00 3.00 1.50 0.704863235 0.362137918 0.727802902 1 0.5 -0.00 3.00 3.00 0.218723006 0.273911575 0.096000102 1 0.5 -0.00 3.00 4.50 0.677766322 0.033411951 0.890245316 1 0.5 -0.00 3.00 6.00 0.73547529 0.18250573 0.855203561 1 0.5 -0.00 3.00 7.50 0.583808459 0.149931494 0.742489909 1 0.5 -0.00 3.00 9.00 0.769348298 0.812645993 0.01625774 1 0.5 -0.00 3.00 10.50 0.973419577 0.845005089 0.060481949 1 0.5 -0.00 3.00 12.00 0.410352836 0.875804913 0.621559169 1 0.5 -0.00 3.00 13.50 0.905732177 0.184995723 0.986081107 1 0.5 -0.00 3.00 15.00 0.532783454 0.765782251 0.744470191 1 0.5 -0.00 4.50 0.00 0.137545122 0.996636883 0.65130876 1 0.5 -0.00 4.50 1.50 0.595060807 0.85381273 0.162985837 1 0.5 -0.00 4.50 3.00 0.167438664 0.264435834 0.209819498 1 0.5 -0.00 4.50 4.50 0.638178864 0.727202175 0.382606606 1 0.5 -0.00 4.50 6.00 0.778748205 0.059412869 0.256575239 1 0.5 -0.00 4.50 7.50 0.868686874 0.501894633 0.488939553 1 0.5 -0.00 4.50 9.00 0.913006265 0.162889344 0.48239359 1 0.5 -0.00 4.50 10.50 0.840166216 0.207737708 0.968237187 1 0.5 -0.00 4.50 12.00 0.417207727 0.923160036 0.408208379 1 0.5 -0.00 4.50 13.50 0.207995391 0.174694051 0.875178125 1 0.5 -0.00 4.50 15.00 0.730946242 0.329307767 0.010602224 1 0.5 -0.00 6.00 0.00 0.134090279 0.534228592 0.048787746 1 0.5 -0.00 6.00 1.50 0.64113479 0.369930326 0.994821624 1 0.5 -0.00 6.00 3.00 0.5567109 0.437494706 0.671177203 1 0.5 -0.00 6.00 4.50 0.455507106 0.465068882 0.016098735 1 0.5 -0.00 6.00 6.00 0.328275534 0.751828153 0.47656799 1 0.5 -0.00 6.00 7.50 0.469734966 0.251864422 0.060947728 1 0.5 -0.00 6.00 9.00 0.910735685 0.75849124 0.582694489 1 0.5 -0.00 6.00 10.50 0.347090471 0.501512092 0.865398364 1 0.5 -0.00 6.00 12.00 0.533235589 0.765573457 0.608310863 1 0.5 -0.00 6.00 13.50 0.606444005 0.365320284 0.20191976 1 0.5 -0.00 6.00 15.00 0.743805435 0.935552992 0.781571375 1 0.5 \ No newline at end of file diff --git a/essay.md b/essay.md index 45314a4..0bae993 100644 --- a/essay.md +++ b/essay.md @@ -275,12 +275,22 @@ void Ball::bounce(Object &object) 事件驱动的步骤是:预测可能的碰撞,放入优先队列。只有系统运动到最近一次事件发生时才需要处理事件和再次预测事件。:猜想复杂度因素:球体系统密度,球体系统运动剧烈程度。 -### 4.2 算法探究2:包围盒检测 +### 4.2 算法探究2:空间区域划分 > 原命题:尝试将大片区域分割为长方形的小格,并在一种新的事件类型中仅预测某个例子在某一时刻和相邻9个方格中所有粒子的碰撞。改进CollisionSystem的simulate方法,减少了碰撞预测数量,代价是监测粒子在方格之间的运动 :这里应当使用八叉树划分空间,需要学习算法[空间划分的数据结构(四叉树/八叉树/BVH树/BSP树/k-d树) - 走看看 (zoukankan.com)](http://t.zoukankan.com/KillerAery-p-10878367.html) +首先,创建网格类,用于储存该方格内的小球,包含指向周围8个方格的指针 + +划分方法:根据小球的半径,方格的长宽高是容器长宽高除小球的直径的3倍 + +输入时让小球带上网格信息,网格堆积从下至上,网格0是哨兵,这样方便取模 + +小球移动时监测网格移动, + +碰撞检测对每 + ### 4.3 算法探究3:优先队列和索引优先队列 > 原命题:使用索引优先队列来保证优先队列的长度最多与例子数量呈线性关系,而非平方级别或者更糟