bool Game::Run()
{
long long lastTickCount = Core::GetTickCount();
float dt = 0.0f;
Draw();
while (!done)
{
InputCheck();
Draw();//TT
long long tickCount = Core::GetTickCount();
long long dTickCount = tickCount - lastTickCount;
dt = static_cast<float>(dTickCount) / 1000.0f;;
//FPS = static_cast<size_t>(1.0f / dt);
dt *= timeScale;
if (!pause) Update(dt);
lastTickCount = Core::GetTickCount();
}
return true;
}
void Game::Draw()
{
render->beginDraw();
//render->drawSphere(Vector3f(0.0f, 0.0f, 0.0f), 50.0, Quaternion(0, Vector3f(0.0f, 0.0f, 1.0f)), Color4f(1.0f, 1.0f, 1.0f, 1.0f));
for (size_t i = 0; i < numEntites; i++)
{
if (Entityes[i]->isBall()) render->drawSphere(Entityes[i]->pos, dynamic_cast<Ball*>(Entityes[i])->r, Entityes[i]->color);
else if (Entityes[i]->isModel()) dynamic_cast<ModelOBJ*>(Entityes[i])->Draw(render);
}
if (drawDebugInfo)
{
}
render->endDraw();
}
void Game::Update(float dt)
{
std::cout << dt << std::endl;
for (size_t i = 0; i < numEntites; i++) Entityes[i]->init();
if (Collision)
{
for (size_t i = 0; i < numEntites; i++)
for (size_t j = i + 1; j < numEntites; j++)
{
if (Entityes[i]->isBall() && Entityes[j]->isBall())
{
Vector3f raxis = Entityes[i]->pos - Entityes[j]->pos;
float dr = raxis.Length();
float r = (dynamic_cast<Ball*>(Entityes[i])->r + dynamic_cast<Ball*>(Entityes[j])->r);
if (dr <= r)
{
std::cout << "Collision " << i << " vs " << j << ". Vel Before: " << Entityes[i]->vel << " vs " << Entityes[j]->vel << "m: " << Entityes[i]->m << " m: " << Entityes[j]->m << std::endl;
if (i == 0)
{
std::cout << "!Black Hole Collision " << std::endl;
}
if (InElasticImpact(*Entityes[i], *Entityes[j]))
{
numEntites--;
Entityes[i]->m += Entityes[j]->m;
std::cout << "m after impact: " << Entityes[i]->m << ". Vel After: " << Entityes[i]->vel << std::endl;
Entityes.erase(Entityes.begin() + j);
}
else
{
std::cerr << "Erorr: collision with not a balls!" << std::endl;
}
//ElasticImpact(*Entityes[i], *Entityes[j], dt);
}
}
}
}
if (GraviForce)
{
for (size_t i = 0; i < numEntites; i++) Entityes[i]->init();
for (size_t i = 0; i < numEntites; i++)
for (size_t j = 0; j < numEntites; j++)
if (i != j)
{
float r2 = (Entityes[i]->pos - Entityes[j]->pos).lenght2();
float f = G * Entityes[i]->m * Entityes[j]->m / r2;
Vector3f force = (Entityes[j]->pos - Entityes[i]->pos).unit() * f;
Entityes[i]->applyForce(force);
Entityes[j]->applyForce(-force);
}
}
for (size_t i = 0; i < numEntites; i++)
{
Entityes[i]->simulate(dt);
}
for (size_t i = 0; i < numEntites; i++)
if (!Entityes[i]->moved)
{
Entityes[i]->move(dt);
}
}
Я для отладки выводил в кажом вызове dt в cout.
Спасибо за помощь)