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!