#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# How to give individual particles a repulsion force using toxiclibs ?

edited February 2018

Hi,

I would like to give particles a repulsion force from each other that is proportionnal to distance (inverted gravity) using toxiclibs. How can I do so ?

Consider the code below:

``````add_library('toxiclibscore')

from toxi.physics2d import VerletPhysics2D

from particle import Particle

def setup():
global particules, n_particle, physics
size(800, 600)

physics = VerletPhysics2D()
particules = []
n_particle = 10

for e in range(n_particle):
angle = TWO_PI  / n_particle
x = radius * sin(angle * e) + map(noise(random(-20, 20)), 0, 1, -20, 120)
y = radius * cos(angle * e)
particules.append(Particle(width / 2 + x, height / 2 + y))

for e in particules:

for e in range(n_particle - 1):
a = particules[e]
b = particules[e+1]
s = VerletSpring2D(a, b, 40, .2)

a = particules[-1]   //// connect the last particle to the first making a loop
b = particules
s = VerletSpring2D(a, b, 40, .2)

def draw():
global particules
background(0)

for e in particules:
e.display()

physics.update()
``````

In setup() if I add something like:

``````for e in particules:
for f in particules:
if e != f:
force = PVector.sub(e.location, f.location)
distance = force.mag()
physics.addBehavior(AttractionBehavior(f, 10, -1 / distance ))
``````

... the behavior will be fixed and won't change over time. What I need is a repulsive force that constantly changes depending on the varying distance between 2 particles. I tried to put that snippet in draw() but the forces kept accumulating and I couldn't figure out how to cancel them at each iteration.

Any idea how I can implement this kind of adaptative behavior with toxiclibs ?

Tagged:

• isn't this the same as your other question?

• How to give individual particles a repulsion force using toxiclibs ?
• How to apply a repulsion force to a particle system ?

if you have multiple questions you'll split the answers and confuse everybody. better to keep it focused in one place.

• @koogs No it not the same question ! One was about applying a repulsion force to a particle system without using any library and the new one is about giving a repulsion force to particles individually and using the toxiclibs library. All this was well explained in the question you deleted.

• @koogs This question is NOT answered ! Please stop deleting or avoiding my questions, you're wasting my time and my energy. I'm putting a lot of effort trying to explain the problems I'm facing in a language that is not my mother tongue.

It's been almost a week that Im trying to make a blob moving in Processing and nobody cares about my questions.

• I sent an email to Zachary Lieberman (told me he got no time to answer)

This forum is all I have, so please read carefully what I wrote before deciding to remove my questions. You'll see that every question I asked is different as I'm trying different approaches. And I'm trying different approache because no one answered my original questions.

• One was about applying a repulsion force to a particle system... the new one is about giving a repulsion force to particles...

how is this NOT the same thing?

your main problem here is this:

Python Mode

this cuts that remaining 10% by 90%

and again.

• @koogs: Applying a fixed repulsion force to a whole particle system is not the same thing as applying a changing force to particles individually. Not to mention that one question was about doing it without library, and the other one with the toxiclibs library. I perfectly understand that very few use the Python mode and that even less people understand how the toxiclibs library works. That's why I asked the help of GoToLoop who, unfortunately for me, doesn't seem to be interested.

• Problem is I'm not good at 3D stuff. Neither the Toxic library. X_X
• And although I'm good at converting a Java Mode sketch to Python Mode (and other flavors), I'm not particular good at the Python language itself! @-)
• In order to help you out previously, I've watched that Shifman's Cloth Challenge. =P~
• Then I had to peruse those 5 classes: VerletPhysics2D, GravityBehavior, VerletParticle2D, VerletSpring2D & Vec2D source code in order to firmly grasp how they work.
• In other words, I had learn the API of those particular 5 classes. #:-S
• While I was at it, it seemed to me that a VerletParticle2D object can have independent behavior, apart from VerletPhysics2D. :-?
• But given I'm still very noob on that library, you should take a look at it as well: O:-)
http://ToxicLibs.org/docs/verletphysics/toxi/physics2d/VerletParticle2D.html
• And also at its complete API reference docs: :-B
http://ToxicLibs.org/docs/core/
http://ToxicLibs.org/docs/verletphysics/
• @GoToLoop Thank you for taking the time to comment my question. I understand you did a lot of research to help me and I'm truely thankful to you for that.

I've read the API and particularly the 5 classes you mentionned, many times, again and again, but couldn't make much sense of it to be honest. It seems quite difficult (near impossible ?) to have some control on a toxiclibs behavior as the sketch is iterating.

All I've managed to do is adding a static repulsion + attraction force beforehand.

https://media.giphy.com/media/1k2wspz8guv3PZ7dYq/giphy.gif

But, again, the repulsion force should be proportional to distance thus changing over time, as particles get closer or farther at each iteration.

Although you're not familiar with the toxiclibs library, if you have any idea/supposition on how I can make that repulsion force work (with or without toxiclibs), I would really love to hear it.

^:)^

``````add_library('toxiclibscore')

from toxi.physics2d import VerletPhysics2D
from particle import Particle

def setup():
global particules, n_particle, physics
size(800, 600)

physics = VerletPhysics2D()
particules = []
n_particle = 120

for e in range(n_particle):
angle = TWO_PI  / n_particle
x = radius * sin(angle * e)
y = radius * cos(angle * e)
particules.append(Particle(width / 2 + x, height / 2 + y))

for e in particules:
for f in particules:
if e != f:

for e in range(n_particle - 1):
a = particules[e]
b = particules[e+1]
s = VerletSpring2D(a, b, 8, .3)

a = particules[-1]
b = particules
s = VerletSpring2D(a, b, 8, .3)

def draw():
global particules
background(0)

beginShape()
for i, e in enumerate(particules):
fill(235, 130, 159)
e.display()
endShape()

physics.update()
``````

Particle class

``````from toxi.physics2d import VerletParticle2D

class Particle(VerletParticle2D):

def __init__(p, x, y):
super(Particle, p).__init__(x, y)
p.location = PVector(x, y)

def display(p):
noStroke()
vertex(p.x(), p.y())
``````
• Anyone ?