pymunk icon indicating copy to clipboard operation
pymunk copied to clipboard

Batch module

Open GuillaumeBroggi opened this issue 7 months ago • 3 comments

Following-up on #235

The new batch module is amazing and looks very promising. Only with this I can get at least a 50% running time improvement.

There is an error when using the arbiter distance with a space containing a sensor. For instance:

import pymunk, pymunk.batch

s = pymunk.Space()
b1 = pymunk.Body(1, 1)
b1.position = 1, 2

shape = pymunk.Circle(b1, 4)
shape.sensor = True
s.add(b1, shape)

b2 = pymunk.Body(1, 1)
b2.position = 3, 4
s.add(b2, pymunk.Circle(b2, 4))

s.step(1)
data = pymunk.batch.Buffer()
pymunk.batch.get_space_arbiters(
    s,
    pymunk.batch.ArbiterFields.DISTANCE_1 | pymunk.batch.ArbiterFields.BODY_B_ID,
    data,
)

raises

Aborting due to Chipmunk error: Index error: The specified contact index is invalid for this arbiter
        Failed condition: 0 <= i && i < cpArbiterGetCount(arb)
        Source:Chipmunk2D/src/cpArbiter.c:93

(Does it mean it would be possible to get arbiter information for sensors through this interface? That would also bring significant speed improvements instead of relying on pre_solve and separate callbacks to monitor sensors.)

If you are looking for more batching ideas, it could be nice to have:

  • batched position_func: I enforce a toroïdal space in my application by using simple maths on the body coordinates inside a position_func. However, the overhead of this callback is significant (almost 50% of the current running time).
  • batched addition of bodies
  • batched removal of bodies

GuillaumeBroggi avatar Nov 16 '23 15:11 GuillaumeBroggi