Line erased when moved too fast
in
Contributed Library Questions
•
2 years ago
Hello,
In this sketch I want to move the pink line with the mouse along the green line. The mouse pointer must be on the blue part to allow the displacement.
The problem: if the mouse is moved too fast, the pink bar disappears.
What's wrong ?
Please note the lines are in a 3D scene.
In this sketch I want to move the pink line with the mouse along the green line. The mouse pointer must be on the blue part to allow the displacement.
The problem: if the mouse is moved too fast, the pink bar disappears.
What's wrong ?
Please note the lines are in a 3D scene.
- /*
- HANDLE + CONSTRAINT MOVE
- */
- import processing.opengl.* ;
- import codeanticode.glgraphics.*;
- import javax.media.opengl.*;
- import javax.media.opengl.glu.*;
- import java.nio.*;
- // VARIABLES
- PVector[] handleOne = new PVector[2];
- boolean locked = boolean(0) ; // test verrouillage souris sur point ?
- boolean bover = boolean(0) ;
- int movingPoint = 0 ;
- // Colors
- int bgColor = color(255, 255, 255); // background color
- int handleColor = color(255, 128, 220);
- int squareColor = color(255, 0, 0);
- int ellipseColor = color(0, 255, 255);
- void setup() {
- size (500, 500, OPENGL);
- background(bgColor);
- smooth();
- ellipseMode(CENTER);
- rectMode(CENTER);
- // Line definition
- handleOne[0] = new PVector(0, 250, 0);
- handleOne[1] = new PVector(100, 250, 0);
- }
- void draw() {
- stroke(handleColor);
- strokeWeight(2);
- translate(50, 50, 0);
- rotateZ(map(-90, 0, height, 0, PI));
- //ellipse(handleOne[0].x, handleOne[0].y, 0, 10);
- //Draws handle line
- beginShape(LINES);
- vertex(handleOne[0].x, handleOne[0].y, 0);
- vertex(handleOne[1].x, handleOne[1].y, 0);
- endShape();
- //Draw reference line
- stroke(0, 255, 0);
- beginShape(LINES);
- vertex(50, 0, 0);
- vertex(50, 500, 0);
- endShape();
- //Draw Ellipse
- stroke(ellipseColor);
- fill(ellipseColor);
- ellipse(handleOne[0].x, handleOne[0].y, 0, 10);
- //Tests whether mouse rolls over points and draws red squares
- strokeWeight(1);
- if (locked) {
- squareColor = color(255, 0, 0);
- } else {
- squareColor = color(128, 0, 0);
- }
- stroke(squareColor);
- fill(squareColor);
- if(dist(mouseX, mouseY, screenX(handleOne[0].x, handleOne[0].y, handleOne[0].z), screenY(handleOne[0].x, handleOne[0].y, handleOne[0].z)) <= 5) {
- movingPoint = 1;
- rect(handleOne[0].x, handleOne[0].y, 10, 10);
- } else {
- movingPoint = -1;
- }
- if (movingPoint != -1) {
- bover = true ;
- } else {
- bover = false;
- }
- //
- }
- */
- CURRENTLY UNUSED
- void keyPressed() {
- if (key == CODED) {
- if (keyCode == UP) {
- print(handleOne[0]);
- handleOne[0].y = handleOne[0].y-5;
- handleOne[1].y = handleOne[1].y-5;
- }
- if (keyCode == DOWN) {
- print(handleOne[0]);
- handleOne[0].y = handleOne[0].y+5;
- handleOne[1].y = handleOne[1].y+5;
- }
- }
- }
- */
- //********** Detects whether mouse button is pressed
- void mousePressed() {
- if(bover) {
- locked = true;
- } else {
- locked = false;
- }
- }
- //********** Moves selected point while button held
- void mouseDragged() {
- if(locked) {
- strokeWeight(1);
- stroke(255,0,0);
- fill(255,0,0);
- //handleOne[0].x = getMouse3D()[0] ;
- handleOne[0].y = getMouse3D()[1] ;
- //handleOne[0].z = getMouse3D()[2] ;
- //handleOne[1].x = getMouse3D()[0]+100 ;
- handleOne[1].y = getMouse3D()[1] ;
- //handleOne[1].z = getMouse3D()[2] ;
- // mouse (screen) to world conversion
- }
- }
- //********** Unlocks upon button release
- void mouseReleased() {
- locked = false;
- bover = false;
- }
- //************* SCREEN TO WORLD FUNCTION - found on old Processing forum ********************
- float[] getMouse3D()
- {
- ((PGraphicsOpenGL)g).beginGL();
- int viewport[] = new int[4];
- double[] proj=new double[16];
- double[] model=new double[16];
- ((PGraphicsOpenGL)g).gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
- ((PGraphicsOpenGL)g).gl.glGetDoublev(GL.GL_PROJECTION_MATRIX,proj,0);
- ((PGraphicsOpenGL)g).gl.glGetDoublev(GL.GL_MODELVIEW_MATRIX,model,0);
- FloatBuffer fb = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asFloatBuffer();
- ((PGraphicsOpenGL)g).gl.glReadPixels(mouseX, height-mouseY, 1, 1, GL.GL_DEPTH_COMPONENT, GL.GL_FLOAT, fb);
- fb.rewind();
- double[] mousePosArr=new double[4];
- ((PGraphicsOpenGL)g).glu.gluUnProject((double)mouseX,height-(double)mouseY,(double)fb.get(0),model,0,proj,0,viewport,0,mousePosArr,0);
- ((PGraphicsOpenGL)g).endGL();
- return new float[]{(float)mousePosArr[0],(float)mousePosArr[1],(float)mousePosArr[2]};
- }
1