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 & HelpOpenGL and 3D Libraries › 3d constraints with toxiclibs over a custom form
Page Index Toggle Pages: 1
3d constraints with toxiclibs over a custom form (Read 5023 times)
3d constraints with toxiclibs over a custom form
Dec 28th, 2009, 12:15am
 
dear all,

i am trying to use constraints over a custom area so that various particles/forms i am using cannot enter the area and they collide with it. i looked into toxiclib particleconstrain interface, but the only option it gave me was to constrain it in/out of a sphere, inside a worldbound - which is a box, and along a single axis.
ParticleConstraint sphere=new SphereConstraint(new Sphere(new Vec3D(110,111,230), 100), SphereConstraint.OUTSIDE);

is there any function that already exists and that can constrain particles from getting into a box or custom defined area or an OBJ file?

maybe this is possible with toxiclib, but i can't figure it out.
thanks
Re: 3d constraints with toxiclibs over a custom form
Reply #1 - Dec 28th, 2009, 4:48am
 
A mesh based ParticleConstraint implementation doesn't unfortunately exist so far, but is on the todo list... But because ParticleConstraints are defined as an interface only, you're able to create your own constraint logic, simply create a new class like shown below, implementing the apply() method:

Code:
class MyConstraint implements ParticleConstraint {
 public apply(VerletParticle p) {
   // constraint logic goes here
 }
}


To force a particle staying outside an axis aligned box you could then implement something like this:

Code:
class BoxConstraint implements ParticleConstraint {

 AABB box;
 Ray3D intersectRay;

 public BoxConstraint(Vec3D min, Vec3D max) {
   this(AABB.fromMinMax(min,max));
 }

 public BoxConstraint(AABB box) {
   this.box=box;
   this.intersectRay=new Ray3D();
 }

 public void apply(VerletParticle p) {
   // check if particle needs to be constrained
   if (p.isInAABB(box)) {
     // update intersection ray and compute position on bounding box
     intersectRay.dir=p.getInverted();
     p.set(box.intersectsRay(intersectRay,0,Float.MAX_VALUE));
   }
 }
}


Just add this class to your sketch, create an instance of it and add it as constraint to all particles you want to apply it to, for example:

Code:
// create a box from (-100,-100,-100) -> (100,100,100)
ParticleConstraint pc=new BoxConstraint(new Vec3D(-100,-100,-100), new Vec3D(100,100,100));
...
VerletParticle p = new VerletParticle(...);
p.addConstraint(pc);


Hope that helps!
Re: 3d constraints with toxiclibs over a custom form
Reply #2 - Dec 28th, 2009, 6:36am
 
wov, this seems so easy.. Smiley

thanks a million. it does what i need...

it would be great if there is a way to do this with obj. or stl files.... i will have a look at this.

thanks.

t
Re: 3d constraints with toxiclibs over a custom form
Reply #3 - Dec 28th, 2009, 11:52am
 
heh, a bit of geometry often helps wonders & having the right tools for that helps too... Wink

Btw. the above box constraint as is only works with boxes centred around (0,0,0). You'll have to replace the "intersectRay.dir=p.getInverted();" with the line below to make it generic and support all positions:

Code:
intersectRay.dir=box.sub(p).normalize(); 


As for the mesh collision detection, that is a lot more work (and more complicated), so I'll have to leave this until I really need it at some point... there're quite a few bits & bops I can extract from the Audi windtunnel project, but an efficient and more generic solution would also require a list of more optimized checks & data structures (convex hulls, spatial partitioning trees etc.)
Re: 3d constraints with toxiclibs over a custom form
Reply #4 - Dec 28th, 2009, 9:39pm
 
actually when i try to control this so that the threads are not getting into the box, it looks like it is working when i add this:

public void apply(VerletParticle p) {
   // check if particle needs to be constrained
   if (p.isInAABB(box)) {
     // update intersection ray and compute position on bounding box

   //  intersectRay.dir=p.getConstrained(box);
     intersectRay.dir=p.constrain(box);
  //   intersectRay.dir=p.getInverted();
  //  intersectRay.dir=box.sub(p).normalize();
     p.set(box.intersectsRay(intersectRay,0,Float.MAX_VALUE));

   }
 }


the problem is that my multiple threads then become huge, like there are no bounds of the scene. i am not sure why does this happen...

i dont need the scene constrains but i just want them to have the same properties like they had before.
the only thing that i need is to have all the threads not get into a custom box, that is e.g. 800X500X10, and that they don't change their physics, except the boxconstrain...

...

uf. i guess, i need to tinker more...
thanks anyways,



Re: 3d constraints with toxiclibs over a custom form
Reply #5 - Dec 29th, 2009, 3:31am
 
with regard to the constraint to an .obj mesh and using saito's objloader library, i came across the same issue a couple of weeks ago:

processing.org/discourse/yabb2/num_1257778833.html

in the end, i ran out of time before being able to resolve the 3D issue and resorted to a simplified geometry in which the test for interiority only referenced vertex sets aligned with the xy plane. this still allowed a great deal of complexity within the mesh in terms of the solid void relationship.

hope it helps.
Re: 3d constraints with toxiclibs over a custom form
Reply #6 - Dec 30th, 2009, 4:05am
 
hi tadr, sorry for the delay, but I also think you misunderstood some of the logic in the constraint logic... For example setting the direction of the intersection ray as you did using this line below MUST be a failure and doesn't make any sense.
Code:
intersectRay.dir=p.constrain(box); // WRONG! :( 


The constrain method is used to clip a point to stay within a given bounding box. Since you've already established in an earlier line above that the point is inside the box, then this method simply does nothing. But on top of that, the context of how you use this method is wrong, since you now also assign this point position as direction for the intersection test ray and this will produce chaotic results you're getting. Instead the direction should be relative to the box position, see my previous post...

Anyway, I've created a new example showing how to use the above BoxConstraint class properly, so hope this helps now for real Smiley
Code:
import processing.opengl.*;

import toxi.geom.*;
import toxi.physics.*;
import toxi.physics.constraints.*;

BoxConstraint[] boxes=new BoxConstraint[2];

int NUM_PARTICLES = 100;
int REST_LENGTH=10;

VerletPhysics physics;
VerletParticle head;

void setup() {
 size(1024,576,OPENGL);
 // create 2 boxes
 boxes[0]=new BoxConstraint(new Vec3D(100,-100,-100),new Vec3D(150,100,100));
 boxes[1]=new BoxConstraint(new Vec3D(-150,-100,-100),new Vec3D(-100,100,100));
 // setup physics engine
 physics=new VerletPhysics();
 physics.gravity=Vec3D.Y_AXIS.scale(0.02);
 VerletParticle prev=null;
 // create particle string and assign boxes as constraints to each particle
 for(int i=0; i<NUM_PARTICLES; i++) {
   VerletParticle p=new VerletParticle(new Vec3D((i-NUM_PARTICLES/2)*REST_LENGTH,-200,0));
   for(int j=0; j<boxes.length; j++) {
     p.addConstraint(boxes[j]);
   }
   physics.addParticle(p);
   if (prev!=null) {
     physics.addSpring(new VerletSpring(prev,p,REST_LENGTH, 0.5));
   }
   prev=p;
 }
 // anchor the 1st particle in space
 physics.particles.get(0).lock();
}

void draw() {
 physics.update();
 background(255);
 noFill();
 translate(width/2,height/2,0);
 rotateX(-0.33);
 rotateY(frameCount*0.01);
 // draw the box obstacles
 for(int i=0; i<boxes.length; i++) {
   boxes[i].draw();
 }
 // draw all springs
 beginShape(LINES);
 for(Iterator i=physics.springs.iterator(); i.hasNext();) {
   VerletSpring s=(VerletSpring)i.next();
   vertex(s.a.x,s.a.y,s.a.z); vertex(s.b.x,s.b.y,s.b.z);
 }
 endShape();
 for(Iterator i=physics.particles.iterator(); i.hasNext();) {
   VerletParticle p=(VerletParticle)i.next();
   ellipse(p.x,p.y,2,2);
 }
}

class BoxConstraint implements ParticleConstraint {

 public AABB box;
 protected Ray3D intersectRay;

 public BoxConstraint(Vec3D min, Vec3D max) {
   this(AABB.fromMinMax(min,max));
 }

 public BoxConstraint(AABB box) {
   this.box=box;
   this.intersectRay=new Ray3D(box,new Vec3D());
 }

 public void apply(VerletParticle p) {
   if (p.isInAABB(box)) {
     intersectRay.dir=box.sub(p).normalize();
     p.set(box.intersectsRay(intersectRay,0,Float.MAX_VALUE));
   }
 }

 public void draw() {
   Vec3D m=box.getMin();
   Vec3D n=box.getMax();
   beginShape(QUAD_STRIP);
   stroke(0);
   vertex(m.x,m.y,m.z); vertex(n.x,m.y,m.z);
   vertex(m.x,n.y,m.z); vertex(n.x,n.y,m.z);
   vertex(m.x,n.y,n.z); vertex(n.x,n.y,n.z);
   vertex(m.x,m.y,n.z); vertex(n.x,m.y,n.z);
   vertex(m.x,m.y,m.z); vertex(n.x,m.y,m.z);
   endShape();
 }
}
Re: 3d constraints with toxiclibs over a custom form
Reply #7 - Dec 30th, 2009, 11:05am
 
Dear toxi,

thanks a lot for your generous help. i think this will really do the trick...

i will integrate it in my sketch, and once i finish this project, i will keep you updated.

dear pip,

thanks for pointing me to that method.. i will need to investigate that more.

soon.
tx...
t
Re: 3d constraints with toxiclibs over a custom form
Reply #8 - May 11th, 2010, 5:57pm
 
Toxi, what version of toxiclibs are you using? I have 0018 and I get an error and have a question. The first being that physics.gravity is not visible. I can do physics.setGravity(); though. Secondly, what is a Ray3D object and where can I find that in the documentation? Is that part of Processing or Toxiclibs?

Thanks so much for sharing your knowledge, toxiclibs has really accelerated my understanding of programming.

I'm super butt hurt I couldn't attend the Processing Paris workshop...
Re: 3d constraints with toxiclibs over a custom form
Reply #9 - May 20th, 2010, 3:17am
 
Hey Jono,

Learning all this stuff myself!

check out the toxiclibs docs at "toxiclibs.org/javadocs/"

The Ray3D and VerletPhysics classes now use setter methods instead of letting you set their properties directly--which, I guess, is better object-oriented practice because the class can change significantly internally, but it will look the same to us.

In the sample sketch change the following:

Code:
physics.gravity=Vec3D.Y_AXIS.scale(0.02);
to
physics.setGravity(Vec3D.Y_AXIS.scale(0.02));

intersectRay.dir=box.sub(p).normalize();
to
intersectRay.setDirection(box.sub(p).normalize());


should work!

Thanks for the libs Toxi!


Re: 3d constraints with toxiclibs over a custom form
Reply #10 - May 20th, 2010, 4:02pm
 
Sorry for the delay, guys...

Nwallen is pretty much spot on with his reply already, so not much more to add. I'm not a particular fan of enforcing getters/setters everywhere, but I've noticed a few places where this pattern is indeed useful or even needed. E.g. for the Ray3D class, the reason for the setter was to be able to normalize the direction vector at this moment of setting, rather than every time the direction is needed for calculations (so you can actually pass in any vector now) - so this setter is actually speeding things up now too...

http://toxiclibs.org/javadocs/ is indeed the place to look for, but the docs are also included in all downloads (look inside "docs" folder the individual lib packages)

Lastly, in a workshop last weekend in Istanbul, we've quickly adapted the already bundled SoftBodySquare demo to create a 3D cloth simulation with obstacles. During that I've also noticed (once more) the above BoxConstraint logic is too simplistic and actually produces wrong results in many situations. So I've done some changes to the behaviour of this class which now casts the intersection ray from the particle position into the current direction of movement and checks the box surface...

The new demo with the updated BoxConstraint is here (but still undergoing further changes before getting included in the next release):

http://openprocessing.org/visuals/?visualID=9826

Hth!
Re: 3d constraints with toxiclibs over a custom form
Reply #11 - May 21st, 2010, 3:07pm
 
Wow, just when I thought toxiclibs couldn't get sweeter...

Thanks for the update Nwallen. That totally did the trick. Funny cause I changed the other methods to getter / setters, but somehow overlooked the fact that Ray3D would be the same.
Page Index Toggle Pages: 1