aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/system/CollisionSystem.h
blob: cfa599e8d4c46c54e38e5e1b7d07a2d7c5b4d1de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#pragma once

#include "Collider.h"
#include "api/Rigidbody.h"
#include "api/Transform.h"
#include "api/BoxCollider.h"
#include "api/CircleCollider.h"
#include "api/Vector2.h"
#include <tuple>
#include <vector>
#include <variant>

namespace crepe {


class CollisionSystem {
private:
	using collider_stor = std::variant<BoxCollider, CircleCollider>;
private:
	struct CollidedInfoStor {
		//! Store either BoxCollider or CircleCollider
		collider_stor collider; 
		Transform& transform;
		Rigidbody& rigidbody;      // Rigidbody data
	};
public:
	struct ColliderInfo {
		const Collider& collider;
		Transform& transform;
		Rigidbody& rigidbody;
	};
	struct CollisionInfo{
    ColliderInfo first;
    ColliderInfo second;
		Vector2 move_back_value;
	};
public:
	CollisionSystem();
	void update();
private: //handling
	void call_collision_handler(CollidedInfoStor& data1,CollidedInfoStor& data2);
	void static_collision_handler(CollisionInfo& info);
private: //detection
	
	std::vector<std::pair<CollidedInfoStor,CollidedInfoStor>> check_collisions(const std::vector<std::reference_wrapper<BoxCollider>>& boxcolliders, const std::vector<std::reference_wrapper<CircleCollider>>& circlecolliders);
	bool check_box_box_collision(const BoxCollider& box1, const BoxCollider& box2, const Transform& transform1, const Transform& transform2, const Rigidbody& rigidbody1, const Rigidbody& rigidbody2);
	bool check_box_circle_collision(const BoxCollider& box1, const CircleCollider& circle2, const Transform& transform1, const Transform& transform2, const Rigidbody& rigidbody1, const Rigidbody& rigidbody2);
	bool check_circle_circle_collision(const CircleCollider& circle1, const CircleCollider& circle2, const Transform& transform1, const Transform& transform2, const Rigidbody& rigidbody1, const Rigidbody& rigidbody2);
	Vector2 current_position(const Collider& collider, const Transform& transform, const Rigidbody& rigidbody);


};

} // namespace crepe