 # How to loop (adding time)?

#1

OsciStudio Version:0.6.0.38pre2
Operating System: win11

Hello,
I recently purchased and am thrilled.
But how to draw Lorenz attractor ?

``````SLIDER(X);
SLIDER(Y);
SLIDER(Z);
SLIDER(TIME);

float mX = 0.1;
float mY = 0.1;
float mZ = 0.1;

float a = 10;
float b = 28;
float c = 8.0 / 3.0;

vec3 gen(float t){
vec3 v;

v = input(t);
float mDX = a * (mY - mX);
float mDY = b * mX - mY - mX * mZ;
float mDZ = mX * mY - c * mZ;

v.x = v.x + (mDX * TIME);
v.y = v.y + (mDY * TIME);
v.z = v.z + (mDZ * TIME);

return v;
}
``````

thanks.

sry X, Y, Z is unused for now

#2

Hello,
Seems to be moving but how i create longest curve ?
This is Rossler attractor

``````SLIDER(Scale, 0.001..1.0);
SLIDER(Focal_Len, 0..1);
SLIDER(Camera_Z, 0..1);
float mX = 0.1;
float mY = 0.1;
float mZ = 0.1;

float a = 0.2;
float b = 0.2;
float c = 5.7;

vec3 v(mX, mY, mZ);
float mDT = 0.0001;

void update(){
float mDX = -mY - mZ;
float mDY = mX + a * mY;
float mDZ = b + mZ * (mX - c);

mX = mX + (mDX * mDT);
mY = mY + (mDY * mDT);
mZ = mZ + (mDZ * mDT);

//mX = mX * Focal_Len / (mZ - Camera_Z);
//mY = mY * Focal_Len / (mZ - Camera_Z);

v = {mX * Scale, mY * Scale, mZ * Scale};
};
vec3 gen(float t){
//v = input(t);
return v;
``````

}

thanks

#3

partially solved with mDT=0.02

#4

one possibility is to move your rossler equations into update() method and use a PathRecorder.

here is a similar example… you will need to press “play” for this to work.

``````SLIDER(center_x, -1..1);
SLIDER(center_y, -1..1);
SLIDER(k, 0..1);
SLIDER(timebase, 0.1..10).setDefaultValue(1);

#include <PathRecorder.h>

vec3 pos;
vec3 vel;

PathRecorder<100> history;

void setup(){
// give some initial position and velocity
pos = vec3(0, 0.5);
vel = vec3(0, 0.1);
}

void update(){
int subdiv = 128;
if(beatgrid(1,subdiv)){
vec3 delta = (pos - vec3(center_x, center_y));
float len_x = abs(delta.x);
float len_y = abs(delta.y);

// F=-kx is roughly hooks law, i think
vec3 F = -k*delta;

// use F=ma, assume m=0.001 and solve. so a = F/m = F/0.0001
vec3 acc = F/0.0001;

// now integrate
double dt = 1.0/subdiv;
vel += acc*dt;
pos += vel*dt;

// a bit nasty, but we dissipate some energy. friction maybe?
vel -= vel*dt;

// also nasty, but we clamp the position
pos.x = clamp(pos.x, -1, 1);
pos.y = clamp(pos.y, -1, 1);

}
}

vec3 gen(float t){
vec3 v;

v = history.gen(tri(t), false);

return v;
}
``````

ps. the `if(beatgrid(1,subdiv))` ties the simulation speed to the currently set bpm. you can eliminate this, but then the equation will run differently depending on your sample rate.

an alternative to this is to add a tick phasor at the top, like `PHASOR(tick).setRange("freq",1,50)` and use `if(tick.didReset)` in the update method. this gives you completely steady simulation updates without any binding to sample rate or bpm.

#5

pps. as a little general thing: doing all the work in update() and only synthesis in gen() has the advantage that everything works as expected when you stack more stuff like trace and grid after the livecoding.

#6

Thanks, this is it !
Working perfectly.