Fixes 03 (#40)
Fixes #33, #34, #35, #37, #39, #41, #42, #43 Also fixes b3Body_SetMassData inertia tensor.
This commit is contained in:
parent
29bf523ce7
commit
655799893a
25 changed files with 771 additions and 151 deletions
|
|
@ -1,8 +1,10 @@
|
|||
# Foundations
|
||||
|
||||
Box3D provides minimal base functionality for allocation hooks and vector math. The C interface
|
||||
allows most runtime data and types to be defined internally in the `src` folder.
|
||||
|
||||
## Assertions
|
||||
|
||||
Box3D will assert on bad input. This includes things like sending in NaN or infinity for values.
|
||||
It will assert if you use negative values for things that should only be positive, such as density.
|
||||
|
||||
|
|
@ -13,6 +15,7 @@ You may wish to capture assertions in your application. In that case use `b3SetA
|
|||
lets you override the debugger break and/or perform your own error handling.
|
||||
|
||||
## Allocation
|
||||
|
||||
Box3D uses memory efficiently and minimizes per-frame allocations by pooling memory. The engine
|
||||
quickly adapts to the simulation size. After the first step or two of simulation there should be
|
||||
no further per-frame allocations.
|
||||
|
|
@ -29,6 +32,7 @@ You can provide a custom allocator using `b3SetAllocator()` and query the total
|
|||
allocated using `b3GetByteCount()`.
|
||||
|
||||
## Version
|
||||
|
||||
The `b3Version` structure holds the current version so you can query it at run-time using
|
||||
`b3GetVersion()`.
|
||||
|
||||
|
|
@ -120,90 +124,62 @@ b3Transform inv = b3InvertTransform(t);
|
|||
```
|
||||
|
||||
### b3Matrix3
|
||||
|
||||
3×3 matrix stored as three column vectors `cx`, `cy`, `cz`. Primarily used for inertia
|
||||
tensors and rotation matrices. Useful operations include `b3MulMV` (matrix-vector multiply),
|
||||
`b3MulMM` (matrix-matrix multiply), `b3Transpose`, `b3InvertMatrix`, and `b3MakeMatrixFromQuat`.
|
||||
|
||||
### b3AABB
|
||||
|
||||
Axis-aligned bounding box with `lowerBound` and `upperBound` as `b3Vec3`. Helpers include
|
||||
`b3AABB_Overlaps`, `b3AABB_Contains`, `b3AABB_ContainsPoint`, `b3AABB_Union`,
|
||||
`b3AABB_Center`, `b3AABB_Extents`, `b3AABB_Inflate`, and `b3AABB_Transform`.
|
||||
|
||||
## Multithreading {#multi}
|
||||
Box3D has been highly optimized for multithreading. Multithreading is not required and by
|
||||
default Box3D runs single-threaded. If performance matters, you should consider wiring up
|
||||
the multithreading interface.
|
||||
|
||||
Box3D does **not** create threads itself. You supply your own thread pool and hook it up through
|
||||
the world definition before calling `b3CreateWorld`. See `b3EnqueueTaskCallback`,
|
||||
`b3FinishTaskCallback`, `b3WorldDef::workerCount`, `b3WorldDef::enqueueTask`, and
|
||||
`b3WorldDef::finishTask`:
|
||||
Box3D has been optimized for multithreading. Multithreading is not required and by default Box3D will run single-threaded. If performance is important for your application, you should consider using the multithreading features.
|
||||
|
||||
### Internal scheduler
|
||||
|
||||
Box3D has a built-in task scheduler that creates threads. You can use the built-in scheduler by setting the worker count in the world definition. This example shows how to use 4 workers. In this case Box3D will create 3 threads, and count the thread that calls `b3World_Step` as the fourth worker. I recommend to use the core count of the CPU as the worker count, not counting hyper-threads or efficiency cores.
|
||||
|
||||
```c
|
||||
b3WorldDef worldDef = b3DefaultWorldDef();
|
||||
worldDef.workerCount = myThreadCount;
|
||||
worldDef.enqueueTask = MyEnqueueTask;
|
||||
worldDef.finishTask = MyFinishTask;
|
||||
worldDef.userTaskContext = myScheduler;
|
||||
|
||||
b3WorldId worldId = b3CreateWorld(&worldDef);
|
||||
worldDef.workerCount = 4;
|
||||
```
|
||||
|
||||
Your `MyEnqueueTask` callback receives a `b3TaskCallback*` function pointer plus a context
|
||||
pointer that must be forwarded to the task exactly once on a worker thread. Return a non-null
|
||||
`void*` handle representing your pending task object, or null if the work was executed
|
||||
serially inside the callback (Box3D then skips the corresponding `b3FinishTaskCallback`).
|
||||
### External scheduler
|
||||
|
||||
You can optionally connect your own task scheduler if you want more control. Multithreading is established for each Box3D world you create and must be hooked up to the world definition. See `b3TaskCallback()`, `b3EnqueueTaskCallback()`, and `b3FinishTaskCallback()` for more details. Also see `b3WorldDef::workerCount`, `b3WorldDef::enqueueTask`, and `b3WorldDef::finishTask`.
|
||||
|
||||
```c
|
||||
// Enqueue callback signature
|
||||
// Implement b3EnqueueTaskCallback
|
||||
void* MyEnqueueTask(b3TaskCallback* task, void* taskContext,
|
||||
void* userContext, const char* taskName)
|
||||
{
|
||||
MyTask* t = AllocTask();
|
||||
t->task = task;
|
||||
t->taskContext = taskContext;
|
||||
SubmitToThreadPool(t); // runs task(taskContext) on a worker
|
||||
return t; // non-null => Box3D will call MyFinishTask(t, ...)
|
||||
SubmitToThreadPool(t);
|
||||
return t;
|
||||
}
|
||||
|
||||
// Finish callback signature
|
||||
// Implement b3FinishTaskCallback
|
||||
void MyFinishTask(void* userTask, void* userContext)
|
||||
{
|
||||
MyTask* t = (MyTask*)userTask;
|
||||
WaitForCompletion(t);
|
||||
FreeTask(t);
|
||||
}
|
||||
```
|
||||
|
||||
`MyFinishTask` must block until the task has completed. Because the step blocks here on the tasks it
|
||||
spawned, `b3World_Step` holds its stack across every fork and join inside the step. This has a
|
||||
consequence for how you drive it. Run `b3World_Step` from a thread you can dedicate to the step, or
|
||||
from a fiber that `MyFinishTask` can park so the underlying thread is freed to run the queued tasks.
|
||||
In a job system that cannot park a job's stack, do not call `b3World_Step` from inside a job: a job
|
||||
that blocks on the sub-jobs it spawned without yielding its thread starves the workers it is waiting
|
||||
on and can deadlock. The alternative is help-while-wait, where the waiting thread runs other pending
|
||||
tasks itself. This is what the in-tree scheduler does, which is why it needs no fiber.
|
||||
|
||||
Box3D ships an in-tree scheduler (`src/scheduler.h`) used by the test suite and samples.
|
||||
You can adapt it or use it directly:
|
||||
|
||||
```c
|
||||
#include "scheduler.h"
|
||||
|
||||
b3Scheduler* scheduler = b3CreateScheduler(workerCount);
|
||||
|
||||
b3WorldDef worldDef = b3DefaultWorldDef();
|
||||
worldDef.workerCount = workerCount;
|
||||
worldDef.enqueueTask = b3SchedulerEnqueueTask;
|
||||
worldDef.finishTask = b3SchedulerFinishTask;
|
||||
worldDef.userTaskContext = scheduler;
|
||||
|
||||
b3WorldId worldId = b3CreateWorld(&worldDef);
|
||||
// ... simulate ...
|
||||
b3DestroyWorld(worldId);
|
||||
b3DestroyScheduler(scheduler);
|
||||
worldDef.enqueueTask = MyEnqueueTask;
|
||||
worldDef.finishTask = MyFinishTask;
|
||||
worldDef.workerCount = GetMyWorkerCount();
|
||||
```
|
||||
|
||||
### Threading model
|
||||
|
||||
The multithreading design for Box3D is focused on [data parallelism](https://en.wikipedia.org/wiki/Data_parallelism).
|
||||
The goal is to use multiple cores to finish the world simulation as fast as possible.
|
||||
Box3D multithreading is not designed for [task parallelism](https://en.wikipedia.org/wiki/Task_parallelism).
|
||||
|
|
@ -213,26 +189,34 @@ main thread. Those are examples of task parallelism.
|
|||
So when you design your game loop, you should let Box3D *go wide* and use multiple cores to
|
||||
finish its work quickly, without other threads interacting with the Box3D world at the same time.
|
||||
|
||||
It is expected that the thread that calls `b3World_Step` participates in making progress. Do not call
|
||||
`b3World_Step` and park that fiber. Tasks will only be enqueued on the thread that calls `b3World_Step`.
|
||||
`MyFinishTask` must block until the task has completed. Ideally your scheduler is helping make progress
|
||||
when `MyFinishTask` is called.
|
||||
|
||||
### Avoiding race conditions
|
||||
|
||||
In a multithreaded environment you must be careful to avoid [race conditions](https://en.wikipedia.org/wiki/Race_condition).
|
||||
Modifying the world while it is simulating will lead to unpredictable behavior and is never safe.
|
||||
It is also not safe to read data from a Box3D world while it is simulating; Box3D may move data
|
||||
It is also not safe to read data from a Box3D world while it is simulating. Box3D may move data
|
||||
structures to improve cache performance, so you could easily read garbage.
|
||||
|
||||
> **Caution**:
|
||||
> Do not perform read or write operations on a Box3D world during `b3World_Step()`
|
||||
|
||||
> **Caution**:
|
||||
> Do not write to the Box3D world from multiple threads
|
||||
> Do not perform read or write operations on a Box3D world during `b3World_Step()`.
|
||||
> Do not write to the Box3D world from multiple threads. Any operation that wakes
|
||||
> a body is not thread-safe.
|
||||
|
||||
It *is safe* to do ray-casts, shape-casts, and overlap tests from multiple threads outside of
|
||||
`b3World_Step()`. Generally any read-only operation is safe to do multithreaded outside of
|
||||
`b3World_Step()`. This can be very useful if you have multithreaded game logic.
|
||||
|
||||
## Multithreading Multiple Worlds
|
||||
|
||||
Some applications may wish to create multiple Box3D worlds and simulate them on different threads.
|
||||
This works fine because Box3D has very limited use of globals.
|
||||
|
||||
There are a few caveats:
|
||||
|
||||
- You will get a race condition if you create or destroy Box3D worlds from multiple threads. Use a mutex to guard those operations.
|
||||
- If you simulate multiple Box3D worlds simultaneously, they should probably not share a task system. Otherwise you risk preemption between worlds competing for the same workers.
|
||||
- Any callbacks you hook up to Box3D must be thread-safe, including memory allocators.
|
||||
|
|
|
|||
|
|
@ -2,22 +2,23 @@
|
|||
<!-- Created with Inkscape (http://www.inkscape.org/) -->
|
||||
|
||||
<svg
|
||||
xmlns:dc="http://purl.org/dc/elements/1.1/"
|
||||
xmlns:cc="http://creativecommons.org/ns#"
|
||||
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
|
||||
xmlns:svg="http://www.w3.org/2000/svg"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
|
||||
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
|
||||
width="278.64941"
|
||||
height="199.68138"
|
||||
width="285"
|
||||
height="205"
|
||||
id="svg2"
|
||||
version="1.1"
|
||||
inkscape:version="0.92.4 (5da689c313, 2019-01-14)"
|
||||
inkscape:version="1.4.3 (0d15f75, 2025-12-25)"
|
||||
sodipodi:docname="manifolds.svg"
|
||||
inkscape:export-filename="D:\Development\Box2D\Box2D\Documentation\images\manifolds.png"
|
||||
inkscape:export-xdpi="90"
|
||||
inkscape:export-ydpi="90">
|
||||
inkscape:export-ydpi="90"
|
||||
viewBox="-4 -4 285 205"
|
||||
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
|
||||
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
xmlns:svg="http://www.w3.org/2000/svg"
|
||||
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
|
||||
xmlns:cc="http://creativecommons.org/ns#"
|
||||
xmlns:dc="http://purl.org/dc/elements/1.1/">
|
||||
<defs
|
||||
id="defs4">
|
||||
<marker
|
||||
|
|
@ -50,38 +51,38 @@
|
|||
</marker>
|
||||
<inkscape:perspective
|
||||
sodipodi:type="inkscape:persp3d"
|
||||
inkscape:vp_x="0 : 526.18109 : 1"
|
||||
inkscape:vp_x="0 : 531.49971 : 1"
|
||||
inkscape:vp_y="0 : 1000 : 0"
|
||||
inkscape:vp_z="744.09448 : 526.18109 : 1"
|
||||
inkscape:persp3d-origin="372.04724 : 350.78739 : 1"
|
||||
inkscape:vp_z="744.09448 : 531.49971 : 1"
|
||||
inkscape:persp3d-origin="372.04724 : 356.10601 : 1"
|
||||
id="perspective10" />
|
||||
<inkscape:perspective
|
||||
id="perspective2830"
|
||||
inkscape:persp3d-origin="0.5 : 0.33333333 : 1"
|
||||
inkscape:vp_z="1 : 0.5 : 1"
|
||||
inkscape:persp3d-origin="0.5 : 5.6519521 : 1"
|
||||
inkscape:vp_z="1 : 5.8186188 : 1"
|
||||
inkscape:vp_y="0 : 1000 : 0"
|
||||
inkscape:vp_x="0 : 0.5 : 1"
|
||||
inkscape:vp_x="0 : 5.8186188 : 1"
|
||||
sodipodi:type="inkscape:persp3d" />
|
||||
<inkscape:perspective
|
||||
id="perspective2852"
|
||||
inkscape:persp3d-origin="0.5 : 0.33333333 : 1"
|
||||
inkscape:vp_z="1 : 0.5 : 1"
|
||||
inkscape:persp3d-origin="0.5 : 5.6519521 : 1"
|
||||
inkscape:vp_z="1 : 5.8186188 : 1"
|
||||
inkscape:vp_y="0 : 1000 : 0"
|
||||
inkscape:vp_x="0 : 0.5 : 1"
|
||||
inkscape:vp_x="0 : 5.8186188 : 1"
|
||||
sodipodi:type="inkscape:persp3d" />
|
||||
<inkscape:perspective
|
||||
id="perspective3648"
|
||||
inkscape:persp3d-origin="0.5 : 0.33333333 : 1"
|
||||
inkscape:vp_z="1 : 0.5 : 1"
|
||||
inkscape:persp3d-origin="0.5 : 5.6519521 : 1"
|
||||
inkscape:vp_z="1 : 5.8186188 : 1"
|
||||
inkscape:vp_y="0 : 1000 : 0"
|
||||
inkscape:vp_x="0 : 0.5 : 1"
|
||||
inkscape:vp_x="0 : 5.8186188 : 1"
|
||||
sodipodi:type="inkscape:persp3d" />
|
||||
<inkscape:perspective
|
||||
id="perspective4310"
|
||||
inkscape:persp3d-origin="0.5 : 0.33333333 : 1"
|
||||
inkscape:vp_z="1 : 0.5 : 1"
|
||||
inkscape:persp3d-origin="0.5 : 5.6519521 : 1"
|
||||
inkscape:vp_z="1 : 5.8186188 : 1"
|
||||
inkscape:vp_y="0 : 1000 : 0"
|
||||
inkscape:vp_x="0 : 0.5 : 1"
|
||||
inkscape:vp_x="0 : 5.8186188 : 1"
|
||||
sodipodi:type="inkscape:persp3d" />
|
||||
<marker
|
||||
inkscape:stockid="Arrow2Mend"
|
||||
|
|
@ -99,10 +100,10 @@
|
|||
</marker>
|
||||
<inkscape:perspective
|
||||
id="perspective4342"
|
||||
inkscape:persp3d-origin="0.5 : 0.33333333 : 1"
|
||||
inkscape:vp_z="1 : 0.5 : 1"
|
||||
inkscape:persp3d-origin="0.5 : 5.6519521 : 1"
|
||||
inkscape:vp_z="1 : 5.8186188 : 1"
|
||||
inkscape:vp_y="0 : 1000 : 0"
|
||||
inkscape:vp_x="0 : 0.5 : 1"
|
||||
inkscape:vp_x="0 : 5.8186188 : 1"
|
||||
sodipodi:type="inkscape:persp3d" />
|
||||
</defs>
|
||||
<sodipodi:namedview
|
||||
|
|
@ -112,21 +113,24 @@
|
|||
borderopacity="1.0"
|
||||
inkscape:pageopacity="0.0"
|
||||
inkscape:pageshadow="2"
|
||||
inkscape:zoom="2"
|
||||
inkscape:cx="107.80664"
|
||||
inkscape:cy="78.632812"
|
||||
inkscape:zoom="2.8284271"
|
||||
inkscape:cx="113.13709"
|
||||
inkscape:cy="65.760931"
|
||||
inkscape:document-units="px"
|
||||
inkscape:current-layer="layer1"
|
||||
showgrid="false"
|
||||
inkscape:window-width="1097"
|
||||
inkscape:window-height="1068"
|
||||
inkscape:window-width="2079"
|
||||
inkscape:window-height="1458"
|
||||
inkscape:window-x="214"
|
||||
inkscape:window-y="23"
|
||||
inkscape:window-maximized="0"
|
||||
fit-margin-top="0"
|
||||
fit-margin-left="0"
|
||||
fit-margin-right="0"
|
||||
fit-margin-bottom="0" />
|
||||
fit-margin-bottom="0"
|
||||
inkscape:showpageshadow="2"
|
||||
inkscape:pagecheckerboard="0"
|
||||
inkscape:deskcolor="#d1d1d1" />
|
||||
<metadata
|
||||
id="metadata7">
|
||||
<rdf:RDF>
|
||||
|
|
@ -135,7 +139,6 @@
|
|||
<dc:format>image/svg+xml</dc:format>
|
||||
<dc:type
|
||||
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
|
||||
<dc:title></dc:title>
|
||||
</cc:Work>
|
||||
</rdf:RDF>
|
||||
</metadata>
|
||||
|
|
|
|||
|
Before Width: | Height: | Size: 9.9 KiB After Width: | Height: | Size: 10 KiB |
|
|
@ -1,4 +1,10 @@
|
|||
# Overview {#mainpage}
|
||||
|
||||
> **Caution**:
|
||||
> The written part of this manual is a work in progress.
|
||||
> It will be updated as v1.0 approaches.
|
||||
> The reference section should be complete and accurate.
|
||||
|
||||
Box3D is a 3D rigid body simulation library for games. Programmers can
|
||||
use it in their games to make objects move in realistic ways and make
|
||||
the game world more interactive. From the game engine's point of view,
|
||||
|
|
@ -14,6 +20,7 @@ engine begin with the `b3` prefix. Hopefully this is sufficient to avoid
|
|||
name clashing with your application.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
In this manual I'll assume you are familiar with basic physics
|
||||
concepts, such as mass, force, torque, and impulses. If not, please
|
||||
first consult Google search and Wikipedia.
|
||||
|
|
@ -33,6 +40,7 @@ should be comfortable with compiling, linking, and debugging.
|
|||
> Box3D. There are many resources for this online.
|
||||
|
||||
## Scope
|
||||
|
||||
This manual covers the majority of the Box3D API. However, not every
|
||||
aspect is covered. Please look at the Reference section and samples
|
||||
application included with Box3D to learn more.
|
||||
|
|
@ -45,6 +53,7 @@ Box3D may be out of sync with this manual.
|
|||
> latest version on the main branch.
|
||||
|
||||
## Feedback and Bugs
|
||||
|
||||
Please file bugs and feature requests here:
|
||||
[Box3D Issues](https://github.com/erincatto/box3d/issues)
|
||||
|
||||
|
|
|
|||
|
|
@ -704,22 +704,25 @@ instead of bodies. Since you can attach multiple shapes to a body, this allows f
|
|||
possible setups. For example, you can make a vehicle that is heavier in the back.
|
||||
|
||||
### Density
|
||||
|
||||
The shape density is used to compute the mass properties of the parent
|
||||
body. The density can be zero or positive. You should generally use
|
||||
similar densities for all your shapes. This will improve stacking
|
||||
stability.
|
||||
|
||||
The mass of a body is not adjusted when you set the density. You must
|
||||
call `b3Body_ApplyMassFromShapes()` for this to occur. Generally you should establish
|
||||
the shape density in `b3ShapeDef` and avoid modifying it later because this
|
||||
can be expensive, especially on a compound body.
|
||||
You may adjust the density of an existing body. You may choose to update the body mass immediately
|
||||
or defer for a later call to `b3Body_ApplyMassFromShapes()`. Generally you should establish
|
||||
the shape density in `b3ShapeDef` and avoid modifying it later because this can be expensive,
|
||||
especially on a body with many shapes.
|
||||
|
||||
```c
|
||||
b3Shape_SetDensity(myShapeId, 5.0f, true);
|
||||
bool updateMass = false;
|
||||
b3Shape_SetDensity(myShapeId, 5.0f, updateMass);
|
||||
b3Body_ApplyMassFromShapes(myBodyId);
|
||||
```
|
||||
|
||||
### Friction
|
||||
|
||||
Friction is used to make objects slide along each other realistically.
|
||||
Box3D supports static and dynamic friction, but uses the same parameter
|
||||
for both. Box3D attempts to simulate friction accurately and the friction
|
||||
|
|
@ -745,6 +748,7 @@ shapeDef.baseMaterial.friction = 0.5f;
|
|||
```
|
||||
|
||||
### Restitution
|
||||
|
||||
[Restitution](https://en.wikipedia.org/wiki/Coefficient_of_restitution) is used to make
|
||||
objects bounce. The restitution value is
|
||||
usually set to be between 0 and 1. Consider dropping a ball on a table.
|
||||
|
|
@ -772,6 +776,7 @@ shapeDef.baseMaterial.restitution = 0.3f;
|
|||
```
|
||||
|
||||
### Friction and Restitution Callbacks
|
||||
|
||||
Advanced users can override friction and restitution mixing using `b3FrictionCallback`
|
||||
and `b3RestitutionCallback`. These should be very lightweight functions because they
|
||||
are called frequently. The callbacks receive the two friction (or restitution) values
|
||||
|
|
@ -794,6 +799,7 @@ worldDef.frictionCallback = MyFrictionCallback;
|
|||
```
|
||||
|
||||
### Filtering {#filtering}
|
||||
|
||||
Collision filtering allows you to efficiently prevent collision between shapes.
|
||||
For example, say you make a character that rides a bicycle. You want the
|
||||
bicycle to collide with the terrain and the character to collide with
|
||||
|
|
@ -866,6 +872,7 @@ Collision groups have a higher priority than categories and masks.
|
|||
|
||||
Note that additional collision filtering occurs automatically in Box3D. Here is a
|
||||
list:
|
||||
|
||||
- A shape on a static body can only collide with a dynamic body.
|
||||
- A shape on a kinematic body can only collide with a dynamic body.
|
||||
- Shapes on the same body never collide with each other.
|
||||
|
|
@ -878,6 +885,7 @@ an existing shape using `b3Shape_GetFilter()` and
|
|||
it causes contacts to be destroyed.
|
||||
|
||||
### Sensors
|
||||
|
||||
Sometimes game logic needs to know when two shapes overlap yet there
|
||||
should be no collision response. This is done by using sensors. A sensor
|
||||
is a shape that detects overlap but does not produce a response.
|
||||
|
|
|
|||
Loading…
Reference in a new issue