box2d-ruby 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +336 -0
- data/examples/contact_events.rb +22 -0
- data/examples/debug_draw.rb +15 -0
- data/examples/falling_ball.rb +19 -0
- data/examples/rugl_debug_draw.rb +68 -0
- data/examples/stagecraft_debug_draw.rb +64 -0
- data/examples/support/box2d_debug_lines.rb +125 -0
- data/ext/box2d/CMakeLists.txt +35 -0
- data/ext/box2d/extconf.rb +42 -0
- data/ext/box2d/native.c +9 -0
- data/ext/box2d/vendor/box2d/LICENSE +21 -0
- data/ext/box2d/vendor/box2d/VERSION +1 -0
- data/ext/box2d/vendor/box2d/include/box2d/base.h +131 -0
- data/ext/box2d/vendor/box2d/include/box2d/box2d.h +1222 -0
- data/ext/box2d/vendor/box2d/include/box2d/collision.h +830 -0
- data/ext/box2d/vendor/box2d/include/box2d/id.h +144 -0
- data/ext/box2d/vendor/box2d/include/box2d/math_functions.h +761 -0
- data/ext/box2d/vendor/box2d/include/box2d/types.h +1457 -0
- data/ext/box2d/vendor/box2d/src/CMakeLists.txt +223 -0
- data/ext/box2d/vendor/box2d/src/aabb.c +132 -0
- data/ext/box2d/vendor/box2d/src/aabb.h +56 -0
- data/ext/box2d/vendor/box2d/src/arena_allocator.c +112 -0
- data/ext/box2d/vendor/box2d/src/arena_allocator.h +48 -0
- data/ext/box2d/vendor/box2d/src/array.c +8 -0
- data/ext/box2d/vendor/box2d/src/array.h +179 -0
- data/ext/box2d/vendor/box2d/src/atomic.h +79 -0
- data/ext/box2d/vendor/box2d/src/bitset.c +67 -0
- data/ext/box2d/vendor/box2d/src/bitset.h +65 -0
- data/ext/box2d/vendor/box2d/src/body.c +1884 -0
- data/ext/box2d/vendor/box2d/src/body.h +194 -0
- data/ext/box2d/vendor/box2d/src/box2d.natvis +41 -0
- data/ext/box2d/vendor/box2d/src/broad_phase.c +524 -0
- data/ext/box2d/vendor/box2d/src/broad_phase.h +83 -0
- data/ext/box2d/vendor/box2d/src/constants.h +54 -0
- data/ext/box2d/vendor/box2d/src/constraint_graph.c +322 -0
- data/ext/box2d/vendor/box2d/src/constraint_graph.h +58 -0
- data/ext/box2d/vendor/box2d/src/contact.c +650 -0
- data/ext/box2d/vendor/box2d/src/contact.h +148 -0
- data/ext/box2d/vendor/box2d/src/contact_solver.c +2120 -0
- data/ext/box2d/vendor/box2d/src/contact_solver.h +54 -0
- data/ext/box2d/vendor/box2d/src/core.c +178 -0
- data/ext/box2d/vendor/box2d/src/core.h +149 -0
- data/ext/box2d/vendor/box2d/src/ctz.h +112 -0
- data/ext/box2d/vendor/box2d/src/distance.c +1415 -0
- data/ext/box2d/vendor/box2d/src/distance_joint.c +556 -0
- data/ext/box2d/vendor/box2d/src/dynamic_tree.c +1989 -0
- data/ext/box2d/vendor/box2d/src/geometry.c +1028 -0
- data/ext/box2d/vendor/box2d/src/hull.c +328 -0
- data/ext/box2d/vendor/box2d/src/id_pool.c +79 -0
- data/ext/box2d/vendor/box2d/src/id_pool.h +35 -0
- data/ext/box2d/vendor/box2d/src/island.c +977 -0
- data/ext/box2d/vendor/box2d/src/island.h +89 -0
- data/ext/box2d/vendor/box2d/src/joint.c +1272 -0
- data/ext/box2d/vendor/box2d/src/joint.h +335 -0
- data/ext/box2d/vendor/box2d/src/manifold.c +1726 -0
- data/ext/box2d/vendor/box2d/src/math_functions.c +159 -0
- data/ext/box2d/vendor/box2d/src/motor_joint.c +283 -0
- data/ext/box2d/vendor/box2d/src/mouse_joint.c +214 -0
- data/ext/box2d/vendor/box2d/src/mover.c +73 -0
- data/ext/box2d/vendor/box2d/src/prismatic_joint.c +656 -0
- data/ext/box2d/vendor/box2d/src/revolute_joint.c +534 -0
- data/ext/box2d/vendor/box2d/src/sensor.c +389 -0
- data/ext/box2d/vendor/box2d/src/sensor.h +36 -0
- data/ext/box2d/vendor/box2d/src/shape.c +1714 -0
- data/ext/box2d/vendor/box2d/src/shape.h +123 -0
- data/ext/box2d/vendor/box2d/src/solver.c +2038 -0
- data/ext/box2d/vendor/box2d/src/solver.h +155 -0
- data/ext/box2d/vendor/box2d/src/solver_set.c +613 -0
- data/ext/box2d/vendor/box2d/src/solver_set.h +57 -0
- data/ext/box2d/vendor/box2d/src/table.c +238 -0
- data/ext/box2d/vendor/box2d/src/table.h +37 -0
- data/ext/box2d/vendor/box2d/src/timer.c +185 -0
- data/ext/box2d/vendor/box2d/src/types.c +151 -0
- data/ext/box2d/vendor/box2d/src/weld_joint.c +310 -0
- data/ext/box2d/vendor/box2d/src/wheel_joint.c +551 -0
- data/ext/box2d/vendor/box2d/src/world.c +3301 -0
- data/ext/box2d/vendor/box2d/src/world.h +192 -0
- data/generator/generate.rb +316 -0
- data/generator/layout_probe.c +507 -0
- data/generator/verify_layouts.rb +32 -0
- data/lib/box2d/body.rb +172 -0
- data/lib/box2d/body_definition.rb +38 -0
- data/lib/box2d/body_shapes.rb +135 -0
- data/lib/box2d/chain.rb +61 -0
- data/lib/box2d/debug_draw.rb +118 -0
- data/lib/box2d/events.rb +103 -0
- data/lib/box2d/fixed_stepper.rb +39 -0
- data/lib/box2d/handle.rb +46 -0
- data/lib/box2d/hit.rb +5 -0
- data/lib/box2d/joint.rb +197 -0
- data/lib/box2d/native.rb +1462 -0
- data/lib/box2d/native_loader.rb +45 -0
- data/lib/box2d/pixel_scale.rb +29 -0
- data/lib/box2d/shape.rb +109 -0
- data/lib/box2d/shape_definition.rb +44 -0
- data/lib/box2d/value_conversion.rb +80 -0
- data/lib/box2d/version.rb +7 -0
- data/lib/box2d/world.rb +135 -0
- data/lib/box2d/world_joints.rb +156 -0
- data/lib/box2d/world_queries.rb +122 -0
- data/lib/box2d/world_registry.rb +95 -0
- data/lib/box2d.rb +31 -0
- data/script/build_platform_gem.rb +24 -0
- data/script/deterministic_scene.rb +56 -0
- data/script/update_deterministic_snapshot.rb +11 -0
- data/script/verify_platform_gem.rb +24 -0
- metadata +164 -0
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
include(GNUInstallDirs)
|
|
2
|
+
|
|
3
|
+
set(BOX2D_SOURCE_FILES
|
|
4
|
+
aabb.c
|
|
5
|
+
aabb.h
|
|
6
|
+
arena_allocator.c
|
|
7
|
+
arena_allocator.h
|
|
8
|
+
array.c
|
|
9
|
+
array.h
|
|
10
|
+
atomic.h
|
|
11
|
+
bitset.c
|
|
12
|
+
bitset.h
|
|
13
|
+
body.c
|
|
14
|
+
body.h
|
|
15
|
+
broad_phase.c
|
|
16
|
+
broad_phase.h
|
|
17
|
+
constants.h
|
|
18
|
+
constraint_graph.c
|
|
19
|
+
constraint_graph.h
|
|
20
|
+
contact.c
|
|
21
|
+
contact.h
|
|
22
|
+
contact_solver.c
|
|
23
|
+
contact_solver.h
|
|
24
|
+
core.c
|
|
25
|
+
core.h
|
|
26
|
+
ctz.h
|
|
27
|
+
distance.c
|
|
28
|
+
distance_joint.c
|
|
29
|
+
dynamic_tree.c
|
|
30
|
+
geometry.c
|
|
31
|
+
hull.c
|
|
32
|
+
id_pool.c
|
|
33
|
+
id_pool.h
|
|
34
|
+
island.c
|
|
35
|
+
island.h
|
|
36
|
+
joint.c
|
|
37
|
+
joint.h
|
|
38
|
+
manifold.c
|
|
39
|
+
math_functions.c
|
|
40
|
+
motor_joint.c
|
|
41
|
+
mouse_joint.c
|
|
42
|
+
mover.c
|
|
43
|
+
prismatic_joint.c
|
|
44
|
+
revolute_joint.c
|
|
45
|
+
sensor.c
|
|
46
|
+
sensor.h
|
|
47
|
+
shape.c
|
|
48
|
+
shape.h
|
|
49
|
+
solver.c
|
|
50
|
+
solver.h
|
|
51
|
+
solver_set.c
|
|
52
|
+
solver_set.h
|
|
53
|
+
table.c
|
|
54
|
+
table.h
|
|
55
|
+
timer.c
|
|
56
|
+
types.c
|
|
57
|
+
weld_joint.c
|
|
58
|
+
wheel_joint.c
|
|
59
|
+
world.c
|
|
60
|
+
world.h
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
set(BOX2D_API_FILES
|
|
64
|
+
../include/box2d/base.h
|
|
65
|
+
../include/box2d/box2d.h
|
|
66
|
+
../include/box2d/collision.h
|
|
67
|
+
../include/box2d/id.h
|
|
68
|
+
../include/box2d/math_functions.h
|
|
69
|
+
../include/box2d/types.h
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
# Hide internal functions
|
|
73
|
+
# https://gcc.gnu.org/wiki/Visibility
|
|
74
|
+
set(CMAKE_C_VISIBILITY_PRESET hidden)
|
|
75
|
+
set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)
|
|
76
|
+
|
|
77
|
+
add_library(box2d ${BOX2D_SOURCE_FILES} ${BOX2D_API_FILES})
|
|
78
|
+
|
|
79
|
+
target_include_directories(box2d
|
|
80
|
+
PUBLIC
|
|
81
|
+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include>
|
|
82
|
+
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
|
|
83
|
+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
|
|
84
|
+
PRIVATE
|
|
85
|
+
${CMAKE_CURRENT_SOURCE_DIR}
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
set(CMAKE_DEBUG_POSTFIX "d")
|
|
89
|
+
|
|
90
|
+
# Box2D uses C17 for _Static_assert and anonymous unions
|
|
91
|
+
set_target_properties(box2d PROPERTIES
|
|
92
|
+
C_STANDARD 17
|
|
93
|
+
C_STANDARD_REQUIRED YES
|
|
94
|
+
C_EXTENSIONS YES
|
|
95
|
+
COMPILE_WARNING_AS_ERROR ON
|
|
96
|
+
VERSION ${PROJECT_VERSION}
|
|
97
|
+
SOVERSION ${PROJECT_VERSION_MAJOR}
|
|
98
|
+
DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX}
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
if (BOX2D_PROFILE)
|
|
102
|
+
target_compile_definitions(box2d PRIVATE BOX2D_PROFILE)
|
|
103
|
+
|
|
104
|
+
FetchContent_Declare(
|
|
105
|
+
tracy
|
|
106
|
+
GIT_REPOSITORY https://github.com/wolfpld/tracy.git
|
|
107
|
+
GIT_TAG v0.11.1
|
|
108
|
+
GIT_SHALLOW TRUE
|
|
109
|
+
GIT_PROGRESS TRUE
|
|
110
|
+
)
|
|
111
|
+
FetchContent_MakeAvailable(tracy)
|
|
112
|
+
|
|
113
|
+
target_link_libraries(box2d PUBLIC TracyClient)
|
|
114
|
+
endif()
|
|
115
|
+
|
|
116
|
+
if (BOX2D_VALIDATE)
|
|
117
|
+
message(STATUS "Box2D validation ON")
|
|
118
|
+
target_compile_definitions(box2d PRIVATE BOX2D_VALIDATE)
|
|
119
|
+
endif()
|
|
120
|
+
|
|
121
|
+
if (BOX2D_DISABLE_SIMD)
|
|
122
|
+
message(STATUS "Box2D SIMD disabled")
|
|
123
|
+
target_compile_definitions(box2d PRIVATE BOX2D_DISABLE_SIMD)
|
|
124
|
+
endif()
|
|
125
|
+
|
|
126
|
+
if (MSVC)
|
|
127
|
+
message(STATUS "Box2D on MSVC")
|
|
128
|
+
if (BUILD_SHARED_LIBS)
|
|
129
|
+
# this is needed by DLL users to import Box2D symbols
|
|
130
|
+
target_compile_definitions(box2d INTERFACE BOX2D_DLL)
|
|
131
|
+
endif()
|
|
132
|
+
|
|
133
|
+
# Visual Studio won't load the natvis unless it is in the project
|
|
134
|
+
target_sources(box2d PRIVATE box2d.natvis)
|
|
135
|
+
|
|
136
|
+
# Enable asserts in release with debug info
|
|
137
|
+
target_compile_definitions(box2d PUBLIC "$<$<CONFIG:RELWITHDEBINFO>:B2_ENABLE_ASSERT>")
|
|
138
|
+
|
|
139
|
+
# Warnings
|
|
140
|
+
# 4710 - warn about inline functions that are not inlined
|
|
141
|
+
target_compile_options(box2d PRIVATE /Wall /wd4820 /wd5045 /wd4061 /wd4711 /wd4710)
|
|
142
|
+
|
|
143
|
+
if (BOX2D_AVX2)
|
|
144
|
+
message(STATUS "Box2D using AVX2")
|
|
145
|
+
target_compile_definitions(box2d PRIVATE BOX2D_AVX2)
|
|
146
|
+
target_compile_options(box2d PRIVATE /arch:AVX2)
|
|
147
|
+
endif()
|
|
148
|
+
|
|
149
|
+
if (${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang")
|
|
150
|
+
target_compile_options(box2d PRIVATE -Wmissing-prototypes)
|
|
151
|
+
endif()
|
|
152
|
+
|
|
153
|
+
elseif (MINGW)
|
|
154
|
+
message(STATUS "Box2D on MinGW")
|
|
155
|
+
if (BOX2D_AVX2)
|
|
156
|
+
message(STATUS "Box2D using AVX2")
|
|
157
|
+
target_compile_definitions(box2d PRIVATE BOX2D_AVX2)
|
|
158
|
+
target_compile_options(box2d PRIVATE -mavx2)
|
|
159
|
+
else()
|
|
160
|
+
endif()
|
|
161
|
+
elseif (APPLE)
|
|
162
|
+
message(STATUS "Box2D on Apple")
|
|
163
|
+
target_compile_options(box2d PRIVATE -Wmissing-prototypes -Wall -Wextra -pedantic -Werror)
|
|
164
|
+
elseif (EMSCRIPTEN)
|
|
165
|
+
message(STATUS "Box2D on Emscripten")
|
|
166
|
+
if (BOX2D_DISABLE_SIMD OFF)
|
|
167
|
+
target_compile_options(box2d PRIVATE -msimd128 -msse2)
|
|
168
|
+
endif()
|
|
169
|
+
elseif (UNIX)
|
|
170
|
+
message(STATUS "Box2D using Unix")
|
|
171
|
+
target_compile_options(box2d PRIVATE -Wmissing-prototypes -Wall -Wextra -pedantic -Werror -Wno-unused-value)
|
|
172
|
+
if ("${CMAKE_HOST_SYSTEM_PROCESSOR}" STREQUAL "aarch64")
|
|
173
|
+
# raspberry pi
|
|
174
|
+
# -mfpu=neon
|
|
175
|
+
# target_compile_options(box2d PRIVATE)
|
|
176
|
+
else()
|
|
177
|
+
# x64
|
|
178
|
+
if (BOX2D_AVX2)
|
|
179
|
+
message(STATUS "Box2D using AVX2")
|
|
180
|
+
# FMA -mfma -mavx -march=native
|
|
181
|
+
target_compile_definitions(box2d PRIVATE BOX2D_AVX2)
|
|
182
|
+
target_compile_options(box2d PRIVATE -mavx2)
|
|
183
|
+
else()
|
|
184
|
+
endif()
|
|
185
|
+
endif()
|
|
186
|
+
endif()
|
|
187
|
+
|
|
188
|
+
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" PREFIX "src" FILES ${BOX2D_SOURCE_FILES})
|
|
189
|
+
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/../include" PREFIX "include" FILES ${BOX2D_API_FILES})
|
|
190
|
+
|
|
191
|
+
add_library(box2d::box2d ALIAS box2d)
|
|
192
|
+
|
|
193
|
+
install(
|
|
194
|
+
TARGETS box2d
|
|
195
|
+
EXPORT box2dConfig
|
|
196
|
+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
|
197
|
+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
|
198
|
+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
install(
|
|
202
|
+
EXPORT box2dConfig
|
|
203
|
+
NAMESPACE box2d::
|
|
204
|
+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/box2d"
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
install(
|
|
208
|
+
FILES ${BOX2D_API_FILES}
|
|
209
|
+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/box2d
|
|
210
|
+
)
|
|
211
|
+
|
|
212
|
+
include(CMakePackageConfigHelpers)
|
|
213
|
+
|
|
214
|
+
write_basic_package_version_file(
|
|
215
|
+
"${CMAKE_CURRENT_BINARY_DIR}/box2dConfigVersion.cmake"
|
|
216
|
+
VERSION ${BOX2D_VERSION}
|
|
217
|
+
COMPATIBILITY SameMajorVersion
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
install(
|
|
221
|
+
FILES "${CMAKE_CURRENT_BINARY_DIR}/box2dConfigVersion.cmake"
|
|
222
|
+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/box2d"
|
|
223
|
+
)
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2023 Erin Catto
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
#include "aabb.h"
|
|
5
|
+
|
|
6
|
+
#include "box2d/math_functions.h"
|
|
7
|
+
|
|
8
|
+
#include <float.h>
|
|
9
|
+
|
|
10
|
+
bool b2IsValidAABB( b2AABB a )
|
|
11
|
+
{
|
|
12
|
+
b2Vec2 d = b2Sub( a.upperBound, a.lowerBound );
|
|
13
|
+
bool valid = d.x >= 0.0f && d.y >= 0.0f;
|
|
14
|
+
valid = valid && b2IsValidVec2( a.lowerBound ) && b2IsValidVec2( a.upperBound );
|
|
15
|
+
return valid;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// From Real-time Collision Detection, p179.
|
|
19
|
+
b2CastOutput b2AABB_RayCast( b2AABB a, b2Vec2 p1, b2Vec2 p2 )
|
|
20
|
+
{
|
|
21
|
+
// Radius not handled
|
|
22
|
+
b2CastOutput output = { 0 };
|
|
23
|
+
|
|
24
|
+
float tmin = -FLT_MAX;
|
|
25
|
+
float tmax = FLT_MAX;
|
|
26
|
+
|
|
27
|
+
b2Vec2 p = p1;
|
|
28
|
+
b2Vec2 d = b2Sub( p2, p1 );
|
|
29
|
+
b2Vec2 absD = b2Abs( d );
|
|
30
|
+
|
|
31
|
+
b2Vec2 normal = b2Vec2_zero;
|
|
32
|
+
|
|
33
|
+
// x-coordinate
|
|
34
|
+
if ( absD.x < FLT_EPSILON )
|
|
35
|
+
{
|
|
36
|
+
// parallel
|
|
37
|
+
if ( p.x < a.lowerBound.x || a.upperBound.x < p.x )
|
|
38
|
+
{
|
|
39
|
+
return output;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
else
|
|
43
|
+
{
|
|
44
|
+
float inv_d = 1.0f / d.x;
|
|
45
|
+
float t1 = ( a.lowerBound.x - p.x ) * inv_d;
|
|
46
|
+
float t2 = ( a.upperBound.x - p.x ) * inv_d;
|
|
47
|
+
|
|
48
|
+
// Sign of the normal vector.
|
|
49
|
+
float s = -1.0f;
|
|
50
|
+
|
|
51
|
+
if ( t1 > t2 )
|
|
52
|
+
{
|
|
53
|
+
float tmp = t1;
|
|
54
|
+
t1 = t2;
|
|
55
|
+
t2 = tmp;
|
|
56
|
+
s = 1.0f;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Push the min up
|
|
60
|
+
if ( t1 > tmin )
|
|
61
|
+
{
|
|
62
|
+
normal.y = 0.0f;
|
|
63
|
+
normal.x = s;
|
|
64
|
+
tmin = t1;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Pull the max down
|
|
68
|
+
tmax = b2MinFloat( tmax, t2 );
|
|
69
|
+
|
|
70
|
+
if ( tmin > tmax )
|
|
71
|
+
{
|
|
72
|
+
return output;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// y-coordinate
|
|
77
|
+
if ( absD.y < FLT_EPSILON )
|
|
78
|
+
{
|
|
79
|
+
// parallel
|
|
80
|
+
if ( p.y < a.lowerBound.y || a.upperBound.y < p.y )
|
|
81
|
+
{
|
|
82
|
+
return output;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
else
|
|
86
|
+
{
|
|
87
|
+
float inv_d = 1.0f / d.y;
|
|
88
|
+
float t1 = ( a.lowerBound.y - p.y ) * inv_d;
|
|
89
|
+
float t2 = ( a.upperBound.y - p.y ) * inv_d;
|
|
90
|
+
|
|
91
|
+
// Sign of the normal vector.
|
|
92
|
+
float s = -1.0f;
|
|
93
|
+
|
|
94
|
+
if ( t1 > t2 )
|
|
95
|
+
{
|
|
96
|
+
float tmp = t1;
|
|
97
|
+
t1 = t2;
|
|
98
|
+
t2 = tmp;
|
|
99
|
+
s = 1.0f;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Push the min up
|
|
103
|
+
if ( t1 > tmin )
|
|
104
|
+
{
|
|
105
|
+
normal.x = 0.0f;
|
|
106
|
+
normal.y = s;
|
|
107
|
+
tmin = t1;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Pull the max down
|
|
111
|
+
tmax = b2MinFloat( tmax, t2 );
|
|
112
|
+
|
|
113
|
+
if ( tmin > tmax )
|
|
114
|
+
{
|
|
115
|
+
return output;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Does the ray start inside the box?
|
|
120
|
+
// Does the ray intersect beyond the max fraction?
|
|
121
|
+
if ( tmin < 0.0f || 1.0f < tmin )
|
|
122
|
+
{
|
|
123
|
+
return output;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Intersection.
|
|
127
|
+
output.fraction = tmin;
|
|
128
|
+
output.normal = normal;
|
|
129
|
+
output.point = b2Lerp( p1, p2, tmin );
|
|
130
|
+
output.hit = true;
|
|
131
|
+
return output;
|
|
132
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2023 Erin Catto
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
#pragma once
|
|
5
|
+
|
|
6
|
+
#include "box2d/types.h"
|
|
7
|
+
|
|
8
|
+
// Ray cast an AABB
|
|
9
|
+
b2CastOutput b2AABB_RayCast( b2AABB a, b2Vec2 p1, b2Vec2 p2 );
|
|
10
|
+
|
|
11
|
+
// Get surface area of an AABB (the perimeter length)
|
|
12
|
+
static inline float b2Perimeter( b2AABB a )
|
|
13
|
+
{
|
|
14
|
+
float wx = a.upperBound.x - a.lowerBound.x;
|
|
15
|
+
float wy = a.upperBound.y - a.lowerBound.y;
|
|
16
|
+
return 2.0f * ( wx + wy );
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/// Enlarge a to contain b
|
|
20
|
+
/// @return true if the AABB grew
|
|
21
|
+
static inline bool b2EnlargeAABB( b2AABB* a, b2AABB b )
|
|
22
|
+
{
|
|
23
|
+
bool changed = false;
|
|
24
|
+
if ( b.lowerBound.x < a->lowerBound.x )
|
|
25
|
+
{
|
|
26
|
+
a->lowerBound.x = b.lowerBound.x;
|
|
27
|
+
changed = true;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if ( b.lowerBound.y < a->lowerBound.y )
|
|
31
|
+
{
|
|
32
|
+
a->lowerBound.y = b.lowerBound.y;
|
|
33
|
+
changed = true;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if ( a->upperBound.x < b.upperBound.x )
|
|
37
|
+
{
|
|
38
|
+
a->upperBound.x = b.upperBound.x;
|
|
39
|
+
changed = true;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if ( a->upperBound.y < b.upperBound.y )
|
|
43
|
+
{
|
|
44
|
+
a->upperBound.y = b.upperBound.y;
|
|
45
|
+
changed = true;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return changed;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/// Do a and b overlap
|
|
52
|
+
static inline bool b2AABB_Overlaps( b2AABB a, b2AABB b )
|
|
53
|
+
{
|
|
54
|
+
return !( b.lowerBound.x > a.upperBound.x || b.lowerBound.y > a.upperBound.y || a.lowerBound.x > b.upperBound.x ||
|
|
55
|
+
a.lowerBound.y > b.upperBound.y );
|
|
56
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2023 Erin Catto
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
#include "arena_allocator.h"
|
|
5
|
+
|
|
6
|
+
#include "array.h"
|
|
7
|
+
#include "core.h"
|
|
8
|
+
|
|
9
|
+
#include <stdbool.h>
|
|
10
|
+
#include <stddef.h>
|
|
11
|
+
|
|
12
|
+
B2_ARRAY_SOURCE( b2ArenaEntry, b2ArenaEntry )
|
|
13
|
+
|
|
14
|
+
b2ArenaAllocator b2CreateArenaAllocator( int capacity )
|
|
15
|
+
{
|
|
16
|
+
B2_ASSERT( capacity >= 0 );
|
|
17
|
+
b2ArenaAllocator allocator = { 0 };
|
|
18
|
+
allocator.capacity = capacity;
|
|
19
|
+
allocator.data = b2Alloc( capacity );
|
|
20
|
+
allocator.allocation = 0;
|
|
21
|
+
allocator.maxAllocation = 0;
|
|
22
|
+
allocator.index = 0;
|
|
23
|
+
allocator.entries = b2ArenaEntryArray_Create( 32 );
|
|
24
|
+
return allocator;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
void b2DestroyArenaAllocator( b2ArenaAllocator* allocator )
|
|
28
|
+
{
|
|
29
|
+
b2ArenaEntryArray_Destroy( &allocator->entries );
|
|
30
|
+
b2Free( allocator->data, allocator->capacity );
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
void* b2AllocateArenaItem( b2ArenaAllocator* alloc, int size, const char* name )
|
|
34
|
+
{
|
|
35
|
+
// ensure allocation is 32 byte aligned to support 256-bit SIMD
|
|
36
|
+
int size32 = ( ( size - 1 ) | 0x1F ) + 1;
|
|
37
|
+
|
|
38
|
+
b2ArenaEntry entry;
|
|
39
|
+
entry.size = size32;
|
|
40
|
+
entry.name = name;
|
|
41
|
+
if ( alloc->index + size32 > alloc->capacity )
|
|
42
|
+
{
|
|
43
|
+
// fall back to the heap (undesirable)
|
|
44
|
+
entry.data = b2Alloc( size32 );
|
|
45
|
+
entry.usedMalloc = true;
|
|
46
|
+
|
|
47
|
+
B2_ASSERT( ( (uintptr_t)entry.data & 0x1F ) == 0 );
|
|
48
|
+
}
|
|
49
|
+
else
|
|
50
|
+
{
|
|
51
|
+
entry.data = alloc->data + alloc->index;
|
|
52
|
+
entry.usedMalloc = false;
|
|
53
|
+
alloc->index += size32;
|
|
54
|
+
|
|
55
|
+
B2_ASSERT( ( (uintptr_t)entry.data & 0x1F ) == 0 );
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
alloc->allocation += size32;
|
|
59
|
+
if ( alloc->allocation > alloc->maxAllocation )
|
|
60
|
+
{
|
|
61
|
+
alloc->maxAllocation = alloc->allocation;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
b2ArenaEntryArray_Push( &alloc->entries, entry );
|
|
65
|
+
return entry.data;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
void b2FreeArenaItem( b2ArenaAllocator* alloc, void* mem )
|
|
69
|
+
{
|
|
70
|
+
int entryCount = alloc->entries.count;
|
|
71
|
+
B2_ASSERT( entryCount > 0 );
|
|
72
|
+
b2ArenaEntry* entry = alloc->entries.data + ( entryCount - 1 );
|
|
73
|
+
B2_ASSERT( mem == entry->data );
|
|
74
|
+
if ( entry->usedMalloc )
|
|
75
|
+
{
|
|
76
|
+
b2Free( mem, entry->size );
|
|
77
|
+
}
|
|
78
|
+
else
|
|
79
|
+
{
|
|
80
|
+
alloc->index -= entry->size;
|
|
81
|
+
}
|
|
82
|
+
alloc->allocation -= entry->size;
|
|
83
|
+
b2ArenaEntryArray_Pop( &alloc->entries );
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
void b2GrowArena( b2ArenaAllocator* alloc )
|
|
87
|
+
{
|
|
88
|
+
// Stack must not be in use
|
|
89
|
+
B2_ASSERT( alloc->allocation == 0 );
|
|
90
|
+
|
|
91
|
+
if ( alloc->maxAllocation > alloc->capacity )
|
|
92
|
+
{
|
|
93
|
+
b2Free( alloc->data, alloc->capacity );
|
|
94
|
+
alloc->capacity = alloc->maxAllocation + alloc->maxAllocation / 2;
|
|
95
|
+
alloc->data = b2Alloc( alloc->capacity );
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
int b2GetArenaCapacity( b2ArenaAllocator* alloc )
|
|
100
|
+
{
|
|
101
|
+
return alloc->capacity;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
int b2GetArenaAllocation( b2ArenaAllocator* alloc )
|
|
105
|
+
{
|
|
106
|
+
return alloc->allocation;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
int b2GetMaxArenaAllocation( b2ArenaAllocator* alloc )
|
|
110
|
+
{
|
|
111
|
+
return alloc->maxAllocation;
|
|
112
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2023 Erin Catto
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
#pragma once
|
|
5
|
+
|
|
6
|
+
#include "array.h"
|
|
7
|
+
|
|
8
|
+
B2_ARRAY_DECLARE( b2ArenaEntry, b2ArenaEntry );
|
|
9
|
+
|
|
10
|
+
typedef struct b2ArenaEntry
|
|
11
|
+
{
|
|
12
|
+
char* data;
|
|
13
|
+
const char* name;
|
|
14
|
+
int size;
|
|
15
|
+
bool usedMalloc;
|
|
16
|
+
} b2ArenaEntry;
|
|
17
|
+
|
|
18
|
+
// This is a stack-like arena allocator used for fast per step allocations.
|
|
19
|
+
// You must nest allocate/free pairs. The code will B2_ASSERT
|
|
20
|
+
// if you try to interleave multiple allocate/free pairs.
|
|
21
|
+
// This allocator uses the heap if space is insufficient.
|
|
22
|
+
// I could remove the need to free entries individually.
|
|
23
|
+
typedef struct b2ArenaAllocator
|
|
24
|
+
{
|
|
25
|
+
char* data;
|
|
26
|
+
int capacity;
|
|
27
|
+
int index;
|
|
28
|
+
|
|
29
|
+
int allocation;
|
|
30
|
+
int maxAllocation;
|
|
31
|
+
|
|
32
|
+
b2ArenaEntryArray entries;
|
|
33
|
+
} b2ArenaAllocator;
|
|
34
|
+
|
|
35
|
+
b2ArenaAllocator b2CreateArenaAllocator( int capacity );
|
|
36
|
+
void b2DestroyArenaAllocator( b2ArenaAllocator* allocator );
|
|
37
|
+
|
|
38
|
+
void* b2AllocateArenaItem( b2ArenaAllocator* alloc, int size, const char* name );
|
|
39
|
+
void b2FreeArenaItem( b2ArenaAllocator* alloc, void* mem );
|
|
40
|
+
|
|
41
|
+
// Grow the arena based on usage
|
|
42
|
+
void b2GrowArena( b2ArenaAllocator* alloc );
|
|
43
|
+
|
|
44
|
+
int b2GetArenaCapacity( b2ArenaAllocator* alloc );
|
|
45
|
+
int b2GetArenaAllocation( b2ArenaAllocator* alloc );
|
|
46
|
+
int b2GetMaxArenaAllocation( b2ArenaAllocator* alloc );
|
|
47
|
+
|
|
48
|
+
B2_ARRAY_INLINE( b2ArenaEntry, b2ArenaEntry )
|