Example #1
0
	Polygon::Polygon(std::vector<Vec2> v) 
	{
		mType = PolygonType;
		for (std::vector<Vec2>::iterator iter = v.begin(); iter != v.end(); iter++) {
			vertices.push_back(*iter);
		}

		axes.reserve(vertices.size());
		for (unsigned i = 0; i < getVertsNum(); i++) {
			// get the current vertex
			Vec2 p1 = vertices[i];
			// get the next vertex
			Vec2 p2 = vertices[i + 1 == getVertsNum() ? 0 : i + 1];
			// subtract thw two to get the edge vector
			Vec2 edge = Subtract(p1, p2);
			// get either perpendicular vector
			Vec2 normal = edge.Perpendicular();
			Normalize(normal);
			axes.emplace_back(normal);
		}
	}
Example #2
0
	void PolygonCollider::Init(std::vector<Vec2>& vec)
	{
		mType = PolygonType;

		FirstPoint(vec);
		PointAngle(vec);
		PointSort(vec, vertices);
		PointSearch(vec, vertices);

		Vec2 normal{};
		Vec2 u = vertices[1] - vertices[0];
		Vec2 v = vertices[2] - vertices[0];
		normal = Cross(u, v);
		Normalize(normal);
		for (unsigned i = 0; i < vertices.size(); i++)
		{
			vertices[i].nx = normal.x;
			vertices[i].ny = normal.y;
			vertices[i].nz = normal.z;
		}

		COPY_TO_VERTEXBUFFER();

		axes.reserve(getVertsNum());
		for (unsigned i = 0; i < getVertsNum(); i++) {
			// get the current vertex
			Vec2 p1 = vertices[i];
			// get the next vertex
			Vec2 p2 = vertices[i + 1 == getVertsNum() ? 0 : i + 1];
			// subtract thw two to get the edge vector
			Vec2 edge = Subtract(p1, p2);
			// get either perpendicular vector
			Vec2 normal = edge.Perpendicular();
			Normalize(normal);
			axes.emplace_back(normal);
		}
	}
Example #3
0
Direction GetRotationDirection(Vec2 heading, Vec2 target) {
    Vec2 perp = heading.Perpendicular();
    target.Normalize();
    return perp.Dot(target) >= 0.0 ? Direction::Right : Direction::Left;
}