day3 (german!) libs
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
import mathematik.*;
|
||||
|
||||
import processing.opengl.*;
|
||||
|
||||
import mathematik.*;
|
||||
|
||||
import teilchen.Particle;
|
||||
import teilchen.Physics;
|
||||
|
||||
/**
|
||||
* this sketch show how to create a particle system with a single particle in it.
|
||||
*/
|
||||
|
||||
Physics mPhysics;
|
||||
|
||||
Particle mParticle;
|
||||
|
||||
void setup() {
|
||||
size(640, 480, OPENGL);
|
||||
smooth();
|
||||
frameRate(30);
|
||||
|
||||
/* create a particle system. */
|
||||
mPhysics = new Physics();
|
||||
|
||||
/*
|
||||
* a physic-based particle system consists of a few components.
|
||||
*
|
||||
* 1 particles.
|
||||
* there are different kinds of particles. for now we use a simple particle.
|
||||
*
|
||||
* 2 forces.
|
||||
* there are all kinds of forces. one of the most obvious force is the gravitational force,
|
||||
* but there all kinds of different forces like attractors and springs. forces usually
|
||||
* affect all particles in the system.
|
||||
*
|
||||
* 3 behaviors
|
||||
* a behavior is special kind of force. it is something like an internal force or a motor
|
||||
* that only affects a single particle. a typical force is for example the 'seek force'
|
||||
* which constantly pulls a particle into a certain direction.
|
||||
*
|
||||
* 4 integrators.
|
||||
* integrators are used to integrate acceleration and velocity to calculate the new position.
|
||||
* the most well-known is the 'euler' integrator, but there are also optimized versions like 'runge-kutta'
|
||||
* or 'Midpoint' or even slightly different concepts like 'verlet'.
|
||||
*
|
||||
*/
|
||||
|
||||
/* create a particle. note that the particle is automatically added to particle system */
|
||||
mParticle = mPhysics.makeParticle();
|
||||
}
|
||||
|
||||
void draw() {
|
||||
/* update the particle system to the next step. usually the time step is the duration of the las frame */
|
||||
final float mDeltaTime = 1.0 / frameRate;
|
||||
mPhysics.step(mDeltaTime);
|
||||
|
||||
/* draw particle */
|
||||
background(255);
|
||||
stroke(0, 127);
|
||||
fill(0, 32);
|
||||
ellipse(mParticle.position().x, mParticle.position().y, 12, 12);
|
||||
|
||||
/* reset particle s position and velocity */
|
||||
if (mousePressed) {
|
||||
mParticle.position().set(mouseX, mouseY);
|
||||
mParticle.velocity().set(mouseX - pmouseX, mouseY - pmouseY);
|
||||
mParticle.velocity().scale(10);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
import processing.opengl.*;
|
||||
|
||||
import mathematik.*;
|
||||
|
||||
import teilchen.Particle;
|
||||
import teilchen.Physics;
|
||||
import teilchen.force.Gravity;
|
||||
|
||||
/**
|
||||
* this sketch show how to create a particle system with a single particle in it.
|
||||
*/
|
||||
|
||||
Physics mPhysics;
|
||||
|
||||
Particle mParticle;
|
||||
|
||||
void setup() {
|
||||
size(640, 480, OPENGL);
|
||||
smooth();
|
||||
frameRate(30);
|
||||
|
||||
/* create a particle system */
|
||||
mPhysics = new Physics();
|
||||
|
||||
/* create a gravitational force */
|
||||
Gravity mGravity = new Gravity();
|
||||
/* the direction of the gravity is defined by the 'force' vector */
|
||||
mGravity.force().set(0, 30, 0);
|
||||
/* forces, like gravity or any other force, can be added to the system. they will be automatically applied to all particles */
|
||||
mPhysics.add(mGravity);
|
||||
|
||||
/* create a particle and add it to the system */
|
||||
mParticle = mPhysics.makeParticle();
|
||||
}
|
||||
|
||||
void draw() {
|
||||
/* update the particle system. this applies the gravity to the particle */
|
||||
final float mDeltaTime = 1.0 / frameRate;
|
||||
mPhysics.step(mDeltaTime);
|
||||
|
||||
/* draw particle */
|
||||
background(255);
|
||||
stroke(0, 127);
|
||||
fill(0, 32);
|
||||
ellipse(mParticle.position().x, mParticle.position().y, 12, 12);
|
||||
|
||||
/* reset particle s position and velocity */
|
||||
if (mousePressed) {
|
||||
mParticle.position().set(mouseX, mouseY);
|
||||
mParticle.velocity().set(mouseX - pmouseX, mouseY - pmouseY);
|
||||
mParticle.velocity().scale(10);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
import processing.opengl.*;
|
||||
|
||||
import mathematik.*;
|
||||
|
||||
import teilchen.Particle;
|
||||
import teilchen.Physics;
|
||||
import teilchen.force.Gravity;
|
||||
|
||||
/**
|
||||
* this sketch shows how to create and handle multiple particles and remove individual particles.
|
||||
*/
|
||||
|
||||
Physics mPhysics;
|
||||
|
||||
void setup() {
|
||||
size(640, 480, OPENGL);
|
||||
smooth();
|
||||
frameRate(30);
|
||||
|
||||
/* create a particle system */
|
||||
mPhysics = new Physics();
|
||||
|
||||
/* create a gravitational force and add it to the particle system */
|
||||
Gravity myGravity = new Gravity(0, 30, 0);
|
||||
mPhysics.add(myGravity);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
if (mousePressed) {
|
||||
/* create and add a particle to the system */
|
||||
Particle mParticle = mPhysics.makeParticle();
|
||||
/* set particle to mouse position with random velocity */
|
||||
mParticle.position().set(mouseX, mouseY);
|
||||
mParticle.velocity().set(random(-20, 20), random(-50));
|
||||
}
|
||||
|
||||
/* update the particle system */
|
||||
final float mDeltaTime = 1.0 / frameRate;
|
||||
mPhysics.step(mDeltaTime);
|
||||
|
||||
/* remove particles right before they hit the edge of the screen */
|
||||
for (int i = 0; i < mPhysics.particles().size(); i++) {
|
||||
Particle mParticle = mPhysics.particles(i);
|
||||
if (mParticle.position().y > height * 0.9f) {
|
||||
mPhysics.particles().remove(i);
|
||||
}
|
||||
}
|
||||
|
||||
/* draw all the particles in the system */
|
||||
background(255);
|
||||
stroke(0, 127);
|
||||
fill(0, 32);
|
||||
for (int i = 0; i < mPhysics.particles().size(); i++) {
|
||||
Particle mParticle = mPhysics.particles(i);
|
||||
ellipse(mParticle.position().x, mParticle.position().y, 10, 10);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
import processing.opengl.*;
|
||||
|
||||
import mathematik.*;
|
||||
|
||||
import teilchen.Particle;
|
||||
import teilchen.Physics;
|
||||
import teilchen.constraint.Teleporter;
|
||||
import teilchen.force.Attractor;
|
||||
import teilchen.force.ViscousDrag;
|
||||
|
||||
/**
|
||||
* this sketch shows how to create and use attractors.
|
||||
*/
|
||||
|
||||
Physics mPhysics;
|
||||
|
||||
Attractor mAttractor;
|
||||
|
||||
void setup() {
|
||||
size(640, 480, OPENGL);
|
||||
smooth();
|
||||
frameRate(30);
|
||||
|
||||
/* create a particle system */
|
||||
mPhysics = new Physics();
|
||||
|
||||
/* create a viscous force that slows down all motion */
|
||||
ViscousDrag myDrag = new ViscousDrag();
|
||||
myDrag.coefficient = 0.75f;
|
||||
mPhysics.add(myDrag);
|
||||
|
||||
/* teleport particles from one edge of the screen to the other */
|
||||
Teleporter mTeleporter = new Teleporter();
|
||||
mTeleporter.min().set(0, 0);
|
||||
mTeleporter.max().set(width, height);
|
||||
mPhysics.add(mTeleporter);
|
||||
|
||||
/* create some particles */
|
||||
for (int i = 0; i < 100; i++) {
|
||||
Particle myParticle = mPhysics.makeParticle();
|
||||
myParticle.position().set(random(width), random(height));
|
||||
}
|
||||
|
||||
/* create an attractor */
|
||||
mAttractor = new Attractor();
|
||||
mAttractor.radius(100);
|
||||
mAttractor.strength(150);
|
||||
mPhysics.add(mAttractor);
|
||||
}
|
||||
|
||||
void mousePressed() {
|
||||
/* flip the direction of the attractors strength. */
|
||||
float myInvertedStrength = -1 * mAttractor.strength();
|
||||
/* a negative strength turns the attractor into a repulsor */
|
||||
mAttractor.strength(myInvertedStrength);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
/* set attractor to mouse position */
|
||||
mAttractor.position().set(mouseX, mouseY);
|
||||
|
||||
/* update the particle system */
|
||||
final float mDeltaTime = 1.0 / frameRate;
|
||||
mPhysics.step(mDeltaTime);
|
||||
|
||||
/* draw */
|
||||
background(255);
|
||||
|
||||
/* draw all the particles in particle system */
|
||||
fill(245);
|
||||
stroke(164);
|
||||
for (int i = 0; i < mPhysics.particles().size(); i++) {
|
||||
Particle myParticle = mPhysics.particles(i);
|
||||
ellipse(myParticle.position().x, myParticle.position().y, 12, 12);
|
||||
}
|
||||
|
||||
/* draw attractor. green if it is attracting and red if it is repelling */
|
||||
noStroke();
|
||||
if (mAttractor.strength() < 0) {
|
||||
fill(255, 0, 0, 50);
|
||||
}
|
||||
else {
|
||||
fill(0, 255, 0, 50);
|
||||
}
|
||||
ellipse(mAttractor.position().x, mAttractor.position().y,
|
||||
mAttractor.radius(), mAttractor.radius());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
import processing.opengl.*;
|
||||
|
||||
import mathematik.*;
|
||||
|
||||
import teilchen.Particle;
|
||||
import teilchen.Physics;
|
||||
import teilchen.ShortLivedParticle;
|
||||
import teilchen.force.Gravity;
|
||||
import teilchen.force.PlaneDeflector;
|
||||
import teilchen.force.ViscousDrag;
|
||||
|
||||
/**
|
||||
* this sketch shows
|
||||
* 1 how to create and use plane deflectors
|
||||
* 2 how to use 'ShortLivedParticle'
|
||||
*/
|
||||
|
||||
Physics mPhysics;
|
||||
|
||||
PlaneDeflector mDeflector;
|
||||
|
||||
void setup() {
|
||||
size(640, 480, OPENGL);
|
||||
smooth();
|
||||
frameRate(30);
|
||||
|
||||
/* create a particle system */
|
||||
mPhysics = new Physics();
|
||||
|
||||
/* create a deflector and add it to the particle system.
|
||||
* the that defines the deflection area is defined by an
|
||||
* origin and a normal. this also means that the plane s size
|
||||
* is infinite.
|
||||
* note that there is also a triangle delfector that is constraint
|
||||
* by three points.
|
||||
*/
|
||||
mDeflector = new PlaneDeflector();
|
||||
/* set plane origin into the center of the screen */
|
||||
mDeflector.plane().origin.set(width / 2, height / 2, 0);
|
||||
mDeflector.plane().normal.set(0, -1, 0);
|
||||
/* the coefficient of restitution defines how hard particles bounce of the deflector */
|
||||
mDeflector.coefficientofrestitution(0.7f);
|
||||
mPhysics.add(mDeflector);
|
||||
|
||||
/* create gravitiy */
|
||||
Gravity myGravity = new Gravity();
|
||||
myGravity.force().y = 50;
|
||||
mPhysics.add(myGravity);
|
||||
|
||||
/* create drag */
|
||||
ViscousDrag myViscousDrag = new ViscousDrag();
|
||||
myViscousDrag.coefficient = 0.1f;
|
||||
mPhysics.add(myViscousDrag);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
/* rotate deflector plane */
|
||||
if (mousePressed) {
|
||||
final float myAngle = 2 * PI * (float)mouseX / width - PI;
|
||||
mDeflector.plane().normal.set(sin(myAngle), -cos(myAngle), 0);
|
||||
}
|
||||
|
||||
/* create a special particle */
|
||||
ShortLivedParticle myNewParticle = new ShortLivedParticle();
|
||||
myNewParticle.position().set(mouseX, mouseY);
|
||||
myNewParticle.velocity().set(0, random(100) + 50);
|
||||
/* this particle is removed after a specific interval */
|
||||
myNewParticle.setMaxAge(4);
|
||||
/* add particle manually to the particle system */
|
||||
mPhysics.add(myNewParticle);
|
||||
|
||||
/* update physics */
|
||||
final float mDeltaTime = 1.0 / frameRate;
|
||||
mPhysics.step(mDeltaTime);
|
||||
|
||||
/* draw all the particles in the particle system */
|
||||
background(255);
|
||||
for (int i = 0; i < mPhysics.particles().size(); i++) {
|
||||
Particle myParticle = mPhysics.particles(i);
|
||||
/* this special particle can tell you how much time it has to live.
|
||||
* we map this information to its transparency.
|
||||
*/
|
||||
float myRatio = 1 - ((ShortLivedParticle)myParticle).ageRatio();
|
||||
stroke(0, 64 * myRatio);
|
||||
fill(0, 32 * myRatio);
|
||||
ellipse(myParticle.position().x, myParticle.position().y, 12, 12);
|
||||
}
|
||||
|
||||
/* draw deflector */
|
||||
stroke(0, 127);
|
||||
line(mDeflector.plane().origin.x - mDeflector.plane().normal.y * -width,
|
||||
mDeflector.plane().origin.y + mDeflector.plane().normal.x * -width,
|
||||
mDeflector.plane().origin.x - mDeflector.plane().normal.y * width,
|
||||
mDeflector.plane().origin.y + mDeflector.plane().normal.x * width);
|
||||
|
||||
stroke(255, 0, 0, 127);
|
||||
line(mDeflector.plane().origin.x,
|
||||
mDeflector.plane().origin.y,
|
||||
mDeflector.plane().origin.x + mDeflector.plane().normal.x * 20,
|
||||
mDeflector.plane().origin.y + mDeflector.plane().normal.y * 20);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
import processing.opengl.*;
|
||||
|
||||
import mathematik.*;
|
||||
|
||||
import teilchen.Particle;
|
||||
import teilchen.Physics;
|
||||
import teilchen.force.Spring;
|
||||
import teilchen.force.ViscousDrag;
|
||||
|
||||
/**
|
||||
* this sketch shows
|
||||
* 1 how to create a viscous drag to slow motion eventually down.
|
||||
* 2 how to create a spring that connects two particles.
|
||||
*/
|
||||
|
||||
Physics mPhysics;
|
||||
|
||||
Spring mSpring;
|
||||
|
||||
void setup() {
|
||||
size(640, 480, OPENGL);
|
||||
smooth();
|
||||
frameRate(30);
|
||||
|
||||
/* create a particle system */
|
||||
mPhysics = new Physics();
|
||||
|
||||
/* create a viscous force that slows down all motion; 0 means no slowing down. */
|
||||
ViscousDrag myDrag = new ViscousDrag(0.25f);
|
||||
mPhysics.add(myDrag);
|
||||
|
||||
/* create two particles that we can connect with a spring */
|
||||
Particle myA = mPhysics.makeParticle();
|
||||
myA.position().set(width / 2 - 50, height / 2);
|
||||
|
||||
Particle myB = mPhysics.makeParticle();
|
||||
myB.position().set(width / 2 + 50, height / 2);
|
||||
|
||||
/* create a spring force that connects two particles.
|
||||
* note that there is more than one way to create a spring.
|
||||
* in our case the restlength of the spring is defined by the
|
||||
* particles current position.
|
||||
*/
|
||||
mSpring = mPhysics.makeSpring(myA, myB);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
/* set first particle to mouse position */
|
||||
if (mousePressed) {
|
||||
mSpring.a().position().set(mouseX, mouseY);
|
||||
}
|
||||
|
||||
/* update the particle system */
|
||||
final float mDeltaTime = 1.0 / frameRate;
|
||||
mPhysics.step(mDeltaTime);
|
||||
|
||||
/* draw particles and connecting line */
|
||||
background(255);
|
||||
noFill();
|
||||
stroke(255, 0, 127, 64);
|
||||
line(mSpring.a().position().x, mSpring.a().position().y,
|
||||
mSpring.b().position().x, mSpring.b().position().y);
|
||||
fill(245);
|
||||
stroke(164);
|
||||
ellipse(mSpring.a().position().x, mSpring.a().position().y, 12, 12);
|
||||
ellipse(mSpring.b().position().x, mSpring.b().position().y, 12, 12);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
import processing.opengl.*;
|
||||
|
||||
import mathematik.*;
|
||||
|
||||
import teilchen.Particle;
|
||||
import teilchen.Physics;
|
||||
import teilchen.force.Spring;
|
||||
|
||||
/**
|
||||
* this sketch shows
|
||||
* 1 how to create a viscous drag to slow motion eventually down.
|
||||
* 2 how to create a spring that connects two particles.
|
||||
*/
|
||||
|
||||
Physics mPhysics;
|
||||
|
||||
Particle mRoot;
|
||||
|
||||
void setup() {
|
||||
size(640, 480, OPENGL);
|
||||
smooth();
|
||||
frameRate(30);
|
||||
|
||||
/* create a particle system */
|
||||
mPhysics = new Physics();
|
||||
|
||||
/* create a particle to which we will connect springs */
|
||||
mRoot = mPhysics.makeParticle(width / 2, height / 2, 0.0);
|
||||
/* we give the root particle a higher mass so it doesn t move as easily */
|
||||
mRoot.mass(30);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
/* create a particle at mouse position and connect it to the root particle through a spring */
|
||||
if (mousePressed) {
|
||||
Particle mParticle = mPhysics.makeParticle(mouseX, mouseY, 0);
|
||||
Spring mSpring = mPhysics.makeSpring(mRoot, mParticle);
|
||||
/* restlength defines the desired length of the spring. in this case it is the distance between the two particles. */
|
||||
float mRestlength = mSpring.restlength();
|
||||
/* we modify the restlength to add a bit of energy into the system */
|
||||
mSpring.restlength(mRestlength * 1.5f);
|
||||
}
|
||||
|
||||
/* update the particle system */
|
||||
final float mDeltaTime = 1.0 / frameRate;
|
||||
mPhysics.step(mDeltaTime);
|
||||
|
||||
/* draw particles and connecting line */
|
||||
background(255);
|
||||
|
||||
/* draw springs */
|
||||
noFill();
|
||||
stroke(255, 0, 127, 64);
|
||||
for (int i = 0; i < mPhysics.forces().size(); i++) {
|
||||
if (mPhysics.forces().get(i) instanceof Spring) {
|
||||
Spring mSSpring = (Spring)mPhysics.forces().get(i);
|
||||
line(mSSpring.a().position().x, mSSpring.a().position().y,
|
||||
mSSpring.b().position().x, mSSpring.b().position().y);
|
||||
}
|
||||
}
|
||||
/* draw particles */
|
||||
fill(245);
|
||||
stroke(164);
|
||||
for (int i = 0; i < mPhysics.particles().size(); i++) {
|
||||
ellipse(mPhysics.particles().get(i).position().x,
|
||||
mPhysics.particles().get(i).position().y,
|
||||
12, 12);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
import processing.opengl.*;
|
||||
|
||||
import mathematik.*;
|
||||
|
||||
import teilchen.Physics;
|
||||
import teilchen.force.Gravity;
|
||||
import teilchen.force.ViscousDrag;
|
||||
import teilchen.util.DrawLib;
|
||||
import teilchen.Particle;
|
||||
import teilchen.constraint.Box;
|
||||
import teilchen.integration.RungeKutta;
|
||||
import teilchen.util.StableSpringQuad;
|
||||
|
||||
Physics mPhysics;
|
||||
|
||||
Particle mRoot;
|
||||
|
||||
void setup() {
|
||||
size(640, 480, OPENGL);
|
||||
smooth();
|
||||
frameRate(60);
|
||||
|
||||
mPhysics = new Physics();
|
||||
/* we use 'runge kutta' as it is more stable for this application */
|
||||
mPhysics.setInegratorRef(new RungeKutta());
|
||||
|
||||
Gravity myGravity = new Gravity();
|
||||
myGravity.force().y = 98.1f;
|
||||
mPhysics.add(myGravity);
|
||||
|
||||
/* add drag to smooth the spring interaction */
|
||||
mPhysics.add(new ViscousDrag(0.2f));
|
||||
|
||||
/* add a container */
|
||||
Box myBox = new Box();
|
||||
myBox.min().set(0, 0, 0);
|
||||
myBox.max().set(width, height, 0);
|
||||
mPhysics.add(myBox);
|
||||
|
||||
/* create root */
|
||||
Particle a = mPhysics.makeParticle(0, 0);
|
||||
Particle b = mPhysics.makeParticle(100, 0);
|
||||
Particle c = mPhysics.makeParticle(100, 100);
|
||||
Particle d = mPhysics.makeParticle(0, 100);
|
||||
|
||||
new StableSpringQuad(mPhysics, d, c, mPhysics.makeParticle(100, 200), mPhysics.makeParticle(0, 200));
|
||||
|
||||
/* create stable quad from springs */
|
||||
/* first the edge-springs ... */
|
||||
final float mySpringConstant = 100;
|
||||
final float mySpringDamping = 5;
|
||||
mPhysics.makeSpring(a, b, mySpringConstant, mySpringDamping);
|
||||
mPhysics.makeSpring(b, c, mySpringConstant, mySpringDamping);
|
||||
mPhysics.makeSpring(c, d, mySpringConstant, mySpringDamping);
|
||||
mPhysics.makeSpring(d, a, mySpringConstant, mySpringDamping).restlength();
|
||||
/* ... then the diagonal-springs */
|
||||
mPhysics.makeSpring(a, c, mySpringConstant, mySpringDamping);
|
||||
mPhysics.makeSpring(b, d, mySpringConstant, mySpringDamping).restlength();
|
||||
|
||||
/* define 'a' as root particle for mouse interaction */
|
||||
mRoot = a;
|
||||
mRoot.fixed(true);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
|
||||
/* handle particles */
|
||||
if (mousePressed) {
|
||||
mRoot.fixed(true);
|
||||
mRoot.position().set(mouseX, mouseY);
|
||||
}
|
||||
else {
|
||||
mRoot.fixed(false);
|
||||
}
|
||||
|
||||
mPhysics.step(1f / frameRate);
|
||||
|
||||
/* draw */
|
||||
background(255);
|
||||
DrawLib.drawSprings(g, mPhysics, color(255, 0, 127, 64));
|
||||
DrawLib.drawParticles(g, mPhysics, 12, color(164), color(245));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
import processing.opengl.*;
|
||||
|
||||
import mathematik.*;
|
||||
|
||||
import teilchen.Particle;
|
||||
import teilchen.Physics;
|
||||
import teilchen.constraint.Stick;
|
||||
import teilchen.force.Gravity;
|
||||
import teilchen.integration.Verlet;
|
||||
|
||||
Physics mPhysics;
|
||||
|
||||
Particle[] mParticles;
|
||||
|
||||
void setup() {
|
||||
size(640, 480, OPENGL);
|
||||
frameRate(60);
|
||||
smooth();
|
||||
|
||||
mPhysics = new Physics();
|
||||
/* increase the number of iterations for contraints in each step. this can greatly relaxes tensions in the system. */
|
||||
mPhysics.contraint_iterations_per_steps = 5;
|
||||
|
||||
/* add gravity for extra fun */
|
||||
mPhysics.add(new Gravity());
|
||||
|
||||
/* we chose verlet integration as it integrates much more nicely with sticks ( and constraints in general ) */
|
||||
Verlet myVerlet = new Verlet();
|
||||
myVerlet.damping(0.99f);
|
||||
mPhysics.setInegratorRef(myVerlet);
|
||||
|
||||
/* setup sticks to form a whip */
|
||||
mParticles = new Particle[16];
|
||||
float mSegmentLength = 20.0;
|
||||
/* create root */
|
||||
for (int x = 0; x < mParticles.length; x++) {
|
||||
mParticles[x] = mPhysics.makeParticle(x * mSegmentLength, 0, 0, 0.1f);
|
||||
if (x > 0) {
|
||||
Stick myStick = new Stick(mParticles[x - 1],
|
||||
mParticles[x],
|
||||
mSegmentLength);
|
||||
/* damp the stick to release tensions from the system */
|
||||
myStick.damping(0.99f);
|
||||
mPhysics.add(myStick);
|
||||
}
|
||||
}
|
||||
|
||||
/* fix root particle so it can stick to the mouse later */
|
||||
mParticles[0].fixed(true);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
/* stick root particle to mouse */
|
||||
mParticles[0].position().set(mouseX, mouseY);
|
||||
|
||||
/* update */
|
||||
mPhysics.step(1.0 / frameRate);
|
||||
|
||||
/* draw sticks with descending stroke weight */
|
||||
background(255);
|
||||
stroke(0, 192);
|
||||
for (int x = 1; x < mParticles.length; x++) {
|
||||
Particle p1 = mParticles[x - 1];
|
||||
Particle p2 = mParticles[x];
|
||||
final float mStrokeWeight = 4.0 * (1.0 - (float)x / mParticles.length);
|
||||
strokeWeight(mStrokeWeight);
|
||||
line(p1.position().x, p1.position().y, p1.position().z,
|
||||
p2.position().x, p2.position().y, p2.position().z);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
import processing.opengl.*;
|
||||
|
||||
import mathematik.Vector3f;
|
||||
|
||||
import teilchen.Particle;
|
||||
import teilchen.Physics;
|
||||
import teilchen.constraint.IConstraint;
|
||||
import teilchen.constraint.Stick;
|
||||
import teilchen.force.Attractor;
|
||||
import teilchen.force.Gravity;
|
||||
import teilchen.integration.Verlet;
|
||||
|
||||
|
||||
Physics mPhysics;
|
||||
|
||||
Particle[][] mParticles;
|
||||
|
||||
final int GRID_WIDTH = 32;
|
||||
|
||||
final int GRID_HEIGHT = 16;
|
||||
|
||||
Attractor mAttractor;
|
||||
|
||||
void setup() {
|
||||
size(640, 480, OPENGL);
|
||||
frameRate(60);
|
||||
|
||||
mPhysics = new Physics();
|
||||
mPhysics.contraint_iterations_per_steps = 5;
|
||||
|
||||
Verlet myVerlet = new Verlet();
|
||||
myVerlet.damping(0.9f);
|
||||
mPhysics.setInegratorRef(myVerlet);
|
||||
mPhysics.add(new Gravity(new Vector3f(0, 1000f, 0)));
|
||||
|
||||
mAttractor = new Attractor();
|
||||
mAttractor.strength(-15000);
|
||||
mAttractor.radius(300);
|
||||
mPhysics.add(mAttractor);
|
||||
|
||||
mParticles = new Particle[GRID_WIDTH][GRID_HEIGHT];
|
||||
|
||||
/* setup cloth */
|
||||
float mGridStepX = ((float)width / GRID_WIDTH);
|
||||
float mGridStepY = (((float)height * 0.5f) / GRID_HEIGHT);
|
||||
for (int y = 0; y < GRID_HEIGHT; y++) {
|
||||
for (int x = 0; x < GRID_WIDTH; x++) {
|
||||
mParticles[x][y] = mPhysics.makeParticle();
|
||||
mParticles[x][y].position().set((x + 0.5f) * mGridStepX,
|
||||
y * mGridStepY,
|
||||
random(0, 1));
|
||||
mParticles[x][y].old_position().set(mParticles[x][y].position());
|
||||
mParticles[x][y].mass(0.1f);
|
||||
|
||||
final float DAMPING = 0.9f;
|
||||
if (y > 0) {
|
||||
Stick myStick = new Stick(mParticles[x][y - 1],
|
||||
mParticles[x][y],
|
||||
mGridStepY);
|
||||
myStick.damping(DAMPING);
|
||||
mPhysics.add(myStick);
|
||||
}
|
||||
if (x > 0) {
|
||||
Stick myStick = new Stick(mParticles[x - 1][y],
|
||||
mParticles[x][y],
|
||||
mGridStepX);
|
||||
myStick.damping(DAMPING);
|
||||
mPhysics.add(myStick);
|
||||
}
|
||||
if (x > 0 && y > 0) {
|
||||
Stick myStick1 = new Stick(mParticles[x - 1][y - 1],
|
||||
mParticles[x][y],
|
||||
new Vector3f(mGridStepX, mGridStepY).length());
|
||||
mPhysics.add(myStick1);
|
||||
Stick myStick2 = new Stick(mParticles[x][y - 1],
|
||||
mParticles[x - 1][y],
|
||||
new Vector3f(mGridStepX, mGridStepY).length());
|
||||
mPhysics.add(myStick2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* fix first row */
|
||||
for (int x = 0; x < mParticles.length; x++) {
|
||||
mParticles[x][0].fixed(true);
|
||||
}
|
||||
}
|
||||
|
||||
void draw() {
|
||||
|
||||
/* update */
|
||||
mAttractor.position().set(mouseX, mouseY, 50);
|
||||
mPhysics.step(1.0 / frameRate);
|
||||
|
||||
background(255);
|
||||
|
||||
/* draw sticks */
|
||||
stroke(0, 127);
|
||||
|
||||
for (int i = 0; i < mPhysics.constraints().size(); i++) {
|
||||
IConstraint mConstraint = mPhysics.constraints().get(i);
|
||||
if (mConstraint instanceof Stick) {
|
||||
final Stick myStick = (Stick)mConstraint;
|
||||
line(myStick.a().position().x,
|
||||
myStick.a().position().y,
|
||||
myStick.a().position().z,
|
||||
myStick.b().position().x,
|
||||
myStick.b().position().y,
|
||||
myStick.b().position().z);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
import mathematik.*;
|
||||
|
||||
import teilchen.BehaviorParticle;
|
||||
import teilchen.Physics;
|
||||
import teilchen.behavior.Arrival;
|
||||
|
||||
|
||||
/**
|
||||
* this sketch shows how to assign an 'arrival' behavior to a particle.
|
||||
*/
|
||||
Physics mPhysics;
|
||||
|
||||
BehaviorParticle mParticle;
|
||||
|
||||
Arrival mArrival;
|
||||
|
||||
void setup() {
|
||||
size(640, 480, OPENGL);
|
||||
smooth();
|
||||
frameRate(120);
|
||||
colorMode(RGB, 1.0f);
|
||||
noFill();
|
||||
|
||||
/* physics */
|
||||
mPhysics = new Physics();
|
||||
|
||||
/* create particles */
|
||||
mParticle = mPhysics.makeParticle(BehaviorParticle.class);
|
||||
mParticle.maximumInnerForce(100);
|
||||
|
||||
/* create arrival behavior */
|
||||
mArrival = new Arrival();
|
||||
mArrival.breakforce(mParticle.maximumInnerForce() * 0.25f);
|
||||
mArrival.breakradius(mParticle.maximumInnerForce() * 0.25f);
|
||||
mParticle.behaviors().add(mArrival);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
|
||||
/* set the arrival position to the mouse position */
|
||||
mArrival.position().set(mouseX, mouseY);
|
||||
|
||||
/* update particle system */
|
||||
mPhysics.step(1.0f / frameRate);
|
||||
|
||||
/* draw behavior particle */
|
||||
background(1);
|
||||
stroke(0, 0.5f);
|
||||
if (mArrival.arriving()) {
|
||||
/* color particle red while it is arriving */
|
||||
stroke(1, 0, 0, 0.5f);
|
||||
}
|
||||
if (mArrival.arrived()) {
|
||||
/* color particle green when it has arrived */
|
||||
stroke(0, 1, 0, 0.5f);
|
||||
}
|
||||
|
||||
line(mParticle.position().x,
|
||||
mParticle.position().y,
|
||||
mParticle.position().x + mParticle.velocity().x,
|
||||
mParticle.position().y + mParticle.velocity().y);
|
||||
fill(1);
|
||||
ellipse(mParticle.position().x, mParticle.position().y, 12, 12);
|
||||
|
||||
/* draw arrival */
|
||||
stroke(0, 0.25f);
|
||||
noFill();
|
||||
ellipse(mArrival.position().x,
|
||||
mArrival.position().y,
|
||||
mArrival.breakradius() * 2,
|
||||
mArrival.breakradius() * 2);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
import processing.opengl.*;
|
||||
import mathematik.*;
|
||||
|
||||
import teilchen.Particle;
|
||||
import teilchen.Physics;
|
||||
import teilchen.force.Spring;
|
||||
import teilchen.util.Overlap;
|
||||
|
||||
/**
|
||||
* this sketch is exactly like Lesson06_Springs, except that it also shows how to remove overlaps.
|
||||
*/
|
||||
|
||||
Physics mPhysics;
|
||||
|
||||
Particle mRoot;
|
||||
|
||||
static final float PARTICLE_RADIUS = 6;
|
||||
|
||||
void setup() {
|
||||
size(640, 480, OPENGL);
|
||||
smooth();
|
||||
frameRate(30);
|
||||
|
||||
mPhysics = new Physics();
|
||||
mRoot = mPhysics.makeParticle(width / 2, height / 2, 0.0);
|
||||
mRoot.mass(30);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
if (mousePressed) {
|
||||
Particle mParticle = mPhysics.makeParticle(mouseX, mouseY, 0);
|
||||
Spring mSpring = mPhysics.makeSpring(mRoot, mParticle);
|
||||
float mRestlength = mSpring.restlength();
|
||||
mSpring.restlength(mRestlength * 1.5f);
|
||||
|
||||
/* we define a radius for the particle so the particle has dimensions */
|
||||
mParticle.radius(PARTICLE_RADIUS);
|
||||
}
|
||||
|
||||
/* move overlapping particles away from each other */
|
||||
Overlap.resolveOverlap(mPhysics.particles());
|
||||
|
||||
/* update the particle system */
|
||||
final float mDeltaTime = 1.0 / frameRate;
|
||||
mPhysics.step(mDeltaTime);
|
||||
|
||||
/* draw particles and connecting line */
|
||||
background(255);
|
||||
|
||||
/* draw springs */
|
||||
noFill();
|
||||
stroke(255, 0, 127, 64);
|
||||
for (int i = 0; i < mPhysics.forces().size(); i++) {
|
||||
if (mPhysics.forces().get(i) instanceof Spring) {
|
||||
Spring mSSpring = (Spring)mPhysics.forces().get(i);
|
||||
line(mSSpring.a().position().x, mSSpring.a().position().y,
|
||||
mSSpring.b().position().x, mSSpring.b().position().y);
|
||||
}
|
||||
}
|
||||
/* draw particles */
|
||||
fill(245);
|
||||
stroke(164);
|
||||
for (int i = 0; i < mPhysics.particles().size(); i++) {
|
||||
ellipse(mPhysics.particles().get(i).position().x,
|
||||
mPhysics.particles().get(i).position().y,
|
||||
PARTICLE_RADIUS * 2, PARTICLE_RADIUS * 2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
import processing.opengl.*;
|
||||
|
||||
import mathematik.Vector3f;
|
||||
|
||||
import teilchen.Particle;
|
||||
import teilchen.Physics;
|
||||
import teilchen.constraint.Box;
|
||||
import teilchen.force.Gravity;
|
||||
import teilchen.force.Spring;
|
||||
import teilchen.force.ViscousDrag;
|
||||
import teilchen.util.CollisionManager;
|
||||
|
||||
static final float PARTICLE_SIZE = 12;
|
||||
|
||||
CollisionManager mCollision;
|
||||
|
||||
Physics mPhysics;
|
||||
|
||||
void setup() {
|
||||
size(640, 480, OPENGL);
|
||||
smooth();
|
||||
frameRate(30);
|
||||
noFill();
|
||||
ellipseMode(CENTER);
|
||||
|
||||
mCollision = new CollisionManager();
|
||||
mCollision.distancemode(CollisionManager.DISTANCE_MODE_FIXED);
|
||||
mCollision.minimumDistance(50);
|
||||
|
||||
mPhysics = new Physics();
|
||||
mPhysics.add(new ViscousDrag(0.85f));
|
||||
mPhysics.add(new Gravity());
|
||||
|
||||
Box myBox = new Box();
|
||||
myBox.min().set(50, 50, 0);
|
||||
myBox.max().set(width - 50, height - 50, 0);
|
||||
myBox.coefficientofrestitution(0.7f);
|
||||
myBox.reflect(true);
|
||||
mPhysics.add(myBox);
|
||||
|
||||
/* create a first particle */
|
||||
final Particle myParticle = mPhysics.makeParticle(new Vector3f(mouseX, mouseY, 0), 10);
|
||||
mCollision.collision().add(myParticle);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
/* create particles */
|
||||
if (mousePressed) {
|
||||
final Particle myParticle = mPhysics.makeParticle(new Vector3f(mouseX, mouseY, 0), 10);
|
||||
mCollision.collision().add(myParticle);
|
||||
}
|
||||
|
||||
/* collision handler */
|
||||
final float mDeltaTime = 1.0 / frameRate;
|
||||
mCollision.createCollisionResolvers();
|
||||
mCollision.loop(mDeltaTime);
|
||||
mPhysics.step(mDeltaTime);
|
||||
|
||||
/* draw */
|
||||
background(255);
|
||||
drawThings();
|
||||
|
||||
mCollision.removeCollisionResolver();
|
||||
}
|
||||
|
||||
void drawThings() {
|
||||
/* collision springs */
|
||||
noFill();
|
||||
stroke(255, 0, 127, 64);
|
||||
for (int i = 0; i < mCollision.collision().forces().size(); ++i) {
|
||||
if (mCollision.collision().forces().get(i) instanceof Spring) {
|
||||
Spring mySpring = (Spring)mCollision.collision_forces().get(i);
|
||||
line(mySpring.a().position().x, mySpring.a().position().y, mySpring.a().position().z,
|
||||
mySpring.b().position().x, mySpring.b().position().y, mySpring.b().position().z);
|
||||
}
|
||||
}
|
||||
|
||||
/* particles */
|
||||
fill(245);
|
||||
stroke(164);
|
||||
for (int i = 0; i < mPhysics.particles().size(); ++i) {
|
||||
Particle myParticle = mPhysics.particles().get(i);
|
||||
pushMatrix();
|
||||
translate(myParticle.position().x, myParticle.position().y, myParticle.position().z);
|
||||
ellipse(0, 0,
|
||||
PARTICLE_SIZE,
|
||||
PARTICLE_SIZE);
|
||||
popMatrix();
|
||||
}
|
||||
}
|
||||
|
||||
Executable
+131
@@ -0,0 +1,131 @@
|
||||
import mathematik.Vector3f;
|
||||
import teilchen.Particle;
|
||||
import teilchen.Physics;
|
||||
import teilchen.ShortLivedParticle;
|
||||
import teilchen.constraint.Box;
|
||||
import teilchen.force.Attractor;
|
||||
import teilchen.force.Gravity;
|
||||
import teilchen.force.ViscousDrag;
|
||||
import teilchen.util.ParticleTrail;
|
||||
|
||||
import java.util.Vector;
|
||||
|
||||
Physics mPhysics;
|
||||
|
||||
Vector<ParticleTrail> mTrails;
|
||||
|
||||
Attractor mAttractor;
|
||||
|
||||
void setup() {
|
||||
size(640, 480, OPENGL);
|
||||
smooth();
|
||||
frameRate(60);
|
||||
|
||||
/* create a particle system */
|
||||
mPhysics = new Physics();
|
||||
|
||||
/* create a gravitational force */
|
||||
Gravity myGravity = new Gravity();
|
||||
mPhysics.add(myGravity);
|
||||
myGravity.force().y = 20;
|
||||
|
||||
/* create drag */
|
||||
ViscousDrag myViscousDrag = new ViscousDrag();
|
||||
myViscousDrag.coefficient = 0.1f;
|
||||
mPhysics.add(myViscousDrag);
|
||||
|
||||
final float mBorder = 40;
|
||||
Box mBox = new Box(new Vector3f(mBorder, mBorder, mBorder), new Vector3f(width - mBorder, height - mBorder, 100 - mBorder));
|
||||
mBox.reflect(true);
|
||||
mPhysics.add(mBox);
|
||||
|
||||
/* create an attractor */
|
||||
mAttractor = new Attractor();
|
||||
mAttractor.radius(200);
|
||||
mAttractor.strength(-300);
|
||||
mPhysics.add(mAttractor);
|
||||
|
||||
|
||||
/* create trails and particles */
|
||||
mTrails = new Vector<ParticleTrail>();
|
||||
for (int i = 0; i < 500; i++) {
|
||||
Particle mParticle = mPhysics.makeParticle();
|
||||
mParticle.mass(2.0f);
|
||||
ParticleTrail myParticleTrail = new ParticleTrail(mPhysics, mParticle, 0.2f, random(0.5f, 1));
|
||||
myParticleTrail.mass(0.5f);
|
||||
mTrails.add(myParticleTrail);
|
||||
}
|
||||
resetParticles(width / 2, height / 2);
|
||||
}
|
||||
|
||||
void resetParticles(float x, float y) {
|
||||
for (ParticleTrail myTrails : mTrails) {
|
||||
myTrails.particle().position().set(x + random(-10, 10), y + random(-10, 10), 0);
|
||||
myTrails.particle().velocity().set(random(-10, 10), random(-10, 10), random(-10, 10));
|
||||
myTrails.fragments().clear();
|
||||
}
|
||||
}
|
||||
|
||||
void draw() {
|
||||
/* set attractor to mouse position */
|
||||
mAttractor.position().set(mouseX, mouseY);
|
||||
|
||||
for (ParticleTrail myTrails : mTrails) {
|
||||
myTrails.loop(1f / frameRate);
|
||||
}
|
||||
|
||||
mPhysics.step(1f / frameRate);
|
||||
|
||||
background(255);
|
||||
for (ParticleTrail myTrail : mTrails) {
|
||||
drawTrail(myTrail);
|
||||
}
|
||||
}
|
||||
|
||||
void drawTrail(ParticleTrail theTrail) {
|
||||
|
||||
final Vector<Particle> mFragments = theTrail.fragments();
|
||||
final Particle mParticle = theTrail.particle();
|
||||
|
||||
/* draw head */
|
||||
if (mFragments.size() > 1) {
|
||||
fill(255, 0, 127);
|
||||
noStroke();
|
||||
pushMatrix();
|
||||
translate(mParticle.position().x,
|
||||
mParticle.position().y,
|
||||
mParticle.position().z);
|
||||
sphereDetail(4);
|
||||
sphere(3);
|
||||
popMatrix();
|
||||
}
|
||||
|
||||
/* draw trail */
|
||||
for (int i = 0; i < mFragments.size() - 1; i++) {
|
||||
if (mFragments.get(i) instanceof ShortLivedParticle) {
|
||||
final float mRatio = 1.0f - ((ShortLivedParticle)mFragments.get(i)).ageRatio();
|
||||
stroke(127, mRatio * 255);
|
||||
strokeWeight(mRatio * 3);
|
||||
}
|
||||
int j = (i + 1) % mFragments.size();
|
||||
line(mFragments.get(i).position().x,
|
||||
mFragments.get(i).position().y,
|
||||
mFragments.get(i).position().z,
|
||||
mFragments.get(j).position().x,
|
||||
mFragments.get(j).position().y,
|
||||
mFragments.get(j).position().z);
|
||||
}
|
||||
if (!mFragments.isEmpty()) {
|
||||
line(mFragments.lastElement().position().x,
|
||||
mFragments.lastElement().position().y,
|
||||
mFragments.lastElement().position().z,
|
||||
mParticle.position().x,
|
||||
mParticle.position().y,
|
||||
mParticle.position().z);
|
||||
}
|
||||
}
|
||||
|
||||
void mousePressed() {
|
||||
resetParticles(mouseX, mouseY);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
import mathematik.*;
|
||||
|
||||
import teilchen.test.cubicle.*;
|
||||
import teilchen.integration.*;
|
||||
import verhalten.*;
|
||||
import teilchen.*;
|
||||
import verhalten.view.*;
|
||||
import teilchen.test.particle.behavior.*;
|
||||
import teilchen.behavior.*;
|
||||
import teilchen.demo.*;
|
||||
import teilchen.test.particle.springs.*;
|
||||
import teilchen.gestalt.test.*;
|
||||
import teilchen.cubicle.*;
|
||||
import verhalten.test.*;
|
||||
import teilchen.force.flowfield.*;
|
||||
import teilchen.test.particle.*;
|
||||
import teilchen.gestalt.util.*;
|
||||
import teilchen.constraint.*;
|
||||
import teilchen.force.vectorfield.*;
|
||||
import teilchen.force.*;
|
||||
import teilchen.util.*;
|
||||
|
||||
import mathematik.*;
|
||||
|
||||
import teilchen.Physics;
|
||||
import teilchen.force.Attractor;
|
||||
import teilchen.force.Gravity;
|
||||
import teilchen.force.Spring;
|
||||
import teilchen.force.ViscousDrag;
|
||||
import teilchen.integration.RungeKutta;
|
||||
import teilchen.util.Overlap;
|
||||
import teilchen.util.StickMan;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* this demo shows some advanced use of particles, springs and attractors to create stickmen.
|
||||
*/
|
||||
|
||||
Physics mPhysics;
|
||||
|
||||
Attractor mAttractor;
|
||||
|
||||
Gravity mGravity;
|
||||
|
||||
ViscousDrag mViscousDrag;
|
||||
|
||||
StickMan[] mMyStickMan;
|
||||
|
||||
void setup() {
|
||||
size(640, 480, OPENGL);
|
||||
smooth();
|
||||
frameRate(60);
|
||||
noFill();
|
||||
|
||||
mPhysics = new Physics();
|
||||
mPhysics.setInegratorRef(new RungeKutta());
|
||||
|
||||
mGravity = new Gravity();
|
||||
mGravity.force().y = 20;
|
||||
mPhysics.add(mGravity);
|
||||
|
||||
mViscousDrag = new ViscousDrag();
|
||||
mViscousDrag.coefficient = 0.85f;
|
||||
mPhysics.add(mViscousDrag);
|
||||
|
||||
mAttractor = new Attractor();
|
||||
mAttractor.radius(500);
|
||||
mAttractor.strength(0);
|
||||
mAttractor.position().set(width / 2, height / 2);
|
||||
mPhysics.add(mAttractor);
|
||||
|
||||
mMyStickMan = new StickMan[20];
|
||||
for (int i = 0; i < mMyStickMan.length; i++) {
|
||||
mMyStickMan[i] = new StickMan(mPhysics, random(0, width), random(0.3f, 0.6f));
|
||||
}
|
||||
}
|
||||
|
||||
void draw() {
|
||||
|
||||
mPhysics.step(1f / 60f);
|
||||
Overlap.resolveOverlap(mPhysics.particles());
|
||||
|
||||
/* constraint particles */
|
||||
for (int i = 0; i < mPhysics.particles().size(); i++) {
|
||||
if (mPhysics.particles(i).position().y > height - 10) {
|
||||
mPhysics.particles(i).position().y = height - 10;
|
||||
}
|
||||
if (mPhysics.particles(i).position().x > width) {
|
||||
mPhysics.particles(i).position().x = width;
|
||||
}
|
||||
if (mPhysics.particles(i).position().x < 0) {
|
||||
mPhysics.particles(i).position().x = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* handle particles */
|
||||
if (mousePressed) {
|
||||
mAttractor.position().set(mouseX, mouseY);
|
||||
if (mouseButton == RIGHT) {
|
||||
mAttractor.strength(-500);
|
||||
mAttractor.radius(500);
|
||||
}
|
||||
else {
|
||||
mAttractor.strength(500);
|
||||
mAttractor.radius(100);
|
||||
}
|
||||
}
|
||||
else {
|
||||
mAttractor.strength(0);
|
||||
}
|
||||
|
||||
if (keyPressed) {
|
||||
mGravity.force().y = -10;
|
||||
}
|
||||
else {
|
||||
mGravity.force().y = 20;
|
||||
}
|
||||
|
||||
/* draw */
|
||||
background(255);
|
||||
|
||||
/* draw springs */
|
||||
stroke(0, 20);
|
||||
for (int i = 0; i < mPhysics.forces().size(); i++) {
|
||||
if (mPhysics.forces(i) instanceof Spring) {
|
||||
Spring mySpring = (Spring)mPhysics.forces(i);
|
||||
line(mySpring.a().position().x,
|
||||
mySpring.a().position().y,
|
||||
mySpring.b().position().x,
|
||||
mySpring.b().position().y);
|
||||
}
|
||||
}
|
||||
|
||||
/* draw particles */
|
||||
for (int i = 0; i < mPhysics.particles().size(); i++) {
|
||||
ellipse(mPhysics.particles(i).position().x,
|
||||
mPhysics.particles(i).position().y, 5, 5);
|
||||
}
|
||||
|
||||
/* draw man */
|
||||
for (int i = 0; i < mMyStickMan.length; i++) {
|
||||
mMyStickMan[i].draw(g);
|
||||
}
|
||||
}
|
||||
|
||||
+184
@@ -0,0 +1,184 @@
|
||||
import mathematik.*;
|
||||
|
||||
import teilchen.Particle;
|
||||
import teilchen.Physics;
|
||||
import teilchen.constraint.AngleConstraintStick;
|
||||
import teilchen.constraint.Stick;
|
||||
import teilchen.force.AngleConstraintSpring;
|
||||
import teilchen.force.Gravity;
|
||||
import teilchen.force.Spring;
|
||||
import teilchen.force.ViscousDrag;
|
||||
import teilchen.integration.RungeKutta;
|
||||
|
||||
|
||||
Physics mPhysics;
|
||||
|
||||
Particle mParticleA;
|
||||
|
||||
Particle mParticleB;
|
||||
|
||||
Particle mParticleC;
|
||||
|
||||
Particle mParticleD;
|
||||
|
||||
AngleConstraintSpring mAngleConstraintABC;
|
||||
|
||||
AngleConstraintStick mAngleConstraintBCD;
|
||||
|
||||
void setup() {
|
||||
size(640, 480);
|
||||
frameRate(30);
|
||||
smooth();
|
||||
|
||||
mPhysics = new Physics();
|
||||
mPhysics.setInegratorRef(new RungeKutta());
|
||||
|
||||
ViscousDrag myViscousDrag = new ViscousDrag();
|
||||
myViscousDrag.coefficient = 1f;
|
||||
mPhysics.add(myViscousDrag);
|
||||
|
||||
Gravity myGravity = new Gravity();
|
||||
myGravity.force().y = 50;
|
||||
mPhysics.add(myGravity);
|
||||
|
||||
/* particles */
|
||||
mParticleA = mPhysics.makeParticle();
|
||||
mParticleB = mPhysics.makeParticle();
|
||||
mParticleC = mPhysics.makeParticle();
|
||||
mParticleD = mPhysics.makeParticle();
|
||||
|
||||
mParticleA.position().set(width / 2 + 50, height / 3);
|
||||
mParticleB.position().set(width / 2, height - height / 1.75f);
|
||||
mParticleC.position().set(width / 2, height - height / 4);
|
||||
mParticleD.position().set(width / 2, height - height / 8);
|
||||
|
||||
mParticleA.radius(7);
|
||||
mParticleB.radius(3);
|
||||
mParticleC.radius(10);
|
||||
mParticleD.radius(2);
|
||||
|
||||
mParticleB.fixed(true);
|
||||
|
||||
/* springs */
|
||||
Spring mSpringAB = new Spring(mParticleA, mParticleB);
|
||||
mSpringAB.strength(250);
|
||||
mSpringAB.damping(10);
|
||||
mPhysics.add(mSpringAB);
|
||||
|
||||
Spring mSpringBC = new Spring(mParticleB, mParticleC);
|
||||
mSpringBC.strength(250);
|
||||
mSpringBC.damping(10);
|
||||
mPhysics.add(mSpringBC);
|
||||
|
||||
Stick mSpringCD = new Stick(mParticleC, mParticleD);
|
||||
mSpringCD.damping(1);
|
||||
mPhysics.add(mSpringCD);
|
||||
|
||||
/* angle constraint */
|
||||
mAngleConstraintABC = new AngleConstraintSpring(mParticleA, mParticleB, mParticleC);
|
||||
mAngleConstraintABC.min_angle(PI * 0.5f);
|
||||
mAngleConstraintABC.damping(1);
|
||||
mAngleConstraintABC.strength(200);
|
||||
mPhysics.add(mAngleConstraintABC);
|
||||
|
||||
mAngleConstraintBCD = new AngleConstraintStick(mParticleB, mParticleC, mParticleD);
|
||||
mAngleConstraintBCD.min_angle(PI * 0.8f);
|
||||
mAngleConstraintBCD.damping(0.5f);
|
||||
mPhysics.add(mAngleConstraintBCD);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
/* attach particle to mouse */
|
||||
if (mousePressed) {
|
||||
mParticleA.position().set(mouseX, mouseY);
|
||||
}
|
||||
|
||||
/* apply constraints */
|
||||
mAngleConstraintABC.pre_step();
|
||||
mAngleConstraintBCD.pre_step();
|
||||
draw_physics();
|
||||
|
||||
mPhysics.step(1f / frameRate);
|
||||
|
||||
/* remove contraints */
|
||||
mAngleConstraintABC.post_step();
|
||||
mAngleConstraintBCD.post_step();
|
||||
}
|
||||
|
||||
void draw_physics() {
|
||||
background(255);
|
||||
|
||||
drawSprings();
|
||||
drawSticks();
|
||||
drawParticles();
|
||||
}
|
||||
|
||||
void drawSprings() {
|
||||
for (int i = 0; i < mPhysics.forces().size(); i++) {
|
||||
if (mPhysics.forces(i) instanceof Spring) {
|
||||
final Spring mSpring = (Spring)mPhysics.forces(i);
|
||||
if (mSpring instanceof AngleConstraintSpring) {
|
||||
strokeWeight(1);
|
||||
if (mSpring.active()) {
|
||||
stroke(255, 0, 0, 64);
|
||||
}
|
||||
else {
|
||||
stroke(255, 0, 0, 16);
|
||||
}
|
||||
}
|
||||
else {
|
||||
strokeWeight(3);
|
||||
stroke(0, 128);
|
||||
}
|
||||
line(mSpring.a(), mSpring.b());
|
||||
}
|
||||
}
|
||||
strokeWeight(1);
|
||||
}
|
||||
|
||||
void drawSticks() {
|
||||
for (int i = 0; i < mPhysics.constraints().size(); i++) {
|
||||
if (mPhysics.constraints(i) instanceof Stick) {
|
||||
final Stick mStick = (Stick)mPhysics.constraints(i);
|
||||
if (mStick instanceof AngleConstraintStick) {
|
||||
strokeWeight(1);
|
||||
if (mStick.active()) {
|
||||
stroke(0, 127, 255, 64);
|
||||
}
|
||||
else {
|
||||
stroke(0, 127, 255, 16);
|
||||
}
|
||||
}
|
||||
else {
|
||||
strokeWeight(3);
|
||||
stroke(0, 128);
|
||||
}
|
||||
line(mStick.a(), mStick.b());
|
||||
}
|
||||
}
|
||||
strokeWeight(1);
|
||||
}
|
||||
|
||||
void drawParticles() {
|
||||
stroke(0);
|
||||
fill(92);
|
||||
drawParticle(mParticleA);
|
||||
fill(127);
|
||||
drawParticle(mParticleB);
|
||||
fill(192);
|
||||
drawParticle(mParticleC);
|
||||
fill(64);
|
||||
drawParticle(mParticleD);
|
||||
}
|
||||
|
||||
void drawParticle(Particle p) {
|
||||
ellipse(p.position().x,
|
||||
p.position().y,
|
||||
p.radius() * 2, p.radius() * 2);
|
||||
}
|
||||
|
||||
void line(Particle p1, Particle p2) {
|
||||
line(p1.position().x, p1.position().y,
|
||||
p2.position().x, p2.position().y);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
import mathematik.*;
|
||||
|
||||
import processing.opengl.*;
|
||||
|
||||
import teilchen.BehaviorParticle;
|
||||
import teilchen.Physics;
|
||||
import teilchen.behavior.Arrival;
|
||||
import teilchen.force.Spring;
|
||||
import teilchen.force.ViscousDrag;
|
||||
import teilchen.util.CollisionManager;
|
||||
|
||||
/**
|
||||
* this demo shows how to add behaviors to particles. in this example the
|
||||
* arrival behavior.
|
||||
*/
|
||||
|
||||
Physics mPhysics;
|
||||
|
||||
ArrayList<Duckling> mDucklings;
|
||||
|
||||
CollisionManager mCollision;
|
||||
|
||||
void setup() {
|
||||
size(640, 480, OPENGL);
|
||||
frameRate(60);
|
||||
smooth();
|
||||
colorMode(RGB, 1.0f);
|
||||
|
||||
/* physics */
|
||||
mPhysics = new Physics();
|
||||
|
||||
ViscousDrag myViscousDrag = new ViscousDrag();
|
||||
myViscousDrag.coefficient = 0.25f;
|
||||
mPhysics.add(myViscousDrag);
|
||||
|
||||
mCollision = new CollisionManager();
|
||||
mCollision.minimumDistance(25);
|
||||
|
||||
/* ducklings */
|
||||
mDucklings = new ArrayList<Duckling>();
|
||||
for (int i = 0; i < 13; i++) {
|
||||
final Duckling mDuckling = new Duckling();
|
||||
if (!mDucklings.isEmpty()) {
|
||||
mDuckling.arrival.setPositionRef(mDucklings.get(mDucklings.size()-1).particle.position());
|
||||
}
|
||||
mCollision.collision().add(mDuckling.particle);
|
||||
mDucklings.add(mDuckling);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void draw() {
|
||||
final float mDeltaTime = 1.0f / frameRate;
|
||||
background(1);
|
||||
|
||||
/* update particles */
|
||||
mCollision.createCollisionResolvers();
|
||||
mCollision.loop(mDeltaTime);
|
||||
mPhysics.step(mDeltaTime);
|
||||
|
||||
drawCollisionSprings();
|
||||
mCollision.removeCollisionResolver();
|
||||
|
||||
mDucklings.get(0).arrival.oversteer(!mousePressed);
|
||||
mDucklings.get(0).arrival.position().set(mouseX, mouseY);
|
||||
|
||||
/* draw */
|
||||
for (int i=0; i < mDucklings.size(); i++) {
|
||||
Duckling mDuckling = mDucklings.get(i);
|
||||
drawParticle(mDuckling);
|
||||
}
|
||||
|
||||
/* draw arrival */
|
||||
stroke(0, 0.25f);
|
||||
noFill();
|
||||
ellipse(mDucklings.get(0).arrival.position().x,
|
||||
mDucklings.get(0).arrival.position().y,
|
||||
20, 20);
|
||||
}
|
||||
|
||||
void drawParticle(Duckling pDuckling) {
|
||||
final BehaviorParticle mParticle = pDuckling.particle;
|
||||
final Arrival mArrival = pDuckling.arrival;
|
||||
|
||||
/* draw particle */
|
||||
stroke(0, 0.5f);
|
||||
noFill();
|
||||
if (mArrival.arriving()) {
|
||||
stroke(1, 0, 0, 0.5f);
|
||||
}
|
||||
if (mArrival.arrived()) {
|
||||
stroke(0, 1, 0, 0.5f);
|
||||
}
|
||||
ellipse(mParticle.position().x, mParticle.position().y,
|
||||
mParticle.radius() * 2, mParticle.radius() * 2);
|
||||
|
||||
/* - */
|
||||
pushMatrix();
|
||||
translate(mParticle.position().x,
|
||||
mParticle.position().y);
|
||||
|
||||
/* draw velocity */
|
||||
stroke(1, 0, 0, 0.5f);
|
||||
line(0, 0, mParticle.velocity().x, mParticle.velocity().y);
|
||||
|
||||
/* draw break force */
|
||||
stroke(0, 0.5f, 1, 0.5f);
|
||||
line(0, 0, mArrival.force().x, mArrival.force().y);
|
||||
|
||||
/* - */
|
||||
popMatrix();
|
||||
}
|
||||
|
||||
void drawCollisionSprings() {
|
||||
stroke(0, 1, 0, 0.25f);
|
||||
for (int i = 0; i < mCollision.collision().forces().size(); ++i) {
|
||||
if (mCollision.collision().forces().get(i) instanceof Spring) {
|
||||
Spring mySpring = (Spring) mCollision.collision_forces().get(i);
|
||||
line(mySpring.a().position().x, mySpring.a().position().y, mySpring.a().position().z,
|
||||
mySpring.b().position().x, mySpring.b().position().y, mySpring.b().position().z);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Duckling {
|
||||
|
||||
BehaviorParticle particle;
|
||||
|
||||
Arrival arrival;
|
||||
|
||||
Duckling() {
|
||||
/* create particles */
|
||||
particle = mPhysics.makeParticle(BehaviorParticle.class);
|
||||
particle.position().set(random(width), random(height));
|
||||
particle.maximumInnerForce(random(50, 150));
|
||||
particle.radius(random(6, 10));
|
||||
|
||||
arrival = new Arrival();
|
||||
arrival.breakforce(random(12, 28));
|
||||
arrival.breakradius(random(45, 55));
|
||||
|
||||
particle.behaviors().add(arrival);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user