Created
July 8, 2017 08:59
-
-
Save sknjpn/2360ff8e7da5a44be5104ca0bb00c4ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# include <Siv3D.hpp> // OpenSiv3D v0.1.5 | |
struct Ball | |
{ | |
Ball(const Vec2& _p, const Vec2& _v) : p(_p), v(_v) {} | |
Vec2 p; | |
Vec2 v; | |
}; | |
void Main() | |
{ | |
Graphics::SetBackground(Palette::Darkviolet); | |
Window::SetTitle(L"Ideal gas"); | |
Window::Resize(1280, 720); | |
const double r = 5; | |
double wx = Window::Size().x; | |
Array<Ball> balls; | |
for (double x = r * 2; x < wx - r * 2; x += r * 12) | |
for (double y = r * 2; y < Window::Size().y - r * 2; y += r * 12) | |
balls.emplace_back(Vec2(x, y), RandomVec2(1)); | |
while (System::Update()) | |
{ | |
for (auto& b : balls) | |
{ | |
b.p += b.v; | |
for (auto& t : balls) | |
{ | |
const double length = (b.p - t.p).length(); | |
if (length != 0 && length < r * 2) | |
{ | |
const Vec2 p = (t.p - b.p).normalized(); | |
const Vec2 f = p*(b.v - t.v).dot(p); | |
t.v += f; | |
b.v -= f; | |
b.p += p*(length - r * 2) / 2.0; | |
t.p -= p*(length - r * 2) / 2.0; | |
} | |
} | |
if (b.p.x < r) { b.v.x = -b.v.x; b.p.x = r; } | |
if (b.p.y < r) { b.v.y = -b.v.y; b.p.y = r; } | |
if (b.p.x > wx - r) { b.v.x = -b.v.x; b.p.x = wx - r; } | |
if (b.p.y > Window::Size().y - r) { b.v.y = -b.v.y; b.p.y = Window::Size().y - r; } | |
} | |
if (KeyRight.pressed()) wx++; | |
if (KeyLeft.pressed()) wx--; | |
if (KeySpace.down()) for (auto& b : balls) b.v *= 2; | |
if (KeyEnter.down()) for (auto& b : balls) b.v /= 2; | |
for (const auto& b : balls) Circle(b.p, r).draw(Palette::White); | |
Rect(int(wx), 0, 16, Window::Size().y).draw(Palette::Black); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment