Skip to content

Instantly share code, notes, and snippets.

@sknjpn
Last active June 25, 2017 15:36
Show Gist options
  • Save sknjpn/3439cb15cb6f2dc60ffd9dea3f505b6a to your computer and use it in GitHub Desktop.
Save sknjpn/3439cb15cb6f2dc60ffd9dea3f505b6a to your computer and use it in GitHub Desktop.
# include <Siv3D.hpp>
struct Planet
{
Vec2 pos;
Vec2 v;
int id;
};
void Main()
{
Array<Planet> planets;
for (int i = 0; i < 200; i++) planets.push_back({ RandomVec2(640.0, 480.0), RandomVec2(1.0),Random(0,5) });
while (System::Update())
{
for (auto& p : planets)
{
if (p.pos.x + p.v.x >= 0 && p.pos.x + p.v.x <= 640)
{
if (p.pos.y + p.v.y >= 0 && p.pos.y + p.v.y <= 480) p.pos += p.v;
else p.v.y = -p.v.y;
}
else p.v.x = -p.v.x;
for (auto& pt : planets)
{
if ((p.pos - pt.pos).isZero()) continue;
const double length = (p.pos - pt.pos).length();
if (length < 8.0)
{
if (p.v.length() > pt.v.length()) pt.id = p.id;
else p.id = pt.id;
}
const double f = 100.0 / pow(length, 2.0) * (p.id == pt.id ? -1 : 1);
p.pos += (pt.pos - p.pos).normalized()*f;
pt.pos += (p.pos - pt.pos).normalized()*f;
}
}
for (auto& p : planets)
Circle(p.pos, 4).draw(HSV(p.id * 60));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment