Skip to content

Commit

Permalink
Allow a maximum distance to be set from a barrier
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanhogg committed Feb 13, 2025
1 parent e09a3a3 commit 5e78088
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
8 changes: 5 additions & 3 deletions docs/physics.md
Original file line number Diff line number Diff line change
Expand Up @@ -487,13 +487,15 @@ mean all of the particle's velocity is absorbed.

- `restitution` - the coefficient of restitution (default is `1`)

The `!barrier` node also accepts the same `'min`, `power`, `strength` and
`ease` attributes as `!distance`, which will create a minimum distance
The `!barrier` node also accepts the same `'minimum`, `maximum`, `power`,
`strength` and `ease` attributes as `!distance`, which will create a distance
constraint on all particles with respect to the barrier. This can be used to
create a "soft" boundary. The "hard" bounce condition is still applied if the
particle crosses the barrier.

- `minimum` (or `min`) - a minimum distance that particles must be from the
- `minimum` (or `min`) - a minimum distance that particles may be from the
barrier
- `maximum` (or `max`) - a maximum distance that particles may be from the
barrier
- `power` - the power to which the displacement will be raised (default is `1`)
- `strength` - force magnitude coefficient (default is `1`)
Expand Down
9 changes: 9 additions & 0 deletions src/flitter/render/physics.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ cdef class Barrier:
cdef double strength
cdef double power
cdef double minimum
cdef double maximum

@cython.profile(False)
def __cinit__(self, Node node, double strength, Vector zero):
Expand All @@ -111,6 +112,7 @@ cdef class Barrier:
self.strength = strength
self.power = max(0, node.get_float('power', 1))
self.minimum = node.get_float('minimum', node.get_float('min', 0))
self.maximum = node.get_float('maximum', node.get_float('max', 0))

@cython.profile(False)
cdef void apply_distance(self, Particle particle) noexcept nogil:
Expand All @@ -128,6 +130,13 @@ cdef class Barrier:
k *= self.strength
for i in range(dimensions):
particle.force.numbers[i] = particle.force.numbers[i] + self.normal.numbers[i] * k
elif self.maximum and distance > self.maximum:
k = distance - self.maximum
if self.power != 1:
k **= self.power
k *= self.strength
for i in range(dimensions):
particle.force.numbers[i] = particle.force.numbers[i] - self.normal.numbers[i] * k

@cython.profile(False)
cdef void apply_rebound(self, Particle particle, double speed_of_light, double clock, double delta) noexcept nogil:
Expand Down

0 comments on commit 5e78088

Please sign in to comment.