We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am trying to convert my root system sketch from 2D to 3D so I can rotate around it and be able to see it from different angles. My friend told me about using PVectors to implement the z-axis but I am not sure about how to do that. Any body help? Sorry I am new to using a forum so forgive me if I am not asking properly or giving enough information. My code is below:
Snake[] snakes = new Snake[0];
void setup() {
background(0);
size(1200, 600,P3D);
smooth();
for (int k = 0; k < 10; k++){
snakes = (Snake[]) append(snakes, new Snake(width/2, height/2, 15, random(100), 1));
}
}
void draw() {
for(int i = 0; i < snakes.length; i++) {
if(!snakes[i].done()){
snakes[i].go();
stroke(random(255), 0, 0, 50);}
}
}
class Snake {
float X;
float Y;
float rot;
float V;
float tm;
int fm;
Snake(int tX, int tY, float tfm, float trot, float tV) {
X = tX;
Y = tY;
rot = trot;
tm = tfm;
V = tV;
}
void go() {
V += random(-0.01, 0.01);
tm /= 1.01;
strokeWeight(tm);
rot += random(-0.2, 0.2);
line( X,Y, X + V*sin(rot), Y + V*cos(rot));
line(X, Y, X + V*sin(rot), Y + V*cos(rot));
X += V*sin(rot);
Y += V*cos(rot);
fm++;
if(random(400) > 398.5-(fm/20)) {
snakes = (Snake[]) append(snakes, new Snake(int(X), int(Y), tm, rot + random(-0.2, 0.2), V));
}
}
boolean done() {
if(tm < 0.01) {
return true;
}else{
return false;
}
}
}
Answers
PeasyCam library
but that won't work because you're only drawing one step at a time - if you start rotating the screen then only the things you draw after rotating will be rotated, not the whole thing.
Do you want the root system to grow in 3d, and then be able to rotate it. Or have the existing 2d root system that you can rotate in 3d?
I would love for it to be able to grow in 3D, even if I couldn't rotate it, growing in three dimensions would be enough. Would you able to show me how to do that?
In line 31/33 add float Z;
Line 51 is the same as 52
It might be faster to store old x and y and calc new x and y draw the line between them
(Swap 53/54 and 51)
to go 3D add a calc for Z here
Use Z in the line() command with 6 Parameters
To fight what koogs said you can put the 3D data in a arraylist and display this
but you'll have to redraw the entire structure every time (or at least when it changes)
but the display will still be in 2d so the third dimension is a waste
take a piece of paper. draw a cube. is that 3d or 2d?
This is all great guys, but I am having trouble following how to actually convert my code to have a Z axis and make it 3D. Can anyone show me what the code would look like?
that is one approach.
The snakes are only used in setup() and are just there to help to build the lines (in the ArrayList lines. An ArrayList is a more comfy array).
you don't watch the tree grow, it is just there (I think it would be too slow otherwise)
it also makes use of a more advanced line which can take shadows
this function drawline by James Carruthers basically makes a long box between our 2 points
Thank you. Definitely in the direction I want my project to go! Awesome
You could animate it by varying how much of the pre-calculated tree you draw with each frame (assuming the trunk is drawn first). That would actually be less work (ie faster) than drawing the whole tree ever time.
If you don't want animation but want better efficiency then pshapes would probably be better, as they'd reduce the number of internal calls to the renderer by batching things up. That's badly explained, it is early here 8)
2 good ideas, thank you!
(Not sure the second would work with your Lines though, they wouldn't rotate to face the camera...)
Dunno