Skip to content

Commit 5e78088

Browse files
committed
Allow a maximum distance to be set from a barrier
1 parent e09a3a3 commit 5e78088

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

docs/physics.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -487,13 +487,15 @@ mean all of the particle's velocity is absorbed.
487487

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

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

496-
- `minimum` (or `min`) - a minimum distance that particles must be from the
496+
- `minimum` (or `min`) - a minimum distance that particles may be from the
497+
barrier
498+
- `maximum` (or `max`) - a maximum distance that particles may be from the
497499
barrier
498500
- `power` - the power to which the displacement will be raised (default is `1`)
499501
- `strength` - force magnitude coefficient (default is `1`)

src/flitter/render/physics.pyx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ cdef class Barrier:
102102
cdef double strength
103103
cdef double power
104104
cdef double minimum
105+
cdef double maximum
105106

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

115117
@cython.profile(False)
116118
cdef void apply_distance(self, Particle particle) noexcept nogil:
@@ -128,6 +130,13 @@ cdef class Barrier:
128130
k *= self.strength
129131
for i in range(dimensions):
130132
particle.force.numbers[i] = particle.force.numbers[i] + self.normal.numbers[i] * k
133+
elif self.maximum and distance > self.maximum:
134+
k = distance - self.maximum
135+
if self.power != 1:
136+
k **= self.power
137+
k *= self.strength
138+
for i in range(dimensions):
139+
particle.force.numbers[i] = particle.force.numbers[i] - self.normal.numbers[i] * k
131140

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

0 commit comments

Comments
 (0)