-
Notifications
You must be signed in to change notification settings - Fork 0
/
IDEAS.cpp
154 lines (131 loc) · 4.02 KB
/
IDEAS.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <cmath>
#include <iostream>
namespace sf {
template<class T>
struct Vector2 {
T x{};
T y{};
Vector2& operator+=(const Vector2& other) {
x += other.x;
y += other.y;
return *this;
}
Vector2& operator-=(const Vector2& other) {
x -= other.x;
y -= other.y;
return *this;
}
Vector2& operator*=(const T rhs) {
x *= rhs;
y *= rhs;
return *this;
}
Vector2& operator/=(const T rhs) {
x /= rhs;
y /= rhs;
return *this;
}
};
template<class T>
Vector2<T> operator-(const Vector2<T>& inst) {
return {-inst.x, -inst.y};
}
template<class T>
Vector2<T> operator+(const Vector2<T>& lhs, const Vector2<T>& rhs) {
return Vector2<T>(lhs) += rhs;
}
template<class T>
Vector2<T> operator-(const Vector2<T>& lhs, const Vector2<T>& rhs) {
return Vector2<T>(lhs) -= rhs;
}
template<class T>
Vector2<T> operator*(const Vector2<T>& lhs, const T rhs) {
return Vector2<T>(lhs) *= rhs;
}
template<class T>
Vector2<T> operator*(const T lhs, const Vector2<T>& rhs) {
return rhs * lhs;
}
template<class T>
Vector2<T> operator/(const Vector2<T>& lhs, const T rhs) {
return Vector2<T>(lhs) /= rhs;
}
using Vector2f = Vector2<float>;
} // namespace sf
template <typename T>
constexpr T cross(const sf::Vector2<T>& lhs, const sf::Vector2<T>& rhs) {
return lhs.x * rhs.y - lhs.y * rhs.x;
}
template <typename T>
constexpr T dot(const sf::Vector2<T>& lhs, const sf::Vector2<T>& rhs) {
return lhs.x * rhs.x + lhs.y * rhs.y;
}
template<class T>
T lengthSquared(const sf::Vector2<T>& v) {
return dot(v, v);
}
template<class T>
T length(const sf::Vector2<T>& v) {
return std::sqrt(lengthSquared(v));
}
template<class T>
T distanceSquared(const sf::Vector2<T>& lhs, const sf::Vector2<T>& rhs) {
return lengthSquared(lhs - rhs);
}
template<class T>
T distance(const sf::Vector2<T>& lhs, const sf::Vector2<T>& rhs) {
return std::sqrt(distanceSquared(lhs, rhs));
}
template<class T>
sf::Vector2<T> normalize(const sf::Vector2<T>& v) {
return v / length(v);
}
/*
template<class T>
T toRadians(const sf::Vector2<T>& v) {
return std::atan2(v.y, v.x);
}
*/
class Rock {
public:
Rock(float radius, float mass) : m_radius(radius), m_mass(mass) {}
sf::Vector2f getPosition() const { return m_position; }
sf::Vector2f getVelocity() const { return m_velocity; }
float getRadius() const { return m_radius; }
float getMass() const { return m_mass; }
inline void applyForce(const sf::Vector2f impulse) {
m_velocity += impulse / m_mass;
}
private:
sf::Vector2f m_position;
sf::Vector2f m_velocity;
sf::Vector2f m_force;
float m_radius;
float m_mass;
};
bool has_collision(const Rock& lhs, const Rock& rhs) {
auto r2 = lhs.getRadius() + rhs.getRadius(); // (lhs.getRadius() + rhs.getRadius()) * (lhs.getRadius() + rhs.getRadius())
r2 *= r2;
return lengthSquared(lhs.getPosition() - rhs.getPosition()) <= r2;
}
void apply_force_if_collision(Rock& lhs, Rock& rhs) {
if(has_collision(lhs, rhs)) {
auto delta = lhs.getPosition() - rhs.getPosition();
auto distance_squared = lengthSquared(delta);
auto radius_sum_squared = (lhs.getRadius() + rhs.getRadius()) * (lhs.getRadius() + rhs.getRadius());
if(distance_squared <= radius_sum_squared) { // objects have collided
auto collision_normal = normalize(delta);
auto relative_velocity = lhs.getVelocity() - rhs.getVelocity();
auto closing_velocity = dot(relative_velocity, collision_normal);
if(closing_velocity < 0) { // objects are moving towards each other
auto e = 1.0f; // coefficient of restitution
auto impulse_magnitude = -(1 + e) * closing_velocity * lhs.getmass() * rhs.getMass() / (lhs.getMass() + rhs.getMass());
auto impulse = impulse_magnitude * collision_normal;
lhs.applyForce(impulse);
rhs.applyForce(-impulse);
}
}
}
}
int main() {
}