Image to PhysicsBody - Siv3D/Reference-JP GitHub Wiki

# include <Siv3D.hpp>
# include <HamFramework.hpp>

void Main()
{
	CameraBox2D camera(Vec2(0, 0), 17.0);

	PhysicsWorld world;
	auto ground = world.createLineString(Vec2(0, 0), { Vec2(-20, 20), Vec2(-20, 10), Vec2(20, 0), Vec2(20, 20) }, none, none, PhysicsBodyType::Static);
	Array<PhysicsBody> bodies;

	Image image(640, 480, Color(0, 0));
	DynamicTexture texture(image);

	while (System::Update())
	{
		if (Input::MouseL.pressed)
		{
			const Point from = Input::MouseL.clicked ? Mouse::Pos() : Mouse::PreviousPos();

			Line(from, Mouse::Pos()).overwrite(image, 8, Palette::Orange);

			texture.fill(image);
		}
		else if (Input::MouseL.released)
		{
			Polygon polygon = Imaging::FindExternalContour(image.flipped(), true).simplified(5);

			// 非常に小さい Polygon があるとエラーになるのではじく
			if (!polygon.isEmpty && polygon.area() > 5.0)
			{
				bodies.push_back(world.createPolygon(Vec2(0, 25), polygon.moveBy(-320, -240).scale(0.015)));
			}

			image.fill(Color(0, 0));

			texture.fill(image);
		}

		world.update();
		camera.update();
		{
			const auto t1 = camera.createTransformer();

			ground.draw();

			int32 i = 0;

			for (const auto& body : bodies)
			{
				body.draw(HSV(i++ * 70));
			}
		}
		camera.draw(Palette::Orange);

		texture.draw();
	}
}
⚠️ **GitHub.com Fallback** ⚠️