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 › 2d Mouse Coords to a 3d space (3d wire drawing)
Page Index Toggle Pages: 1
2d Mouse Coords to a 3d space (3d wire drawing) (Read 1979 times)
2d Mouse Coords to a 3d space (3d wire drawing)
Jul 21st, 2009, 11:09am
 
Hi,

I am creating a sketch similar in drawing functionality to Amit Pitaru's Sonic Wire Scultpure and I am using the TUIO library ( http://www.tuio.org/?processing ) and  MSARemote (http://www.memo.tv) to add multi-touch functionality.
Everything is working fine except for the z-rotation, I added a 2-finger gesture to rotate-Z but then the mouse coordinates do not match the line.

I used a little code from a post by Markavian here http://processing.org/discourse/yabb2/num_1117920593.html

I really wish my trig / understanding of 3d translations were better but I can't figure this out!

Can someone please tell me how to get my mouse's coordinates to remain relative to the z-rotation so that the line is drawn under the cursor

Here is my code (check applyRotation() & captureMouse()):
Code:


/**
* 3d drawing tool using TUIO library
* @author Kyle Phillips - http://labs.hapticdata.com
*/

import TUIO.*;
import processing.opengl.*;
import java.util.ArrayList;


TuioProcessing tuioClient;

float startRad = 0.0f;
float lastRad = 0.0f;
int depth = 50;
float x,y,z;
float angleX, angleY,angleZ;
float threshold = 0.3f;

int numCursors = 0;
ArrayList segments;
ArrayList currentSegment;


void setup()
{
 size(1280,720,OPENGL);
 segments = new ArrayList();

 frameRate(60);
 strokeWeight(2);
 background(64);

 tuioClient = new TuioProcessing(this);

}

void draw() {
 // clear screen
 background(64);
 stroke(255,50);
 strokeWeight(1);
 line(0,height/2,width,height/2);
 line(width/2,0,width/2,height);  
 if(mousePressed && currentSegment != null) {
   // if the mouse is being dragged
   // capture new mouse coordinates
   captureMouse();

 }

 Vector tuioCursorList = tuioClient.getTuioCursors();

 for( int j=0;j<tuioCursorList.size();j++)
 {
   TuioCursor tcur = (TuioCursor)tuioCursorList.elementAt(j);
   float vx = tcur.getXSpeed();
   float vy = tcur.getYSpeed();

   if(tuioCursorList.size() == 1)
   {
     lastRad = 0.0f;
     if(abs(vx) > threshold)angleX += radians(vx*-4);
     if(abs(vy) > threshold)angleY += radians(vy*-4);
   }
   else if(tuioCursorList.size() == 2)
   {
   
     //lastRotationZ -= rad;
     lastRad = getRotation(tuioCursorList);
     angleZ = startRad - lastRad;
     //println(angleZ*PI);
   }
   
   println(angleZ);
 }

 applyRotation();
 // draw the shape from the store arrays of points
 if(currentSegment != null) drawShape();
 
}


float getRotation(Vector tuioCursorList)
{
  float y1 = ((TuioCursor)tuioCursorList.elementAt(0)).getY();
     float y2 = ((TuioCursor)tuioCursorList.elementAt(1)).getY();
     float x1 = ((TuioCursor)tuioCursorList.elementAt(0)).getX();
     float x2 = ((TuioCursor)tuioCursorList.elementAt(1)).getX();
     return atan2(y2-y1,x2-x1);
}

// markavian (2005|07|15)
// Reritten rotation method for all vertexes
// taken from david huebner's FVector class (2005|06|05)
void applyRotation(){
 for(int j=0;j<segments.size();j++)
 {
   ArrayList seg = (ArrayList)segments.get(j);
   // for every existing vertex in the array...
   for(int i = 0; i < seg.size(); i++){

     float cosval, sinval;
     float tmp1, tmp2;

     PVector pv = (PVector) seg.get(i);
     x = pv.x;
     y = pv.y;
     z = pv.z;

     cosval = cos(angleY);
     sinval = sin(angleY);
     tmp1   = y*cosval - z*sinval;
     tmp2   = y*sinval + z*cosval;

     y = tmp1;
     z = tmp2;

     cosval = cos(angleX);
     sinval = sin(angleX);
     tmp1   = x*cosval - z*sinval;
     tmp2   = x*sinval + z*cosval;
     
     x = tmp1;
     z = tmp2;
   

     pv.set(x,y,z);
   }
 }
 angleX = 0;
 angleY = 0;
 
 translate(width/2,height/2,-height/2);
 rotateZ(-angleZ);
 translate(-width/2,-height/2,height/2);
}
// Draw the 'wire' onto the stage...
void drawShape(){

 // markavian (2005|07|15) added translate because of change to applyRotation method
 translate(width/2, height/2);

 stroke(255,255,255,100);
 strokeWeight(2);
 for(int l=0;l<segments.size();l++)
 {
   ArrayList seg = (ArrayList)segments.get(l);

   for(int j = 0; j < seg.size()-1; j++) {
     PVector lastPV = (PVector)seg.get(j);
     PVector currentPV = (PVector)seg.get(j+1);
     line(lastPV.x, lastPV.y,lastPV.z,currentPV.x,currentPV.y,currentPV.z);
   }
 }
}



void mousePressed() {
 currentSegment = new ArrayList();
 segments.add(currentSegment);
}




// Capture the mouse coordinates.
void captureMouse(){

 int xpoint = mouseX-width/2;
 int ypoint = mouseY-height/2;
 int zpoint = 1;
 currentSegment.add(new PVector(xpoint,ypoint,zpoint));

}

void keyPressed()
{
 if(key == 'c')
 {
   segments = new ArrayList();
 }
}

void updateTuioObject(TuioObject tobj)
{
}

void addTuioCursor(TuioCursor tcur)
{

 if(numCursors == 1)
 {
   Vector tuioCursorList = tuioClient.getTuioCursors();
   startRad = getRotation(tuioCursorList) + angleZ;
 }
 
 numCursors++;
}

void removeTuioCursor(TuioCursor tcur)
{
 numCursors--;
}



please help!
Re: 2d Mouse Coords to a 3d space (3d wire drawing)
Reply #1 - Jul 21st, 2009, 7:58pm
 
To fix this all you need to do is rotate the mouse vector by the same amount that you rotate the coordinate system. The easiest way to do this is to convert to polar coordinates, add the z-rotation angle, and then convert back to cartesian coordinates.

The new captureMouse() method
Code:

// Capture the mouse coordinates.
void captureMouse(){

 int xpoint = mouseX-width/2;
 int ypoint = mouseY-height/2;
 int zpoint = 1;
 
 // convert to polar coords
 float r = sqrt(xpoint*xpoint + ypoint*ypoint);
 float theta = atan2(ypoint, xpoint);
 
 // add the z-rotation
 theta += angleZ;
 
 // convert back to cartesian coords
 xpoint = int(round(r * cos(theta)));
 ypoint = int(round(r * sin(theta)));
 
 currentSegment.add(new PVector(xpoint,ypoint,zpoint));

}


I must admit I am a bit jealous. I wish I had a TUIO table to play around with!
Re: 2d Mouse Coords to a 3d space (3d wire drawing)
Reply #2 - Jul 24th, 2009, 1:16pm
 
thank you so much!

I'm really glad that you were able to provide me with a solution and an explanation; I completely understand, thanks again!

-Kyle
Re: 2d Mouse Coords to a 3d space (3d wire drawing)
Reply #3 - Jul 24th, 2009, 1:20pm
 
I actually don't have a TUIO table (I've got a multi-touch table but I'm using ofxTouch on that), I'm using MSARemote for the iPhone for this project. Memo Atken made it (link above).

thanks for a third time!
Page Index Toggle Pages: 1