Forums > Jitter

jitter java attractors

September 15, 2010 | 4:59 pm

Hello

I need help…
In this patch, (as attachment) I try to change the coordinates of the points of attraction
They are in the code Java:

var a_array = new Array(3); // coordonnées attractors
a_array[0] = 3.0; // coordonnée x du premier attracteur
a_array[1] = 5.0; // coordonnée y du premier attracteur
a_array[2] = 0.0; // coordonnée z du premier attracteur
attmat.setcell1d(0, p_array[0],p_array[1],p_array[2]);

a_array[0] = -3.0;
a_array[1] = 2.0;
a_array[2] = 5.0;
attmat.setcell1d(1, p_array[0],p_array[1],p_array[2]);

a_array[0] = 7.0;
a_array[1] = 0.0;
a_array[2] = -1.0;
attmat.setcell1d(2, p_array[0],p_array[1],p_array[2]);

I would like to control them in max/msp with number box
I do not know how to make it
Anybody could he help me?

Thank you very much
Serge

PATCH

– Pasted Max Patch, click to expand. –

JAVA

// inlets and outlets
inlets = 2;
outlets = 2;

function list(a)
{
if (inlet == 1)
{
// right inlet
post(a,"-");
}
else
{
// left inlet
post(a,"n");
}
}

var PARTICLE_COUNT = 1000; // initial number of particle vertices
var ATTRACTOR_COUNT = 3; // initial number of points of gravity

// create a [jit.noise] object for particle and velocity generation
var noisegen = new JitterObject("jit.noise");
noisegen.dim = PARTICLE_COUNT;
noisegen.planecount = 3;
noisegen.type = "float32";

// create a [jit.noise] object for attractor generation
var attgen = new JitterObject("jit.noise");
attgen.dim = ATTRACTOR_COUNT;
attgen.planecount = 3;
attgen.type = "float32";

// create two [jit.expr] objects for the bang_expr() function

// first expression: sum all the planes in the input matrix
var myexpr = new JitterObject("jit.expr");
myexpr.expr = "in[0].p[0]+in[0].p[1]+in[0].p[2]";
// second expression: evaluate a+((b-c)*d/e)
var myexpr2 = new JitterObject("jit.expr");
myexpr2.expr = "in[0]+((in[1]-in[2])*in[3]/in[4])";

// create the Jitter matrices we need to store our data

// matrix of x,y,z particle vertices
var particlemat = new JitterMatrix(3, "float32", PARTICLE_COUNT);
// matrix of x,y,z particle velocities
var velomat = new JitterMatrix(3, "float32", PARTICLE_COUNT);
// matrix of x,y,z points of attraction (gravity centers)
var attmat = new JitterMatrix(3, "float32", ATTRACTOR_COUNT);
// matrix for aggregate distances
var distmat = new JitterMatrix(3, "float32", PARTICLE_COUNT);
// temporary matrix for the bang_op() function
var tempmat = new JitterMatrix(3, "float32", PARTICLE_COUNT);
// temporary summing matrix for the bang_op() function
var summat = new JitterMatrix(1, "float32", PARTICLE_COUNT);
// another temporary summing matrix for the bang_op() function
var summat2 = new JitterMatrix(1, "float32", PARTICLE_COUNT);
// a scalar matrix to store the current gravity point
var scalarmat = new JitterMatrix(3, "float32", PARTICLE_COUNT);
// a scalar matrix to store acceleration (expr_op() function only)
var amat = new JitterMatrix(1, "float32", PARTICLE_COUNT);

var a = 0.001; // acceleration factor
var d = 0.01; // decay factor

var perform_mode="op"; // default perform function
var draw_primitive = "points"; // default drawing primitive

function loadbang() // execute this code when our Max patch opens
{
init(); // initialize our matrices
post("particles initialized.n");
}

function init()
// initialization routine… call at load, as well as
// when we change the number of particles or attractors
{
// generate a matrix of random particles spread between -1 and 1
noisegen.matrixcalc(particlemat, particlemat);
particlemat.op("*", 2.0);
particlemat.op("-", 1.0);
// generate a matrix of random velocities spread between -1 and 1
noisegen.matrixcalc(velomat, velomat);
velomat.op("*", 2.0);
velomat.op("-", 1.0);

// generate a matrix of random attractors spread between -1 and 1
// attgen.matrixcalc(attmat, attmat);
// attmat.op("*", 2.0);
// attmat.op("-", 1.0);

var a_array = new Array(3); // tableau de coordonnées pour un attracteur

//on définit les coordonnées de chaque attracteur

a_array[0] = 3.0; // coordonnée x du premier attracteur
a_array[1] = 5.0; // coordonnée y du premier attracteur
a_array[2] = 0.0; // coordonnée z du premier attracteur
attmat.setcell1d(0, p_array[0],p_array[1],p_array[2]);

a_array[0] = -3.0;
a_array[1] = 2.0;
a_array[2] = 5.0;
attmat.setcell1d(1, p_array[0],p_array[1],p_array[2]);

a_array[0] = 7.0;
a_array[1] = 0.0;
a_array[2] = -1.0;
attmat.setcell1d(2, p_array[0],p_array[1],p_array[2]);

}

function bang() // perform one iteration of our particle system
{
switch(perform_mode) { // choose from the following…
case "op": // use Jitter matrix operators
bang_op();
break;
case "expr": // use [jit.expr] for the bulk of the algorithm
bang_expr();
break;
case "iter": // iterate cell-by-cell through the matrices
bang_iter();
break;
default: // use bang_op() as our default
bang_op();
break;
}

// output our new matrix of particle vertices
// with the current drawing primitive
outlet(0, "jit_matrix", particlemat.name, draw_primitive);

}

function bang_op() // create our particle matrix using Matrix operators
{
for(var i = 0; i < ATTRACTOR_COUNT; i++)
// do one iteration per gravity point
{
// create a scalar matrix out of the current attractor:
scalarmat.setall(attmat.getcell(i));

// subtract our particle positions from the current attractor
// and store in a temporary matrix (x,y,z):
tempmat.op("-", scalarmat, particlemat);

// square to create a cartesian distance matrix (x*x, y*y, z*z):
distmat.op("*", tempmat, tempmat);

// sum the planes of the distance matrix (x*x+y*y+z*z)
summat.planemap = 0;
summat.frommatrix(distmat);
summat2.planemap = 1;
summat.frommatrix(distmat);
summat.op("+", summat, summat2);
summat2.planemap = 2;
summat2.frommatrix(distmat);
summat.op("+", summat, summat2);

// scale our distances by the acceleration value:
tempmat.op("*", a);
// divide our distances by the sum of the distances
// to derive gravity for this frame:
tempmat.op("/", summat);
// add to the current velocity bearings to get the
// amount of motion for this frame:
velomat.op("+", tempmat);
}

// offset our current positions by the amount of motion:
particlemat.op("+", velomat);
// reduce our velocities by the decay factor for the next frame:
velomat.op("*", d);
}

function bang_expr() // create our particle matrix using [jit.expr]
{
// create a scalar matrix out of our acceleration value:
amat.setall(a);

for(var i = 0; i < ATTRACTOR_COUNT; i++)
// do one iteration per gravity point
{
// create a scalar matrix out of the current attractor:
scalarmat.setall(attmat.getcell(i));
// subtract our particle positions from the current attractor
// and store in a temporary matrix (x,y,z):
tempmat.op("-", scalarmat, particlemat);
// square to create a cartesian distance matrix (x*x, y*y, z*z):
distmat.op("*", tempmat, tempmat);

// sum the planes of the distance matrix (x*x+y*y+z*z) :
// "in[0].p[0]+in[0].p[1]+in[0].p[2]" :
myexpr.matrixcalc(distmat, summat);

// derive amount of motion for this frame :
// "in[0]+((in[1]-in[2])*in[3]/in[4])" :
myexpr2.matrixcalc([velomat,scalarmat,particlemat,amat,summat], velomat);
}

// offset our current positions by the amount of motion:
particlemat.op("+", velomat);
// reduce our velocities by the decay factor for the next frame:
velomat.op("*", d);
}

function bang_iter() // create our particle matrix cell-by-cell
{
var p_array = new Array(3); // array for a single particle
var v_array = new Array(3); // array for a single velocity
var a_array = new Array(3); // array for a single attractor

for(var j = 0; j < PARTICLE_COUNT; j++)
// do one iteration per particle
{
// fill an array with the current particle:
p_array = particlemat.getcell(j);
// fill an array with the current particle’s velocity:
v_array = velomat.getcell(j);

for(var i = 0; i < ATTRACTOR_COUNT; i++)
// do one iteration per gravity point
{
// fill an array wi

(edited by admin, please use copy-compressed for patches)


Viewing 1 post (of 1 total)