VerletPhysics2D particles and behaviors management

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!

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. :brain:

We know the container VerletPhysics2D::behaviors:
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

And inside each ParticleBehavior2D there’s a Vec2D object stored in it:
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] or [u]ConstantForceBehavior2D[/u], (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: :tired_face:

  1. AttractionBehavior
  2. ConstantForceBehavior
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;
}

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…

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!

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

Class VerletPhysics2D contains 3 lists:
ToxicLibs.org/docs/verletphysics/toxi/physics2d/VerletPhysics2D.html

  1. VerletPhysics2D::behaviors: VerletPhysics2D
  2. VerletPhysics2D::particles: VerletPhysics2D
  3. VerletPhysics2D::springs: VerletPhysics2D

And when we call VerletPhysics2D::update():
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.

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)

Thus class VerletParticle2D also got a behaviors container:
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()

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!