-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.h
154 lines (125 loc) · 2.94 KB
/
helpers.h
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
#pragma once
#include <array>
#include <cmath>
#include <Box2D\Box2D.h>
#include <SFML/Graphics.hpp>
template<class Shape, unsigned num_extra_fixtures=0>
struct shapeDef
{
b2BodyDef bodyDef;
b2FixtureDef fixtureDef;
Shape shape;
struct shape_fixture
{
b2FixtureDef fixtureDef;
Shape shape;
shape_fixture()
{
fixtureDef.shape = &shape;
}
};
std::array<shape_fixture, num_extra_fixtures> extra_fixtures;
shapeDef()
{
fixtureDef.shape = &shape;
}
};
typedef shapeDef<b2PolygonShape> polygonDef;
typedef shapeDef<b2CircleShape> circleDef;
b2Body* make_box(b2World& world, const b2Vec2& center, const b2Vec2& extents, float angle=0, short group=0);
template<class Shape>
b2Body* make_shape(b2World& world, const shapeDef<Shape>& shape)
{
auto body = world.CreateBody(&shape.bodyDef);
body->CreateFixture(&shape.fixtureDef);
for(const auto& fixt : shape.extra_fixtures)
body->CreateFixture(&fixt.fixtureDef);
return body;
}
double to_radians(double degrees);
double to_degrees(double radians);
template<class T>
T random_margin(const T& dimensions, float margin)
{
return T(
randuniform(margin, dimensions.x - margin),
randuniform(margin, dimensions.y - margin));
}
template<class T>
T random_centered(const T& center, const T& extents)
{
return T(
randcentered(center.x, extents.x),
randcentered(center.y, extents.y));
}
template<class T>
T random_centered(const T& center, float range)
{
return random_centered(center, T(range, range));
}
template<class T>
T random_in(const T& corner1, const T& corner2)
{
return T(
randuniform(std::min(corner1.x, corner2.x), std::max(corner1.x, corner2.x)),
randuniform(std::min(corner1.y, corner2.y), std::max(corner1.y, corner2.y)));
}
//VECTORS
inline float length(sf::Vector2f vec)
{
return std::sqrt((vec.x * vec.x) + (vec.y * vec.y));
}
inline sf::Vector2f rot90(sf::Vector2f vec)
{
return sf::Vector2f(-vec.y, vec.x);
}
inline sf::Vector2f to_length(sf::Vector2f vec, float len)
{
float factor = len / length(vec);
return vec * factor;
}
template<class T>
struct range
{
T min;
T max;
range(T min=0, T max=0):
min(min),
max(max)
{}
range(const range& cpy):
min(cpy.min),
max(cpy.max)
{}
T diff() const { return max - min; };
T clamp(T input)
{
return std::min(std::max(input, min), max);
}
};
template<typename Input, typename Output>
struct linear_scale
{
range<Input> input_range;
range<Output> output_range;
linear_scale(
Input input_min,
Input input_max,
Output output_min,
Output output_max)
:input_range(input_min, input_max)
,output_range(output_min, output_max)
{}
linear_scale(range<Input> input_range, range<Output> output_range)
:input_range(input_range)
,output_range(output_range)
{}
linear_scale()
{}
Output operator()(Input input) const
{
return output_range.min == output_range.max
? output_range.min
: static_cast<Output>((((input - input_range.min) * output_range.diff()) / input_range.diff()) + output_range.min);
}
};