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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263  #include <stdio.h>
#include <math.h>
typedef float r32;
typedef unsigned int u32;
typedef struct vec2_t {
r32 x, y;
} vec2_t;
r32 v2_dot( vec2_t a, vec2_t b ) {
r32 result = a.x * b.x + a.y * b.y;
return result;
}
vec2_t v2_perp( vec2_t a ) {
vec2_t result;
result.x = a.y;
result.y = a.x;
return result;
}
vec2_t v2_normalized( vec2_t a ) {
r32 length = sqrtf( a.x * a.x + a.y * a.y );
a.x = a.x / length;
a.y = a.y / length;
return a;
}
#define min( a, b ) ( ( ( a ) < ( b ) ) ? ( a ) : ( b ) )
#define max( a, b ) ( ( ( a ) > ( b ) ) ? ( a ) : ( b ) )
#define swap( a, b, type ) { type __backup__ = ( a ); ( a ) = ( b ); ( b ) = __backup__; }
typedef struct sat_result_t {
vec2_t axis;
r32 magnitude;
r32 direction;
} sat_result_t;
sat_result_t sat( vec2_t* a, vec2_t* b ) {
sat_result_t result = { 0 };
result.magnitude = 1000000.0f;
int stop = 0;
vec2_t axis[ 8 ];
for ( int i = 0; i < 4; i++ ) {
vec2_t va = { a[ i + 1 ].x  a[ i ].x, a[ i + 1 ].y  a[ i ].y };
vec2_t vb = { b[ i + 1 ].x  b[ i ].x, b[ i + 1 ].y  b[ i ].y };
axis[ i ] = v2_normalized( v2_perp( va ) );
axis[ 4 + i ] = v2_normalized( v2_perp( vb ) );
}
for ( int axis_index = 0; axis_index < 8; axis_index++ ) {
r32 a_min = 1000000.0f;
r32 a_max = a_min;
r32 b_min = 1000000.0f;
r32 b_max = b_min;
for ( int point_index = 0; point_index < 4; point_index++ ) {
r32 a_dot = v2_dot( axis[ axis_index ], a[ point_index ] );
r32 b_dot = v2_dot( axis[ axis_index ], b[ point_index ] );
a_min = min( a_min, a_dot );
a_max = max( a_max, a_dot );
b_min = min( b_min, b_dot );
b_max = max( b_max, b_dot );
}
r32 overlap_max = min( a_max, b_max );
r32 overlap_min = max( a_min, b_min );
r32 overlap = overlap_max  overlap_min;
if ( overlap <= 0 ) {
result.axis.x = 0;
result.axis.y = 0;
result.magnitude = 0;
result.direction = 0;
break;
} else if ( overlap < result.magnitude ) {
result.axis = axis[ axis_index ];
result.magnitude = overlap;
result.direction = 1;
if ( a_min < b_min ) {
result.direciton = 1;
}
}
}
return result;
}
#define width 512
#define height 512
u32 buffer[ width * height * 4 ] = { 0 };
void draw_line( vec2_t p1, vec2_t p2, u32 color ) {
r32 dx = fabsf( p2.x  p1.x );
r32 dy = fabsf( p2.y  p1.y );
if ( dx >= dy ) {
if ( p1.x > p2.x ) {
swap( p1, p2, vec2_t );
}
int sign = 1;
if ( p1.y > p2.y ) {
sign = 1;
}
r32 increment = dy / dx;
u32 x = ( u32 ) p1.x;
u32 x_end = ( u32 ) p2.x;
u32 y = ( u32 ) p1.y;
r32 error = 0.5f;
while ( x <= x_end ) {
buffer[ y * width + x ] = color;
error += increment;
if ( error >= 1.0f ) {
y = ( u32 ) ( y + sign );
error = 1.0f;
}
x++;
}
} else {
if ( p1.y > p2.y ) {
swap( p1, p2, vec2_t );
}
int sign = 1;
if ( p1.x > p2.x ) {
sign = 1;
}
r32 increment = dx / dy;
u32 y = ( u32 ) p1.y;
u32 y_end = ( u32 ) p2.y;
u32 x = ( u32 ) p1.x;
r32 error = 0.5f;
while ( y <= y_end ) {
buffer[ y * width + x ] = color;
error += increment;
if ( error >= 1.0f ) {
x = ( u32 ) ( x + sign );
error = 1.0f;
}
y++;
}
}
}
int main( int argc, char** argv ) {
unsigned char tga_header[ 18 ] = {
0,
0,
2,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0, 0x2,
0, 0x2,
32,
8,
};
vec2_t a_pos = { 255.0f, 255.0f };
vec2_t b_pos = { 350.0f, 290.0f };
for ( int step = 0; step < 150; step++ ) {
vec2_t a[ 5 ] = {
a_pos.x + 0.0f, a_pos.y  50.0f,
a_pos.x + 50.0f, a_pos.y + 0.0f,
a_pos.x + 0.0f, a_pos.y + 50.0f,
a_pos.x + 50.0f, a_pos.y + 0.0f,
};
a[ 4 ] = a[ 0 ];
vec2_t b[ 5 ] = {
b_pos.x + 0.0f, b_pos.y  50.0f,
b_pos.x + 50.0f, b_pos.y + 0.0f,
b_pos.x + 0.0f, b_pos.y + 50.0f,
b_pos.x + 50.0f, b_pos.y + 0.0f,
};
b[ 4 ] = b[ 0 ];
sat_result_t result = sat( a, b );
for ( int i = 0; i < width * height; i++ ) {
buffer[ i ] = 0xff000000;
}
for ( int i = 0; i < 4; i++ ) {
draw_line( a[ i ], a[ i + 1 ], 0xffffffff );
draw_line( b[ i ], b[ i + 1 ], 0xffffffff );
}
if ( result.magnitude ) {
vec2_t start = { 255.0f, 255.0f };
vec2_t end = { 255.0f + result.axis.x * result.magnitude * result.direction, 255.0f + result.axis.y * result.magnitude * result.direction };
draw_line( start, end, 0xffff0000 );
}
char filename[ ] = "render_000.tga";
char c = ( char ) ( step / 100 );
char d = ( char ) ( ( step  ( c * 100 ) ) / 10 );
char u = ( char ) ( step  c * 100  d * 10 );
filename[ 7 ] = '0' + c;
filename[ 8 ] = '0' + d;
filename[ 9 ] = '0' + u;
FILE* file = fopen( filename, "wb" );
fwrite( tga_header, 18, 1, file );
fwrite( buffer, 512 * 512 * 4, 1, file );
fclose( file );
b_pos.x = 1;
b_pos.y = 1;
}
return 0;
}
