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.
Page Index Toggle Pages: 1
Vector math (Read 650 times)
Vector math
Feb 11th, 2006, 6:25pm
 
I am working on a project where I want to model a dandelion  seeds in 3D.

I have a sketch working where the seeds can be drawn and move around in 2D, but I have run into problems trying to take it into 3D.

The source files for the 2D sketch are available here:
http://www.larsjessen.dk/dandelion/dandelionSeeds2D.zip

I have made a sketch to test that the wings of the seed are drawn at the right angle in relation to the seed's stem end point, but can't seem to get it right. I have included my code below and the source can also be downloaded at
http://www.larsjessen.dk/dandelion/angleTest7.zip
which includes the vecmath.jar
I am basically trying to find the correct angles that I need to rotate to draw things correctly in relation to the seed stem vector. Do you have any suggestions?

import javax.vecmath.*;
import processing.opengl.*;

Vector3f centerToStem, stem, wing;
Point3f center, stemStart, stemEnd;
float seedLength = 30.0f;

void setup(){
 size(1024,768,OPENGL);
 center = new Point3f(width/2, height/2, 0);
 stemStart = new Point3f();
 stemEnd = new Point3f();
 centerToStem = new Vector3f();
 stem = new Vector3f();
 wing = new Vector3f();
 stemStart.z = 0;
}

void draw(){
 background(255);
 
 //update points
 stemStart.x = mouseX;
 stemStart.y = mouseY;
   
 noStroke();
 fill(255,0,0);
 ellipse(center.x,center.y,10,10);
 pushMatrix();
 translate(stemStart.x,stemStart.y,stemStart.z);
 ellipse(0,0,5,5);
 popMatrix();

 centerToStem.x = stemStart.x - center.x;
 centerToStem.y = stemStart.y - center.y;
 centerToStem.z = stemStart.z - center.z;
 
 float ratio = seedLength/centerToStem.length();
 //println(ratio);
 stemEnd.x = centerToStem.x*ratio + stemStart.x;
 stemEnd.y = centerToStem.y*ratio + stemStart.y;
 stemEnd.z = centerToStem.z*ratio + stemStart.z;
 stroke(0,0,255);
 line(stemStart.x,stemStart.y,stemStart.z,stemEnd.x,stemEnd.y,stemEnd.z);
 
 translate(stemEnd.x,stemEnd.y,stemEnd.z);
 stem.x = stemEnd.x - stemStart.x;
 stem.y = stemEnd.y - stemStart.y;
 stem.z = stemEnd.z - stemStart.z;
 
 Vector3f zxProjection = new Vector3f(stem.x,0,stem.z);
 float Zrotation = stem.angle(zxProjection);
 
 if(stem.x<0 && stem.y>0) Zrotation = PI - Zrotation;
 if(stem.x<0 && stem.y<0) Zrotation = PI + Zrotation;
 if(stem.x>0 && stem.y<0) Zrotation = -Zrotation;
 rotateZ(Zrotation);
 println(Zrotation);
 
 Vector3f xAxis = new Vector3f(1,0,0);
 float Yrotation = zxProjection.angle(xAxis);
 if(stem.x<0 && stem.z>0) Yrotation = PI - Yrotation;
 if(stem.x<0 && stem.z<0) Yrotation = PI + Yrotation;
 if(stem.x>0 && stem.z<0) Yrotation = -Yrotation;
 if(stem.z == 0) Yrotation = 0;
 println(Yrotation);
 rotateY(Yrotation);
 
 stroke(0,255,0);
 for(int i = 0; i < 10; i++){
   pushMatrix();
   rotateX(TWO_PI*i/10);
   line(0,0,10,25);
   popMatrix();
 }
}


void keyPressed(){
 if(keyCode == UP){
   stemStart.z = stemStart.z + 1;
 }
 else if(keyCode == DOWN){
   stemStart.z = stemStart.z - 1;
 }
}
Page Index Toggle Pages: 1