Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
container object and collision
Browse files Browse the repository at this point in the history
  • Loading branch information
bowling233 committed Jul 7, 2021
1 parent 3039bd9 commit b5ac9ac
Show file tree
Hide file tree
Showing 11 changed files with 288 additions and 89 deletions.
2 changes: 1 addition & 1 deletion .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
//"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64"
Expand Down
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"stdexcept": "cpp",
"streambuf": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp"
"typeinfo": "cpp",
"chrono": "cpp"
},
}
73 changes: 39 additions & 34 deletions OpenGLProject/Collision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
int sumbounce = 0;
int sumexam = 0;



//#############
//Event
//#############
Expand Down Expand Up @@ -39,6 +37,9 @@ std::ostream &operator<<(std::ostream &os, const Event &event)
case Object_type::WALL:
os << "wall";
break;
case Object_type::CONTAINER:
os << "cont";
break;
}
os << " | " << std::setw(3) << event.object->num()
<< " | " << std::setw(3) << event.countObject
Expand Down Expand Up @@ -82,10 +83,10 @@ void CollisionSystem::run(float t)
sumbounce++;
}
}
for (auto j = walls.cbegin(); j != walls.cend();j++)
for (auto j = walls.cbegin(); j != walls.cend(); j++)
{
sumexam++;
if((**i).examine(**j))
if ((**i).examine(**j))
{
std::cout << "!!!!!!!!!!!!!!!!!!!!exam passed" << std::endl;
(**i).bounce(**j);
Expand All @@ -98,31 +99,31 @@ void CollisionSystem::run(float t)
#endif

#ifdef EVENT_DRIVEN
while (!(eventQueue.empty()) && (targetTime >= eventQueue.top().time)) //队列非空、事件有效且发生
{//notice:处理事件以后记得刷新小球位置
while (!(eventQueue.empty()) && (targetTime >= eventQueue.top().time)) //队列非空、事件有效且发生
{ //notice:处理事件以后记得刷新小球位置
if (!eventQueue.top().valid())
{
eventQueue.pop();
continue;
}
move(eventQueue.top().t() - currentTime); //跳转到事件发生时间
Event temp = eventQueue.top();//提出放置,因为需要检测对应物体
eventQueue.top().handle(); //处理事件,处理时小球自动递增计数器
Event temp = eventQueue.top(); //提出放置,因为需要检测对应物体
eventQueue.top().handle(); //处理事件,处理时小球自动递增计数器
eventQueue.pop();
std::cout << currentTime << std::endl;

{ //主小球检测
for (auto const &i : balls)
if ((t = (temp.ball->predict(*i)) >= 0))//predict内置重复检测
if ((t = (temp.ball->predict(*i)) >= 0)) //predict内置重复检测
eventQueue.push(Event(temp.ball, i, t + currentTime));
for (auto const &i : walls)
if ((temp.object!=i)&&(t = (temp.ball->predict(*i)) >= 0))//智能指针重复检测
if ((temp.object != i) && (t = (temp.ball->predict(*i)) >= 0)) //智能指针重复检测
eventQueue.push(Event(temp.ball, i, t + currentTime));
}

if (temp.object->type() == Object_type::BALL) //副小球检测
{
std::shared_ptr<Ball> ball = std::dynamic_pointer_cast<Ball>(temp.object);//智能指针转换
std::shared_ptr<Ball> ball = std::dynamic_pointer_cast<Ball>(temp.object); //智能指针转换
for (auto const &i : balls)
if ((t = (ball->predict(*i)) > 0))
eventQueue.push(Event(ball, i, t + currentTime));
Expand All @@ -146,9 +147,9 @@ void CollisionSystem::reverse()
#ifdef EVENT_DRIVEN
while (!eventQueue.empty())
eventQueue.pop(); //清空
#ifdef DEBUG
OUTPUT << eventQueue;
#endif
#ifdef DEBUG
OUTPUT << eventQueue;
#endif
#endif

for (auto &i : balls)
Expand All @@ -159,9 +160,6 @@ void CollisionSystem::reverse()
#endif
}




//#############
//private
//#############
Expand All @@ -185,7 +183,7 @@ void CollisionSystem::init()
if ((t = ((**i).predict(**j)) >= 0))
eventQueue.push(Event(*i, *j, t + currentTime));
}
for (auto const& j : walls)
for (auto const &j : walls)
if ((t = ((**i).predict(*j)) >= 0))
eventQueue.push(Event(*i, j, t + currentTime));
}
Expand Down Expand Up @@ -218,26 +216,31 @@ std::istream &operator>>(std::istream &is, CollisionSystem &system)
exit(EXIT_FAILURE);
}

for (int i = 0; i != num; i++)
switch (identifier)
{
switch (identifier)
{
case 'B':
{
case 'B':
{
for (int i = 0; i != num; i++)
system.balls.push_back(std::make_shared<Ball>(is));
break;
}
case 'W':
{
break;
}
case 'W':
{
for (int i = 0; i != num; i++)
system.walls.push_back(std::make_shared<Wall>(is));
break;
}
}
break;
}
case 'C':
{
for (int i = 0; i != num; i++)
system.containers.push_back(std::make_shared<Container>(is));
break;
}
}
}
std::clog << "Success:systen read in" << std::endl;
system.init();//notice

system.init(); //notice
return is;
}

Expand All @@ -247,9 +250,11 @@ std::ostream &operator<<(std::ostream &os, CollisionSystem &system)
os << system.balls;
if (!system.walls.empty())
os << system.walls;
if (!system.containers.empty())
os << system.containers;
#ifdef EVENT_DRIVEN
if(!system.eventQueue.empty())
os << system.eventQueue;
if (!system.eventQueue.empty())
os << system.eventQueue;
#endif
return os;
}
2 changes: 2 additions & 0 deletions OpenGLProject/Collision.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,14 @@ class CollisionSystem
void reverse();
std::vector<std::shared_ptr<Ball>> &b() { return balls; }
std::vector<std::shared_ptr<Wall>> &w() { return walls; }
std::vector<std::shared_ptr<Container>> &c() { return containers; }
void move(float);

private:
void init();
std::vector<std::shared_ptr<Ball>> balls;
std::vector<std::shared_ptr<Wall>> walls;
std::vector<std::shared_ptr<Container>> containers;
float currentTime = 0;

#ifdef EVENT_DRIVEN
Expand Down
Loading

0 comments on commit b5ac9ac

Please sign in to comment.