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.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › using Vec3D (or 3d directions in general)
Page Index Toggle Pages: 1
using Vec3D (or 3d directions in general) (Read 1202 times)
using Vec3D (or 3d directions in general)
Jul 10th, 2008, 3:34pm
 
Hi guys, I'm trying to play with 3D vectors, I'm using toxi's ones but my problem is about 3d in general).

I've a motion defined by a Vec3D. I can easily draw the direction of it doing:
Code:

private void drawVec(Vec3D v, Point3D p, float radius)
{
float ax = getContextualAngle(v.x);
float ay = getContextualAngle(v.y);
float az = getContextualAngle(v.z);

float dx = radius * sin(ax);
float dy = radius * sin(ay);
float dz = radius * sin(az);

Point3D newP = new Point3D(p.x + dx, p.y + dy, p.z + dz);
drawLine(0x00FF00, p,newP);
}


where getContextualAngle is simply
Code:

private float getContextualAngle(float vecAngle)
{
// R90 is radians(90)
return map(vecAngle, -1, 1, -R90, R90);
}


I would like to define that circle perpendicular to the vector (each point is equally distant from the next point resulting from the vector).

As start I thought to try and drawing it, so one translated to a point rotating the camera I could simply drawing it, but my problem is of course finding out the rotations to apply. My problem I presume is also in understanding if I have to rotate only on two axis.

Any tips a part study geometry? Smiley


Thanks, chr
Re: using Vec3D (or 3d directions in general)
Reply #1 - Jul 10th, 2008, 3:57pm
 
I really have no idea what your first piece of code is doing.. if I wanted to draw a line along the direction of motion, I'd just use:

Code:
void drawVec(Vec3D v, Point3D p)
{
drawLine(0x00FF00,p,new Point3D(p.x+v.x,p.y+v.y,p.z+v.z));
}


As for drawing a circle perpendicular to this, I did something ages ago that requires working such things out:
http://www.hardcorepawn.com/Anenome/ If you look in the Tube class, there's code which draws a cylinder from point a to point b (in my code they're Vertexes not Point3Ds but I believe it should still be able to copy most of it directly.
Re: using Vec3D (or 3d directions in general)
Reply #2 - Jul 10th, 2008, 4:03pm
 
Hi John, well, you are absolutely right about the inutility of that code (how newbie I am!) :S

now I converted to:
Code:

drawLine(0x00FF00, p, new Point3D(p.x + v.x * radius, p.y + v.y * radius, p.z + v.z * radius));


I'll check your class, thanks a lot!

chr
Re: using Vec3D (or 3d directions in general)
Reply #3 - Jul 10th, 2008, 6:09pm
 
I adapted your code to my case, now to draw the perpendicular my method is:

Code:

private void drawVec(Vec3D v, Point3D p, float radius)
{
Point3D next = new Point3D(p.x + v.x * radius, p.y + v.y * radius, p.z + v.z * radius);
drawLine(0x00FF00, p, next);
// draw circle
int RESOLUTION = 20;
float RH = RESOLUTION * .5f;
float heading, pitch;

Point3D[] points = new Point3D[RESOLUTION];

   float len=sqrt((next.x-p.x)*(next.x-p.x)+(next.y-p.y)*(next.y-p.y)+(next.z-p.z)*(next.z-p.z));
   len/=2;
   Point3D mid = new Point3D((p.x + next.x) / 2,
(p.y + next.y) / 2,
((p.z + next.z) / 2));
heading = atan2(v.z, v.x) * -1;
pitch = atan2(v.y, sqrt((v.x * v.x) + (v.z * v.z)));
   
   float x, y, z;
   int i;
   for(i=0;i<RESOLUTION;i++)
   {
     // rod is horizontal along X to start
   Point3D cp=new Point3D(-len,radius*cos(i*(PI/RH)),radius*sin(i*PI/RH));
   //pitch, then heading
   x = cp.x;
   y = cp.y;
   cp.x = x*cos(pitch)+y*cos(pitch+HALF_PI);
   cp.y=x*sin(pitch)+y*sin(pitch+HALF_PI);
   z = cp.z;
   x = cp.x;
   cp.z=z*cos(heading)+x*cos(heading+HALF_PI);
   cp.x=z*sin(heading)+x*sin(heading+HALF_PI);

     //move tube so that it goes from one end to the other.      
   cp.x+=mid.x;
   cp.y+=mid.y;
   cp.z+=mid.z;
   points[i] = cp;
   }
   // draw
   fill(255,0,0);
   beginShape();
   Point3D s;
   for(i=0;i<points.length;i++)
   {
     s = points[i];
     vertex(s.x,s.y,s.z);
   }
   endShape();
}


Thanks John, chr
Page Index Toggle Pages: 1