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 › Mouse coordinates do not correspond!!!
Page Index Toggle Pages: 1
Mouse coordinates do not correspond!!! (Read 1438 times)
Mouse coordinates do not correspond!!!
Apr 21st, 2009, 6:31am
 
Hello! I´m making a simple program where I use a particle attached to the mouse to play with a geometry in opengl using traer physics, my problem is when i try to draw on the center of my sketch the mouse coordinates get all messed up! I thought you could control this with pushMatrix and popMatrix am I wrong??? Here is an example of my code Code:
import traer.physics.*;
import processing.opengl.*;

Cube cubito;
int rota = 0;
int numCubos = 1;
Vec3D vertices[] = new Vec3D[numCubos*8];
ParticleSystem sistema;
Particle p[] = new Particle[numCubos*8];
Particle mouse;


void setup() {
size(800,600,OPENGL);
frameRate(100);
smooth();
sistema = new ParticleSystem(0,0.1);
mouse = sistema.makeParticle(10, mouseX, mouseY, 0);
cubito = new Cube(100,100,100);
vertices = cubito.getVertices();
for (int i=0; i < numCubos*8; i++) {
p[i] = sistema.makeParticle(5, vertices[i].x, vertices[i].y, vertices[i].z);
sistema.makeAttraction(mouse, p[i], 100, 10);
}

mouse.makeFixed();

for (int i = 0; i < numCubos*8; i++) {
for (int j = 0; j < numCubos*8; j++) {
if (i != j) {
sistema.makeSpring(p[i], p[j], 0.1, 0.1,100);
}
}
}

}





void draw() {
mouse.moveTo(mouseX,mouseY,0);
sistema.tick();
for (int i = 0; i < numCubos*8; i++) {
vertices[i].set(p[i].position().x(),p[i].position().y(),p[i].position().z());
}
cubito.setVertices(vertices[0], vertices[1], vertices[2], vertices[3], vertices[4],
vertices[5],vertices[6],vertices[7]);
background(255);
lights();
pushMatrix();
translate(width/2, height/2);
cubito.drawCube();
popMatrix();
}


There are some classes missing but i guess they are not needed for you to understand, if they are please let me know. Help help!!!!!!!!
thanks ;)
.leo
Re: Mouse coordinates do not correspond!!!
Reply #1 - Apr 21st, 2009, 8:04am
 
If you could post the class as well, it would be helpful.  It's much easier to figure something out if you have all the code and can run it.
Re: Mouse coordinates do not correspond!!!
Reply #2 - Apr 21st, 2009, 11:03am
 
Sure =) thx for the quick respond! here goes!
Code:
class Cube {

int w, h, d;
Vec3D vertice1, vertice2, vertice3, vertice4, vertice5, vertice6, vertice7, vertice8;

Cube(int w, int h, int d ){
this.w = w;
this.h = h;
this.d = d;
vertice1 = new Vec3D(-w/2,-h/2,-d/2);
vertice2 = new Vec3D(w,-h/2,-d/2);
vertice3 = new Vec3D(w,h,-d/2);
vertice4 = new Vec3D(-w/2,h,-d/2);
vertice5 = new Vec3D(-w/2,-h/2,d);
vertice6 = new Vec3D(w,-h/2,d);
vertice7 = new Vec3D(w,h,d);
vertice8 = new Vec3D(-w/2,h,d);

}

Cube(Vec3D v1, Vec3D v2, Vec3D v3, Vec3D v4, Vec3D v5, Vec3D v6, Vec3D v7, Vec3D v8) {
vertice1 = v1;
vertice2 = v2;
vertice3 = v3;
vertice4 = v4;
vertice5 = v5;
vertice6 = v6;
vertice7 = v7;
vertice8 = v8;

}

void setVertices(Vec3D v1, Vec3D v2, Vec3D v3, Vec3D v4, Vec3D v5, Vec3D v6, Vec3D v7, Vec3D v8) {
vertice1 = v1;
vertice2 = v2;
vertice3 = v3;
vertice4 = v4;
vertice5 = v5;
vertice6 = v6;
vertice7 = v7;
vertice8 = v8;

}

Vec3D[] getVertices() {
Vec3D temporal[] = new Vec3D[8];
temporal[0] = vertice1;
temporal[1] = vertice2;
temporal[2] = vertice3;
temporal[3] = vertice4;
temporal[4] = vertice5;
temporal[5] = vertice6;
temporal[6] = vertice7;
temporal[7] = vertice8;

return temporal;
}

void drawCube(){

beginShape(QUADS);
//cara frontal
vertex(vertice1.x, vertice1.y, vertice1.z);
vertex(vertice2.x, vertice2.y, vertice2.z);
vertex(vertice3.x, vertice3.y, vertice3.z);
vertex(vertice4.x, vertice4.y, vertice4.z);

//cara trasera
vertex(vertice5.x, vertice5.y, vertice5.z);
vertex(vertice6.x, vertice6.y, vertice6.z);
vertex(vertice7.x, vertice7.y, vertice7.z);
vertex(vertice8.x, vertice8.y, vertice8.z);

//cara izquierda
vertex(vertice1.x, vertice1.y, vertice1.z);
vertex(vertice4.x, vertice4.y, vertice4.z);
vertex(vertice8.x, vertice8.y, vertice8.z);
vertex(vertice5.x, vertice5.y, vertice5.z);

//cara derecha
vertex(vertice2.x, vertice2.y, vertice2.z);
vertex(vertice6.x, vertice6.y, vertice6.z);
vertex(vertice7.x, vertice7.y, vertice7.z);
vertex(vertice3.x, vertice3.y, vertice3.z);


// cara de arriba
vertex(vertice1.x, vertice1.y, vertice1.z);
vertex(vertice2.x, vertice2.y, vertice2.z);
vertex(vertice6.x, vertice6.y, vertice6.z);
vertex(vertice5.x, vertice5.y, vertice5.z);

//cara de abajo
vertex(vertice4.x, vertice4.y, vertice4.z);
vertex(vertice3.x, vertice3.y, vertice3.z);
vertex(vertice7.x, vertice7.y, vertice7.z);
vertex(vertice8.x, vertice8.y, vertice8.z);

endShape();
}
}


Thats the cube class and here is the Vec3D class from unlekker =)

Code:
// Vec3D - simple 3D vector class
// processing.unlekker.net

class Vec3D {
float x,y,z;

Vec3D(float _x,float _y,float _z) {
x=_x;
y=_y;
z=_z;
}

Vec3D(Vec3D v) {
x=v.x;
y=v.y;
z=v.z;
}

void set(float _x,float _y,float _z) {
x=_x;
y=_y;
z=_z;
}

void set(Vec3D v) {
x=v.x;
y=v.y;
z=v.z;
}

void add(float _x,float _y,float _z) {
x+=_x;
y+=_y;
z+=_z;
}

void add(Vec3D v) {
x+=v.x;
y+=v.y;
z+=v.z;
}

void sub(float _x,float _y,float _z) {
x-=_x;
y-=_y;
z-=_z;
}

void sub(Vec3D v) {
x-=v.x;
y-=v.y;
z-=v.z;
}

void mult(float m) {
x*=m;
y*=m;
z*=m;
}

void div(float m) {
x/=m;
y/=m;
z/=m;
}

float length() {
return sqrt(x*x+y*y+z*z);
}

void normalise() {
float l=length();
if(l!=0) {
x/=l;
y/=l;
z/=l;
}
}

void rotateX(float val) {

double cosval=Math.cos(val);
double sinval=Math.sin(val);
double tmp1=y*cosval - z*sinval;
double tmp2=y*sinval + z*cosval;

y=(float)tmp1;
z=(float)tmp2;
}

void rotateY(float val) {

double cosval=Math.cos(val);
double sinval=Math.sin(val);
double tmp1=x*cosval - z*sinval;
double tmp2=x*sinval + z*cosval;

x=(float)tmp1;
z=(float)tmp2;
}

void rotateZ(float val) {

double cosval=Math.cos(val);
double sinval=Math.sin(val);
double tmp1=x*cosval - y*sinval;
double tmp2=x*sinval + y*cosval;

x=(float)tmp1;
y=(float)tmp2;
}
}


Really appreciate this =)
Re: Mouse coordinates do not correspond!!!
Reply #3 - Apr 21st, 2009, 2:04pm
 
maybe when you translate you don't want it always to translate to height/2, width/2.
you could try translate(mouseX, mouseY); to consider the position of the mouse always as origin for the function drawCube(). i don't know if this would do what you want, though.
try rechecking the code to guarantee the way you deal with coordinates is coherent with what you want.
Re: Mouse coordinates do not correspond!!!
Reply #4 - Apr 22nd, 2009, 3:03pm
 
Mmmm thanks man, but that is not the effect that I intended what i wanted is very basic just draw the cube in the center of my sketch normally this works well in P3D but I don´t know why in OpenGL when i apply transformations with pushMatrix() and popMatrix() even though the transformation is applied the mouse coordinates are just off by the same amount I translated the cube, I just want to play with the cube sing the mouse but it detects it in a weird way =( dunno what the problem is! Thanks for your suggestion anyway =) any other ideas you have I´ll really appreciate it
Re: Mouse coordinates do not correspond!!!
Reply #5 - Apr 23rd, 2009, 10:54am
 
Try this...
mouse.moveTo(mouseX-width/2,mouseY-height/2,0);

I think your 2d environment has x=0, y=0 in the top left corner and your OpenGL 3d environment has x=0,y=0 in the center of the screen.
Re: Mouse coordinates do not correspond!!!
Reply #6 - Apr 27th, 2009, 2:03pm
 
Wow yeah I guess thats it! but it still is a little off but i think this is caused mainly because of the perspective of the camera in OpenGL someone correct me if I´m wrong. Because when the mouse is positioned exactly in the center of the sketch it matches perfectly but when moving towards the corners of the sketch it agains does not correspond. I will try and do it in ortho mode and tell you if it has something wrong.
Re: Mouse coordinates do not correspond!!!
Reply #7 - Apr 27th, 2009, 8:14pm
 
import processing.opengl.*;
void setup()
{
 size(800,600,OPENGL);
}

void draw()
{
 background(0);
 
 pushMatrix();
 //Use some Transformations:  
 translate(mouseX,mouseY);
 
 //Draw Stuff at z=0...
 //Mouse is exactly in the centre
 fill(0,100,255);noStroke();
 rect(-100,-100,200,200);
 stroke(255);
 line(-100,0,100,0);
 line(0,100,0,-100);
 
 //Draw Stuff at z=100...
 //Mouse is not in the centre
 //because the object is closer
 //to camera
 translate(0,0,100);
 
 fill(255,100,0,100);noStroke();
 rect(-100,-100,200,200);
 stroke(255,100);
 line(-100,0,100,0);
 line(0,100,0,-100);
 
 //Get the coordinates (with the current transformations):
 float[] leftUpCorner=_3Dto2D(-100,-100,0);
 float[] rightDownCorner=_3Dto2D(100,100,0);
 
 //Pop transformations  
 popMatrix();
 
 
 //Draw over the screen.(disabling the Z-Buffer)
 hint(DISABLE_DEPTH_TEST);
 
 noStroke();fill(255,200);
 
 rect(leftUpCorner[0]-5,leftUpCorner[1]-5,10,10);
 rect(rightDownCorner[0]-5,rightDownCorner[1]-5,10,10);
 
 hint(ENABLE_DEPTH_TEST);
 
 
}

float[] _3Dto2D(float X,float Y, float Z)
{
 //Calculate 3D coordinates to screen coordiantes
 float XX=screenX(X,Y,Z);
 float YY=screenY(X,Y,Z);
 return new float[]{XX,YY};
}



Try this sketch, the positions are correct. The near plane of your cube is
just closer to the camera. (It has not z=0). Have a look into the camera reference that could help.

Re: Mouse coordinates do not correspond!!!
Reply #8 - May 2nd, 2009, 4:15pm
 
That was really really really helpful!!! =) Solved it thx a lot!!!
Page Index Toggle Pages: 1