Toxi VerletPhysics - getting Particle to rest on a Attractor
in
Contributed Library Questions
•
2 years ago
Hello,
I've been working with Toxi's wonderful libraries and had a question regarding the VerletPhysics classes. Basically I want a particle attracted to a certain point via a AttractionBehavior. I can do this fine, but the problem is the Particle will never completely settle on the Attractor, it will just hover around it bouncing back and forth.
I modified one of the examples included in the VerletPhysics library to demo my issue. Click and Hold your mouse to attract the particle. You'll notice the particle never fully settles at your mouse location (if you are not moving the mouse :) but just jumps around. Is there anyway to use an AttractionBehavior and have particles settle?
Thanks!
I've been working with Toxi's wonderful libraries and had a question regarding the VerletPhysics classes. Basically I want a particle attracted to a certain point via a AttractionBehavior. I can do this fine, but the problem is the Particle will never completely settle on the Attractor, it will just hover around it bouncing back and forth.
I modified one of the examples included in the VerletPhysics library to demo my issue. Click and Hold your mouse to attract the particle. You'll notice the particle never fully settles at your mouse location (if you are not moving the mouse :) but just jumps around. Is there anyway to use an AttractionBehavior and have particles settle?
Thanks!
- /*
* Copyright (c) 2010 Karsten Schmidt
*
* This demo & library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* http://creativecommons.org/licenses/LGPL/2.1/
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
import toxi.geom.*;
import toxi.physics2d.*;
import toxi.physics2d.behaviors.*;
int NUM_PARTICLES = 1;
VerletPhysics2D physics;
AttractionBehavior mouseAttractor;
Vec2D mousePos;
void setup() {
size(680, 382,P3D);
physics = new VerletPhysics2D();
physics.setDrag(0.05f);
physics.setWorldBounds(new Rect(0, 0, width, height));
}
void addParticle() {
VerletParticle2D p = new VerletParticle2D(Vec2D.randomVector().scale(5).addSelf(width / 2, 0));
physics.addParticle(p);
}
void draw() {
background(255,0,0);
noStroke();
fill(255);
if (physics.particles.size() < NUM_PARTICLES) {
addParticle();
}
physics.update();
for (VerletParticle2D p : physics.particles) {
ellipse(p.x, p.y, 50, 50);
}
}
void mousePressed() {
mousePos = new Vec2D(mouseX, mouseY);
// create a new positive attraction force field around the mouse position (radius=250px)
mouseAttractor = new AttractionBehavior(mousePos, 250, 0.9f);
physics.addBehavior(mouseAttractor);
}
void mouseDragged() {
// update mouse attraction focal point
mousePos.set(mouseX, mouseY);
}
void mouseReleased() {
// remove the mouse attraction when button has been released
physics.removeBehavior(mouseAttractor);
}
1