Constrain 3D movement of click and drag object
in
Programming Questions
•
1 year ago
Dear experts,
I am working with processing a short time now, trying to learn en build some stuff.
There is one thing I don't get my head around.
Learned from this post:
I am trying to create an adjustable line, but, I want to constrain the movement to only the X axis.
I am not getting it to work!
Please help get me on the right track...
- PGraphics3D p3d;
- PMatrix3D proj = new PMatrix3D();
- PMatrix3D cam = new PMatrix3D();
- PMatrix3D modvw = new PMatrix3D();
- PMatrix3D modvwInv = new PMatrix3D();
- PMatrix3D screen2Model = new PMatrix3D();
- // rotation
- float rotX, rotY;
- float restX;
- //private colors = (0,0,255);
- color bgCol = color(255);
- color fgCol = color(0);
- color acCol = color(204, 0, 0);
- //bolletjes2D array
- PVector v1, v2, va ;
- PVector[] vecPos = new PVector[10];
- int[] bolletjes2D = new int[0];
- // arrays hold the vertices of 3d object
- float[] vertices3D = new float[0]; //maak lege lijst [0]
- int[] vertices2D = new int[0];
- // index of current mouseover / clicked vertex
- int vertexMouseOver = -1;
- int vertexKlicked= -1;
- // z value in model/world space of current vertex
- float zModelMouseOver;
- float zModelKlick;
- //--------------------SETUP PARAMETERS
- void setup() {
- size(800, 600, P3D);
- p3d = (PGraphics3D)g;
- frameRate(50);
- //positie 2D bolletjes
- v1 = new PVector(10, 0,10);
- v2 = new PVector(10, -50,10);
- vecPos[0] = v1;
- vecPos[1] = v2;
- }
- void draw() {
- rotateX(-.1);
- rotateY(-.1);
- background(bgCol);
- translations();
- //get 3d matrices
- proj = p3d.projection.get();
- cam = p3d.camera.get();
- modvw = p3d.modelview.get();
- modvwInv = p3d.modelviewInv.get();
- //draw control Spheres
- for (int i=0; i<2;i=i+1)
- {
- fill(acCol);
- noStroke();
- pushMatrix();
- va = vecPos[i];
- translate (va.x,va.y,va.z);
- sphere(1);
- popMatrix();
- }
- hitDetect();
- //draw line
- stroke(acCol);
- line(v1.x,v1.y,v1.z,v2.x,v2.y,v2.z);
- drawAxes3D();
- // calculate z value of picked vertex in model(world) space
- if (vertexMouseOver > -1) {
- zModelMouseOver = modelZ(vecPos[vertexMouseOver].x, vecPos[vertexMouseOver].y, vecPos[vertexMouseOver].z);
- }
- drawHitDetect();
- }
- void mousePressed(){
- if (mouseButton==LEFT && vertexMouseOver>-1)
- {
- vertexKlicked = vertexMouseOver;
- zModelKlick = zModelMouseOver;
- screen2Model = modvwInv;
- screen2Model.apply(cam);
- }
- }
- void mouseReleased(){
- vertexKlicked = -1;
- }
- void mouseDragged() {
- if (mouseButton==LEFT && vertexKlicked>-1) {
- float scrn[] = {mouseX, mouseY, 0};
- float model[] = new float[3];
- // apply transformation matrices to mouse coords
- screen2Model.mult(scrn, model);
- //Constrain points to yz plane
- if (vertexKlicked<5) {
- restX= 0;
- }
- else {
- restX = 50;
- }
- vecPos[vertexKlicked].x = model[0];
- vecPos[vertexKlicked].y = model[1];
- vecPos[vertexKlicked].z = model[2];
- }
- else {
- // mouse controlled rotation
- float x1 = mouseX-pmouseX;
- float y1 = mouseY-pmouseY;
- rotX += -y1 * 0.01;
- rotY += x1 * 0.01;
- }
- }
- void translations()
- {
- float model[] = new float[3];
- translate(width/2, height/1.7, 170);
- //mouse rotate
- rotateX(rotX);
- rotateY(rotY);
- }
- void drawAxes3D() {
- stroke(255,0,0);
- line(0,0,0, 100,0,0);
- stroke(0,255,0);
- line(0,0,0, 0,-100,0);
- stroke(0,0,255);
- line(0,0,0, 0,0,100);
- }
- void drawVertSphere() {
- //3d vertices3D as spheres
- noStroke();
- fill(100);
- sphereDetail(4);
- for(int i=0; i<vertices3D.length; i=i+3)
- {
- pushMatrix();
- translate(vertices3D[i], vertices3D[i+1], vertices3D[i+2]);
- sphere(3);
- popMatrix();
- }
- }
- void drawVert() {
- for(int i=0; i<2; i=i+1) {
- int x = int(screenX(vecPos[i].x, vecPos[i].y, vecPos[i].z));
- int y = int(screenY(vecPos[i].x, vecPos[i].y, vecPos[i].z));
- bolletjes2D = append(bolletjes2D, x);
- bolletjes2D = append(bolletjes2D, y);
- if (x > mouseX-10 && x < mouseX+10 && y > mouseY-10 && y < mouseY+10) {
- vertexMouseOver = i;
- }
- }
- noFill();
- stroke(100, 100, 100);
- beginShape();
- for(int i=0; i<vertices3D.length; i=i+3)
- {
- vertex(vertices3D[i], vertices3D[i+1], vertices3D[i+2]);
- }
- endShape();
- }
- void hitDetect() {
- // mouse hit detection using screnX, screenY
- bolletjes2D = new int[0];
- vertexMouseOver = -1;
- for(int i=0; i<2; i=i+1) {
- int x = int(screenX(vecPos[i].x, vecPos[i].y, vecPos[i].z));
- int y = int(screenY(vecPos[i].x, vecPos[i].y, vecPos[i].z));
- vertices2D = append(bolletjes2D, x);
- vertices2D = append(bolletjes2D, y);
- if (x > mouseX-3 && x < mouseX+3 && y > mouseY-3 && y < mouseY+3) {
- vertexMouseOver = i;
- }
- }
- }
- void drawHitDetect() {
- if (vertexMouseOver > -1)
- {
- noStroke();
- fill(125);
- translate (vecPos[vertexMouseOver].x, vecPos[vertexMouseOver].y, vecPos[vertexMouseOver].z);
- sphere(5);
- }
- }
1