Skip to content

Commit

Permalink
Fix to support sensor shapes in batch api #238
Browse files Browse the repository at this point in the history
  • Loading branch information
viblo committed Nov 16, 2023
1 parent d38870b commit 3aaadbd
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 27 deletions.
10 changes: 9 additions & 1 deletion TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,17 @@ v6.x
v7+ (all potentially breaking changes)
---
- Think about split between pymunk.util and pymunk modules


Chipmunk improvements
---------------------
- Investigate the SPOOK solver, which could be much more stable than what is currently used. (but probably very difficult to switch to). https://github.com/viblo/pymunk/issues/216
- Investigate "Affine Body Dynamics Fast, Stable & Intersection-free Simulation of Stiff Materials"

- optimize step function:
- streamline build - benchmark
- https://www.microsoft.com/en-us/research/wp-content/uploads/2016/12/ccds.pdf
- collision detection:
- r-tree https://www.sebastiansylvan.com/post/r-trees--adapting-out-of-core-techniques-to-modern-memory-architectures/

Typing a existing project - learnings
-------------------------------------
Expand Down
38 changes: 38 additions & 0 deletions pymunk/tests/test_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,41 @@ def check_arb_data(arb: pymunk.Arbiter) -> None:
b1.each_arbiter(check_arb_data)
b2.each_arbiter(check_arb_data)
b3.each_arbiter(check_arb_data)

def test_get_arbiter_sensor(self):
s = pymunk.Space()

b1 = pymunk.Body(1, 1)
s1 = pymunk.Circle(b1, 40)
s.add(b1, s1)

b2 = pymunk.Body(1, 1)
b2.position = 1,0
s2 = pymunk.Poly.create_box(b2)
s2.sensor = True
s.add(b2, s2)

s.step(0.1)

data = pymunk.batch.Buffer()
pymunk.batch.get_space_arbiters(
s,
pymunk.batch.ArbiterFields.BODY_A_ID
| pymunk.batch.ArbiterFields.BODY_B_ID
| pymunk.batch.ArbiterFields.CONTACT_COUNT
| pymunk.batch.ArbiterFields.DISTANCE_1
| pymunk.batch.ArbiterFields.DISTANCE_2
| pymunk.batch.ArbiterFields.POINT_A_1
| pymunk.batch.ArbiterFields.POINT_A_2
| pymunk.batch.ArbiterFields.POINT_B_1
| pymunk.batch.ArbiterFields.POINT_B_2,
data,
)


self.assertEqual(list(memoryview(data.int_buf()).cast("P")), [b1.id, b2.id, 0])

self.assertEqual(
list(memoryview(data.float_buf()).cast("d")),
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
)
50 changes: 24 additions & 26 deletions pymunk_cffi/extensions.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,29 +63,6 @@ struct pmBatchedData
pmBatchableBodyFields fields;
};

// cpVectArray *
// cpVectArrayNew(int size)
// {
// cpVectArray *arr = (cpVectArray *)cpcalloc(1, sizeof(cpVectArray));

// arr->num = 0;
// arr->max = (size ? size : 4);
// arr->arr = (cpVect *)cpcalloc(arr->max, sizeof(cpVect));

// return arr;
// }

// void cpVectArrayFree(cpVectArray *arr)
// {
// if (arr)
// {
// cpfree(arr->arr);
// arr->arr = NULL;

// cpfree(arr);
// }
// }

pmFloatArray *
pmFloatArrayNew(int size)
{
Expand Down Expand Up @@ -234,15 +211,36 @@ void pmSpaceArbiterIteratorFuncBatched(cpArbiter *arbiter, void *data)
}
if (d->fields & POINT_A_1)
{
pmFloatArrayPushVect(d->floatArray, cpArbiterGetPointA(arbiter, 0));
if (cpArbiterGetCount(arbiter) > 0)
{
pmFloatArrayPushVect(d->floatArray, cpArbiterGetPointA(arbiter, 0));
}
else
{
pmFloatArrayPushVect(d->floatArray, cpv(0, 0));
}
}
if (d->fields & POINT_B_1)
{
pmFloatArrayPushVect(d->floatArray, cpArbiterGetPointB(arbiter, 0));
if (cpArbiterGetCount(arbiter) > 0)
{
pmFloatArrayPushVect(d->floatArray, cpArbiterGetPointB(arbiter, 0));
}
else
{
pmFloatArrayPushVect(d->floatArray, cpv(0, 0));
}
}
if (d->fields & DISTANCE_1)
{
pmFloatArrayPush(d->floatArray, cpArbiterGetDepth(arbiter, 0));
if (cpArbiterGetCount(arbiter) > 0)
{
pmFloatArrayPush(d->floatArray, cpArbiterGetDepth(arbiter, 0));
}
else
{
pmFloatArrayPush(d->floatArray, 0);
}
}
if (d->fields & POINT_A_2)
{
Expand Down

0 comments on commit 3aaadbd

Please sign in to comment.