Skip to content

Instantly share code, notes, and snippets.

@tokoroten
Last active January 3, 2018 21:25
Show Gist options
  • Save tokoroten/451bf11cdbb2fb8b1e2bb68a52c983ec to your computer and use it in GitHub Desktop.
Save tokoroten/451bf11cdbb2fb8b1e2bb68a52c983ec to your computer and use it in GitHub Desktop.
siv3d box2d
# include <Siv3D.hpp>
# include <HamFramework.hpp>
class Ball
{
public:
static const double SIZE;
static const Polygon POLYGONS[];
static const Color COLORS[];
const int mColor;
bool isLive;
private:
PhysicsBody mBody;
public:
Ball(PhysicsBody body, int color) :
mBody(body), mColor(color), isLive(true)
{
mBody.setDamping(1.0);
}
~Ball() {
}
void run()
{
const auto& center = Vec2(0, 0);
const auto deltaVec = center - mBody.getPos();
//const auto r = deltaVec.length();
//const auto nVec = deltaVec.normalized();
const auto f = deltaVec / 1.0;
mBody.applyLinearImpulse(f);
}
void draw() const
{
const auto color = COLORS[mColor];
const auto pos = mBody.getPos();
const auto angle = mBody.getAngle();
auto polygon = POLYGONS[mColor];
polygon = polygon.rotated(angle);
polygon.draw(pos, color);
polygon.drawFrame(pos, 0.1, Palette::White);
}
};
const double Ball::SIZE = 1.0;
const Polygon Ball::POLYGONS[] = {
Geometry2D::CreateNgon(3, Ball::SIZE),
Geometry2D::CreateNgon(4, Ball::SIZE),
Geometry2D::CreateNgon(5, Ball::SIZE),
Geometry2D::CreateNgon(16, Ball::SIZE),
Geometry2D::CreatePlus(Ball::SIZE, 0.4),
Geometry2D::CreateStar(Ball::SIZE),
};
const Color Ball::COLORS[] = {
Palette::Red,
Palette::Green,
Palette::Blue,
Palette::Yellow,
Palette::Cyan,
Palette::Magenta,
};
class BallGame
{
private:
PhysicsWorld mWorld;
Array<Ball> mBallList;
public:
BallGame() {
init();
};
~BallGame() {};
void init()
{
mWorld.setGravity({ 0,0 });
for (auto i : step(100))
{
const auto pos = RandomVec2() * 20;
const auto color = int(Random() * 6);
const auto body = mWorld.createCircle(pos, Ball::SIZE);
mBallList.push_back(Ball(body, color));
}
}
void fireCheck()
{
}
void run()
{
// input
if (Input::MouseL.clicked) {
const auto pos = Mouse::PosF();
const auto color = int(Random() * 6);
const auto body = mWorld.createCircle(pos, Ball::SIZE);
mBallList.push_back(Ball(body, color));
}
fireCheck();
// move
for (auto& ball : mBallList) {
ball.run();
}
mWorld.update();
}
void draw() const
{
for (const auto& ball : mBallList) {
ball.draw();
}
}
};
void Main()
{
//ScalableWindow::Setup({1920, 1080});
Window::Resize(1920, 1080);
CameraBox2D camera(Vec2(0, 0), 8.0);
auto game = BallGame();
game.init();
const auto font = Font(12);
while (System::Update())
{
camera.update();
{
const auto t1 = camera.createTransformer();
const auto t2 = ScalableWindow::CreateTransformer();
game.run();
game.draw();
}
camera.draw(Palette::Orange);
//draw some ui here
font(Profiler::FPS(), L"fps").draw();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment