Skip to content

Instantly share code, notes, and snippets.

@voidproc
Last active September 23, 2016 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save voidproc/5703ada299c95117af42b44ba2ab3401 to your computer and use it in GitHub Desktop.
Save voidproc/5703ada299c95117af42b44ba2ab3401 to your computer and use it in GitHub Desktop.
Collision detection for Shapes
#include <Siv3D.hpp>
template <class T>
const bool intersect(const T& shape1, const Shape& shape2)
{
const Shape::Type type = shape2.type();
switch (type)
{
case Shape::Type::Circle:
return shape1.intersects(shape2.storage.circle);
case Shape::Type::RectF:
return shape1.intersects(shape2.storage.rectF);
case Shape::Type::Quad:
return shape1.intersects(*shape2.storage.quad);
case Shape::Type::Triangle:
return shape1.intersects(shape2.storage.triangle);
}
return false;
}
void Main()
{
Graphics::SetBackground(Palette::Slategray);
const Vec2 pos { 640 / 2, 480 / 2 };
Array<Shape> shapes;
shapes.push_back(Shape(Circle(pos, 50))); //Circle
shapes.push_back(Shape(RectF(pos.movedBy(-130, -130), 100, 200))); //RectF
shapes.push_back(Shape(RectF(pos.movedBy(0, -100), 120, 100).rotated(45_deg))); //Quad
shapes.push_back(Shape(Triangle(pos, pos.movedBy(140, 0), pos.movedBy(0, 120)))); //Triangle
while (System::Update())
{
for (auto& s : shapes)
{
if (intersect(Vec2(Mouse::Pos()), s))
{
s.draw(Palette::Orange);
}
s.drawFrame(3);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment