# VerletPhysics2D particles and behaviors management

**URL:** <https://discourse.processing.org/t/verletphysics2d-particles-and-behaviors-management/8523>\
**Category:** Libraries\
**Created:** [February 19, 2019, 2:48am UTC](https://discourse.processing.org/t/verletphysics2d-particles-and-behaviors-management/8523 "2019-02-19T02:48:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![billhsu](https://avatars.discourse-cdn.com/v4/letter/b/71e660/32.png) [@billhsu](https://discourse.processing.org/u/billhsu)\
**Post date:** [February 19, 2019, 2:48am UTC](https://discourse.processing.org/t/verletphysics2d-particles-and-behaviors-management/8523/1 "2019-02-19T02:48:43Z")

</div>

I’ve been using VerletPhysics2D for a project, and have some questions about interactions between particles and behaviors.

I’ve studied the VerletPhysics example Attraction2D. When a particle p is added, to prevent collisions, a new AttractionBehavior is also added to the “world” physics; from the example:

physics.addBehavior(new AttractionBehavior(p, 20, -1.2f, 0.01f));

So this “force field” follows the particle p as it moves, which is what we want. (The example doesn’t add the same behavior to the particle p.) Hence, when I check the list of behaviors for p, p.behaviors.size() is zero. (But physics.behaviors.size() includes the new AttractionBehavior, of course.)

In this case, when I look at the object p, is there a way to figure out that it has associated with it this AttractionBehavior? Without adding code that adds the same AttractionBehavior to p?

I’m a bit confused; the AttractionBehavior force field is clearly in physics (I can see it when I check physics.behaviors), but I don’t seem to be able to see the AttractionBehavior when I look at particle p.

(Reason why I’m interested in this detail: my project involves multiple clusters of particles and behaviors that are dynamically created. It’s helpful for me to be able to keep track of particles/behaviors, grouped according to the clusters they belong to. I can do this manually with extra lists, but am wondering if there’s a more elegant way just through the library.)

Thanks!

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [February 19, 2019, 5:32am UTC](https://discourse.processing.org/t/verletphysics2d-particles-and-behaviors-management/8523/2 "2019-02-19T05:32:37Z")

</div>

> [@billhsu](#):
>
> When I look at the object _p_, is there a way to figure out that it has associated with it this AttractionBehavior?

A Java object isn’t “aware” whether its reference is passed around or how many variables and containers currently got its reference stored in them. 🧠

> [@billhsu](#):
>
> The AttractionBehavior force field is clearly in _physics_ (I can see it when I check _physics.behaviors_), but I don’t seem to be able to see the AttractionBehavior when I look at particle _p_.

We know the container VerletPhysics2D::_behaviors_:  
[ToxicLibs.org/docs/verletphysics/toxi/physics2d/VerletPhysics2D.html#behaviors](http://ToxicLibs.org/docs/verletphysics/toxi/physics2d/VerletPhysics2D.html#behaviors)

Contains all the ParticleBehavior2D object references associated w/ it:  
[ToxicLibs.org/docs/verletphysics/toxi/physics2d/behaviors/ParticleBehavior2D.html](http://ToxicLibs.org/docs/verletphysics/toxi/physics2d/behaviors/ParticleBehavior2D.html)

And inside each ParticleBehavior2D there’s a Vec2D object stored in it:  
[ToxicLibs.org/docs/core/toxi/geom/Vec2D.html](http://ToxicLibs.org/docs/core/toxi/geom/Vec2D.html)

Therefore, in order to check whether a particular Vec2D object is associated w/ a VerletPhysics2D engine as a ParticleBehavior2D, we’re gonna have to iterate over the container VerletPhysics2D::_behaviors_, check whether each ParticleBehavior2D stored element is an `instanceof` either class [[u]AttractionBehavior2D[/u]](http://toxiclibs.org/docs/verletphysics/toxi/physics2d/behaviors/AttractionBehavior.html) or [[u]ConstantForceBehavior2D[/u]](http://toxiclibs.org/docs/verletphysics/toxi/physics2d/behaviors/ConstantForceBehavior.html), `(cast)` it to its actual datatype, and then invoke the corresponding method to grab its stored Vec2D behavior particle, and finally compare whether that’s the Vec2D we’re looking for and prematurely `return true;` if it is so: 😫

1. [AttractionBehavior](http://ToxicLibs.org/docs/verletphysics/toxi/physics2d/behaviors/AttractionBehavior.html#getAttractor())
2. [ConstantForceBehavior](http://ToxicLibs.org/docs/verletphysics/toxi/physics2d/behaviors/ConstantForceBehavior.html#getForce())

```java
import toxi.physics2d.VerletPhysics2D;
import toxi.physics2d.behaviors.ParticleBehavior2D;
import toxi.physics2d.behaviors.AttractionBehavior2D;
import toxi.physics2d.behaviors.ConstantForceBehavior2D;
import toxi.geom.Vec2D;

static final boolean amIBehavior(final Vec2D p, final VerletPhysics2D phy) {
  for (final ParticleBehavior2D pb : phy.behaviors)
    if (pb instanceof AttractionBehavior2D) {
      if (((AttractionBehavior2D) pb).getAttractor() == p) return true;
    } else if (((ConstantForceBehavior2D) pb).getForce() == p) return true;
  return false;
}

```

---

<div class="post-metadata">

**Author:** ![billhsu](https://avatars.discourse-cdn.com/v4/letter/b/71e660/32.png) [@billhsu](https://discourse.processing.org/u/billhsu)\
**Post date:** [February 20, 2019, 2:24am UTC](https://discourse.processing.org/t/verletphysics2d-particles-and-behaviors-management/8523/3 "2019-02-20T02:24:20Z")

</div>

Thanks for the detailed response, GoToLoop!

Hmm, looks like it might be easier to manually add the behavior to the associated particle’s list of behaviors. Will probably try that for now…

---

<div class="post-metadata">

**Author:** ![billhsu](https://avatars.discourse-cdn.com/v4/letter/b/71e660/32.png) [@billhsu](https://discourse.processing.org/u/billhsu)\
**Post date:** [February 21, 2019, 2:05am UTC](https://discourse.processing.org/t/verletphysics2d-particles-and-behaviors-management/8523/4 "2019-02-21T02:05:16Z")

</div>

I’ve been trying to make some minimal examples to understand better the interactions between particles and behaviors. If there’s a document I should read first on particle/behavior interaction, other than the javadocs and Daniel Shiffman’s book (which I’ve studied), please point me to it! I haven’t been able to find much else.

Here’s another question for GoToLoop and other VerletPhysics experts.

I make an instance of the VerletPhysics2D engine (no gravity), add a particle to it.

physics=new VerletPhysics2D();  
VerletParticle2D p = new VerletParticle2D(width/2, height/2);  
physics.addParticle§;

The particle doesn’t move on its own, as expected.

I make an AttractionBehavior, connected with the particle, and set it to repel.

ParticleBehavior2D pb = new AttractionBehavior(p, 15, -1.f, 0.01f);

To activate the behavior, I add it to the VerletPhysics2D instance:

physics.addBehavior(pb);

Now the particle starts drifting on its own, pushed by the behavior (I assume). This is pretty much the setup from the Attraction2D tutorial.

However, if I replace physics.addBehavior(pb); with

p.addBehavior(pb);

the particle also drifts. pb is not on the list of behaviors when I check the physics engine instance.

So the VerletPhysics2D engine updates all particles on its particles list, and also all behaviors contained in the behaviors list for each particle? Are there differences between the activity of a behavior, associated with a particle, that’s 1) on the behaviors list for a particle only, 2) on the behaviors list for the engine only, or 3) on both behaviors lists?

Thanks!

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [February 21, 2019, 7:13am UTC](https://discourse.processing.org/t/verletphysics2d-particles-and-behaviors-management/8523/5 "2019-02-21T07:13:51Z")

</div>

> [@billhsu](#):
>
> Here’s another question for GoToLoop and other VerletPhysics experts.

I’m no expert n 3D stuff at all! Most I can do is to attempt to help on Java-side questions. ☕

> [@billhsu](#):
>
> So the VerletPhysics2D engine updates all particles on its particles list, and also all behaviors contained in the behaviors list for each particle?

Class VerletPhysics2D contains 3 lists:  
[ToxicLibs.org/docs/verletphysics/toxi/physics2d/VerletPhysics2D.html](http://ToxicLibs.org/docs/verletphysics/toxi/physics2d/VerletPhysics2D.html)

1. VerletPhysics2D::_behaviors_: [VerletPhysics2D](http://ToxicLibs.org/docs/verletphysics/toxi/physics2d/VerletPhysics2D.html#behaviors)
2. VerletPhysics2D::_particles_: [VerletPhysics2D](http://ToxicLibs.org/docs/verletphysics/toxi/physics2d/VerletPhysics2D.html#particles)
3. VerletPhysics2D::_springs_: [VerletPhysics2D](http://ToxicLibs.org/docs/verletphysics/toxi/physics2d/VerletPhysics2D.html#springs)

And when we call VerletPhysics2D::**update()**:  
[ToxicLibs.org/docs/verletphysics/toxi/physics2d/VerletPhysics2D.html#update()](http://ToxicLibs.org/docs/verletphysics/toxi/physics2d/VerletPhysics2D.html#update())

We should expect that each VerletPhysics2D object inside all those 3 List containers to participate in the updating action.

> [@billhsu](#):
>
> However, if I replace `physics.addBehavior(pb);` with `p.addBehavior(pb);`, the particle also drifts.

If _pb_ wasn’t registered in any of VerletPhysics2D’s 3 List containers, invoking VerletPhysics2D::**update()** should not affect _p_.

But if you can use `p.addBehavior(pb);`, I’m assuming variable _p_ is of datatype VerletParticle2D:  
[ToxicLibs.org/docs/verletphysics/toxi/physics2d/VerletParticle2D.html#addBehavior(toxi.physics2d.behaviors.ParticleBehavior2D)](http://ToxicLibs.org/docs/verletphysics/toxi/physics2d/VerletParticle2D.html#addBehavior(toxi.physics2d.behaviors.ParticleBehavior2D))

Thus class VerletParticle2D also got a _behaviors_ container:  
[ToxicLibs.org/docs/verletphysics/toxi/physics2d/VerletParticle2D.html#behaviors](http://ToxicLibs.org/docs/verletphysics/toxi/physics2d/VerletParticle2D.html#behaviors)

If _p_ is moving, I’m also assuming you’re invoking its method VerletParticle2D::**update()**, right?  
[ToxicLibs.org/docs/verletphysics/toxi/physics2d/VerletParticle2D.html#update()](http://ToxicLibs.org/docs/verletphysics/toxi/physics2d/VerletParticle2D.html#update())

In such case, the ParticleBehavior2D _pb_ would only affect this particular VerletParticle2D _p_ object, w/o affecting those registered in VerletPhysics2D _physics_.

Although it’s a lil’ strange, considering that the AttractionBehavior2D _pb_ object was created passing the VerletParticle2D _p_ object itself as its argument!

So the VerletParticle2D _p_ got an AttractionBehavior2D object inside its _bevaviors_ List which relies on _p_ itself as the AttractionBehavior2D’s Vec2D _attractor_ internal field!
