We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOther Libraries › Detecting Mouse Events in Toxiclib Verlet Physics
Page Index Toggle Pages: 1
Detecting Mouse Events in Toxiclib Verlet Physics (Read 2001 times)
Detecting Mouse Events in Toxiclib Verlet Physics
Feb 16th, 2010, 7:53am
 
I tried searching for this and wasn't really able to find anything on the subject matter. I was wondering what the best way was to go about detecting mouse events on a VerletParticle2D...

From the example packaged with the library seems like you could make a Constraint that is mouseX and mouseY? I'm kind of at a loss though because I don't understand how to make a Constraint that isn't a Sphere. How do you specify points?

I'm sure there are other approaches...?
Re: Detecting Mouse Events in Toxiclib Verlet Physics
Reply #1 - Feb 19th, 2010, 1:04pm
 
Hey, sorry for the slow response... Am not sure i understand your question correctly.. you want to create a constraint based on mouse coordinates

The ParticleConstraint interface of the engine is (as the name hints at) meant to enforce some form of limitation on a particle's movement without prescribing what that limitation would be. The SphereConstraint you're talking about is just one concrete example of this, but the latest release (verletphysics-0006) from a few days ago, has added several more implementations of other limiting factors and you're welcome to take a look at their source code over here:

http://code.google.com/p/toxiclibs/source/browse/#svn/trunk/toxiclibs/src.physics/toxi/physics/constraints

The new release also includes the BoxConstraint demo, which uses axis-aligned bounding boxes to define an impenetrable volume for particles and forces them to slide over...

So with this all said, I still don't understand (know enough about) your plans to have a constraint based on mouse coordinates. Can you explain some more what you're trying to do
Re: Detecting Mouse Events in Toxiclib Verlet Physics
Reply #2 - Feb 22nd, 2010, 8:46am
 
Thanks for the response Toxi. I'm sorry I wasn't clear enough. I have a lot of VerletPhysics2D objects and I wanted to be able to click on a particular thread and drag it to move the whole thread around the screen.

From the documentation of the previous version I theorized that maybe making a Constraint was the way to do this, but from what you say now perhaps this is not the right way to do this...



Thanks for linking the repository and exciting to hear there's a new update! I tried using it but I get an error that reads  "NoSuchMethodError: You're probably using a library that's incompatible with this version of Processing." What version are you running I'm on 1.0.9 which I believe is the most up to date version.

You can see what I'm doing on flickr: http://www.flickr.com/photos/jonobr1/sets/72157623362181281/

or download the app directly at:
http://jonobr1.com/code/HRCL.zip

If you like it, maybe you'll reconsider my application to Universal Everything http://portfolio.jonobr1.com/123188/Universal-Everything-Application  Wink haha
Re: Detecting Mouse Events in Toxiclib Verlet Physics
Reply #3 - Feb 22nd, 2010, 12:01pm
 
Quote:
I have a lot of VerletPhysics2D objects and I wanted to be able to click on a particular thread and drag it to move the whole thread around the screen.


Here's a little demo which hopefully clears up any misunderstandings. Constraints are the wrong thing to think of in this case and it's all much more simple...

Code:
/**
* Draggable particle demo using verletphysics-0006 or newer
* http://toxiclibs.org
*
* Copyright (c) 2010 Karsten Schmidt
* Licensed under GNU LGPL v2.1
* http://creativecommons.org/licenses/LGPL/2.1/
*
*/

import toxi.geom.*;
import toxi.physics2d.*;

VerletPhysics2D physics;
VerletParticle2D selected=null;

// squared snap distance for picking particles
float snapDist=10*10;

void setup() {
 size(600,600);
 physics=new VerletPhysics2D();
 physics.setWorldBounds(new Rect(0,0,width,height));
 // create 10 particle strings of 20 particles each
 for(int i=0; i<10; i++) {
   ParticleString2D s=new ParticleString2D(physics,new Vec2D(width/2,height/2),Vec2D.randomVector().scaleSelf(10),20,1,0.5);
 }
}

void draw() {
 background(255);
 noFill();
 physics.update();
 // draw all springs
 for(Iterator i=physics.springs.iterator(); i.hasNext();) {
   VerletSpring2D s=(VerletSpring2D)i.next();
   line(s.a.x,s.a.y,s.b.x,s.b.y);
 }
 // draw all particles
 for(Iterator i=physics.particles.iterator(); i.hasNext();) {
   VerletParticle2D p=(VerletParticle2D)i.next();
   // selected particle in cyan, all others in black
   stroke(p==selected ? 0xff00ffff : 0xff000000);
   ellipse(p.x,p.y,5,5);
 }
}

// check all particles if mouse pos is less than snap distance
void mousePressed() {
 selected=null;
 Vec2D mousePos=new Vec2D(mouseX,mouseY);
 for(Iterator i=physics.particles.iterator(); i.hasNext();) {
   VerletParticle2D p=(VerletParticle2D)i.next();
   // if mouse is close enough, keep a reference to
   // the selected particle and lock it (becomes unmovable by physics)
   if (p.distanceToSquared(mousePos)<snapDist) {
     selected=p;
     selected.lock();
     break;
   }
 }
}

// only react to mouse dragging events if we have a selected particle
void mouseDragged() {
 if (selected!=null) {
   selected.set(mouseX,mouseY);
 }
}

// if we had a selected particle unlock it again and kill reference
void mouseReleased() {
 if (selected!=null) {
   selected.unlock();
   selected=null;
 }
}


Quote:
Thanks for linking the repository and exciting to hear there's a new update! I tried using it but I get an error that reads  "NoSuchMethodError: You're probably using a library that's incompatible with this version of Processing." What version are you running? I'm on 1.0.9 which I believe is the most up to date version.


This is very weird and I'd appreciate if you could head over to the link below to file a proper ticket and let me know as many details as possible (i.e. which version of the libs you've downloaded, your OS version, Java version etc.). Thanks!

http://code.google.com/p/toxiclibs/issues/entry

Quote:
If you like it, maybe you'll reconsider my application to Universal Everything


Will take a proper look at it your project later, but I'm not an employee of UE, only a collaborator! Smiley
Re: Detecting Mouse Events in Toxiclib Verlet Physics
Reply #4 - Feb 23rd, 2010, 5:24am
 
That little demo is far more than enough to illustrate exactly what I wanted to do. Thank you so much! I think this should be packaged with the other examples. Juxtaposed with the Box Constraint Demo has made it much more clear to me the difference.

As for the error, I had only updated VerletPhysics and had forgotten the others so everything works fine now. Again, thanks so much!  Grin
Re: Detecting Mouse Events in Toxiclib Verlet Physics
Reply #5 - Feb 23rd, 2010, 2:15pm
 
Cool! Glad it was helpful & your problem evaporated! With this last release I've started preparing a "mega" package which bundles the latest versions of all maintained packages. For the future, it'll be best to download this...
Page Index Toggle Pages: 1