Selecting & Moving 3D Primitives - urgent

Following on from my topic about selecting and moving 2D primitives, i am now looking for guidance as to how i can achieve this with 3D primitives. There are three items id like assistance with in this script:

(A) - How can i place ControlP5 Sliders that dont orbit with Peasycam (have had a go at this however it will not work) --SOLVED–

(B) - How can i select individual or grouped 3d primitives and relocate them on the canvas using the mouse function

( C ) - If i must sacrifice Peasycam to allow item “B” to work, then that is okay, but if there is a way i can toggle peasycam on and off with a ControlP5 Button that would be most preferable. --SOLVED–

Thanks for your help!

//  LIBRARIES
import controlP5.*;
import peasy.*;

//  CONSTANTS 
final int NONE = -1;

//  VARIABLES
int selected=NONE; // which column is selected?   

float [] posColorWheel;  // ColorWheel properties  
int widthColorWheel;
int heightColorWheel;

//OBJECTS in an array
GColumn[] myGColumns = new GColumn[7];
GWindow[] myGWindows = new GWindow[6];
GBeam[] myGBeams = new GBeam[6];
TBeam[] myTBeams = new TBeam[1];


//LIBRARY VARIABLES
ControlP5 cp5; 
PeasyCam cam;

//SETUP
void setup() {
  size(1400, 1000, P3D);
  strokeCap(ROUND);
  frameRate(10);
  
  //PEASYCAM SETUP
  cam = new PeasyCam(this, 700, 500, 000, 850);
  cam.setMinimumDistance(50);
  cam.setMaximumDistance(2000);

  //CONTROLP5 SETUP
  cp5 = new ControlP5( this );
  cp5.addColorWheel ("colour", 1200, 800, 100 ).setRGB(color(128, 0, 255));
  
  //
  posColorWheel = cp5.get(ColorWheel.class, "colour").getPosition(); 
  printArray(posColorWheel);
  widthColorWheel = cp5.get(ColorWheel.class, "colour").getWidth();
  heightColorWheel = cp5.get(ColorWheel.class, "colour").getHeight();
  //cp5.setAutoDraw(false); //When this is enabled, the slider disappears

  //INITIALISE COMPONENT VARIABLES
  //GCOLUMNS
  for (int i=0; i<myGColumns.length; i++) {
    myGColumns[i] = new GColumn( 157*i + 50, -30, i);
  }
  //GWINDOWS
  for (int i=0; i<myGWindows.length; i++) {
    myGWindows[i] = new GWindow( 157*i + 50, -30, i);
  }
  //GBEAMS
  for (int i=0; i<myGBeams.length; i++) {
    myGBeams[i] = new GBeam( 157*i + 50, -30, i);
  }
  //GBEAMS
  for (int i=0; i<myTBeams.length; i++) {
    myTBeams[i] = new TBeam( 157*i + 50, -30, i);
  }//for
  
}//func 

//DRAW FUNCTIONS
void draw() {
  background (234,234,234);
  
  //PeasyCam wont move when mouse is using controlP5
  if (cp5.getWindow(this).isMouseOver()) {
    cam.setActive(false);
  } else {
    cam.setActive(true);
  }
  //GCOLUMN DISPLAY
  for (GColumn myGC : myGColumns) {
    myGC.Display();
  }
  //GWINDOW DISPLAY
  for (GWindow myGW : myGWindows) {
    myGW.Display1();
  }
  //GBEAM DISPLAY
  for (GBeam myGB : myGBeams) {
    myGB.Display2();
  }
  //TBEAM DISPLAY
  for (TBeam myTB : myTBeams) {
    myTB.Display3();
  }//for
  
}//func 

//------------------------------------------------------------

void mousePressed() {
  // is mouse over color wheel? 
  if (overColorWheel())
    return; // leave here 

  // reset 
  selected=NONE;

  // search selected
  int i=0; 
  for (GColumn myGC : myGColumns) {
    if (myGC.over()) { 
      selected = i; 
      return;
    }
    i++;
  }//for
}//func 

boolean overColorWheel() {
  // is mouse over color wheel? 
  return mouseX>posColorWheel[0] && 
    mouseX<posColorWheel[0]+widthColorWheel && 
    mouseY>posColorWheel[1] && 
    mouseY<posColorWheel[1]+ heightColorWheel  ;
}//method 

void controlEvent(ControlEvent theEvent) {
  if (theEvent.isController()) { 
    // changing the value
    if (theEvent.getController().getName()=="colour") {
      //color c = cp5.getController("c").getRGB();
      color c = cp5.get(ColorWheel.class, "colour").getRGB();
      if (selected!=NONE) {
        myGColumns[selected].c = c;
        myGBeams[selected].c = c;
      }//if
    }
  }
}

// (NEW TAB "GBeam") ================================
//CLASS NAME
class GBeam {

  //VARIABLES
  color c;

  int x;
  int y;

  int id;  // its ID 
//Window#1
  int beamAX;
  int beamAY;

  int beamAW;
  int beamAH;
  int beamAD;
//Window#2
  int beamBX;
  int beamBY;

  int beamBW;
  int beamBH;
  int beamBD;
//Window#3  
  int beamCX;
  int beamCY;

  int beamCW;
  int beamCH;
  int beamCD;
//Window#4
  int beamDX;
  int beamDY;

  int beamDW;
  int beamDH;
  int beamDD;
//Window#5
  int beamEX;
  int beamEY;

  int beamEW;
  int beamEH;
  int beamED;
//Window#6
  int beamFX;
  int beamFY;

  int beamFW;
  int beamFH;
  int beamFD;
//
  
  //DEFINE CONSTRUCTORS
  GBeam( int x_, int y_, 
    int id_) {

    // color 
    c = color(172, 133, 175);

    // pos
    x = x_;
    y = y_;

    id=id_;
//Row#1
//AA
    beamAX = 106;   //translate x location
    beamAY = 996;  //translate y location

    beamAW = 120;   //box A width   
    beamAH = 7;   //box A height
    beamAD = 5;   //box A depth       
//BB
    beamBX = 106;   //translate x location
    beamBY = 972;  //translate y location

    beamBW = 120;   //box A width   
    beamBH = 7;   //box A height
    beamBD = 5;   //box A depth       
//CC
    beamCX = 106;   //translate x location
    beamCY = 965;  //translate y location

    beamCW = 120;   //box A width   
    beamCH = 3;   //box A height
    beamCD = 5;   //box A depth
//DD
    beamDX = 106;   //translate x location
    beamDY = 960;  //translate y location

    beamDW = 120;   //box A width   
    beamDH = 5;   //box A height
    beamDD = 5;   //box A depth       
//EE
    beamEX = 106;   //translate x location
    beamEY = 762;  //translate y location

    beamEW = 120;   //box A width   
    beamEH = 8;   //box A height
    beamED = 5;   //box A depth
//FF
    beamFX = 106;   //translate x location
    beamFY = 728;  //translate y location

    beamFW = 120;   //box A width   
    beamFH = 8;   //box A height
    beamFD = 5;   //box A depth
}
  
  //DEFINE FUNCTIONS
  void Display2() {
    pushMatrix(); 
    translate(x, y); 
    fill(c); 
    if (selected==id)
      stroke(240,0,0); 
    else 
    stroke(232,232,232,100);
    pushMatrix();
    translate(beamAX, beamAY);
    box(beamAW, beamAH, beamAD);
    popMatrix();
    pushMatrix();
    translate(beamBX, beamBY);
    box(beamBW, beamBH, beamBD);
    popMatrix();
    pushMatrix();
    translate(beamCX, beamCY);
    box(beamCW, beamCH, beamCD);
    popMatrix();
    pushMatrix();
    translate(beamDX, beamDY);
    box(beamDW, beamDH, beamDD);
    popMatrix();
    pushMatrix();
    translate(beamEX, beamEY);
    box(beamEW, beamEH, beamED);
    popMatrix();
    pushMatrix();
    translate(beamFX, beamFY);
    box(beamFW, beamFH, beamFD);
    popMatrix();
    popMatrix(); 
  }//method

  boolean over() {
    return mouseX>x-beamCX-beamCW/2 && 
      mouseX<x+beamCX+beamCW/2 && 
      mouseY>y+beamCY-beamCH/2 && 
      mouseY<y+beamCY+beamCH/2  ;
  }//method 
  //
}//class
//


// (NEW TAB "GCOLUMN") ================================
//CLASS NAME
class GColumn {

  //VARIABLES
  color c;

  int x;
  int y;

  int id;  // its ID 

  int boxAX;
  int boxAY;

  int boxAW;
  int boxAH;
  int boxAD;

  int boxBX;
  int boxBY;

  int boxBW;
  int boxBH;
  int boxBD;

  int spheAX;
  int spheAY;

  int spheR;

  int spheBX;
  int spheBY;

  //DEFINE CONSTRUCTORS
  GColumn( int x_, int y_, 
    int id_) {

    // color 
    c = color(172, 133, 175);

    // pos
    x = x_;
    y = y_;

    id=id_;

    boxAX = 25;   //translate x location
    boxAY = 981;  //translate y location

    boxAW = 56;   //box A width   
    boxAH = 36;   //box A height
    boxAD = 56;   //box A depth       

    boxBX = 26;
    boxBY = 770;

    boxBW = 36;
    boxBH = 370;
    boxBD = 36;

    spheAX = 26;
    spheAY = 963;

    spheR = 25;

    spheBX = 26;
    spheBY = 579;
  }

  //DEFINE FUNCTIONS
  void Display() {
    pushMatrix(); 
    translate(x, y); 
    fill(c); 
    if (selected==id)
      stroke(240,0,0); 
    else 
    stroke(232,232,232,100);
    pushMatrix();
    translate(boxAX, boxAY);
    box(boxAW, boxAH, boxAD);
    popMatrix();
    pushMatrix();
    translate(boxBX, boxBY);
    box(boxBW, boxBH, boxBD);
    popMatrix();
    pushMatrix();
    translate(spheAX, spheAY);
    sphere(spheR);
    popMatrix();
    pushMatrix();
    translate(spheBX, spheBY);
    sphere(spheR);
    popMatrix();
    popMatrix(); 
  }//method

  boolean over() {
    return mouseX>x-boxBX-boxBW/2 && 
      mouseX<x+boxBX+boxBW/2 && 
      mouseY>y+boxBY-boxBH/2 && 
      mouseY<y+boxBY+boxBH/2  ;
  }//method 
  //
}//class
//

// (NEW TAB "GWindow") ================================
//CLASS NAME
class GWindow {

  //VARIABLES
  color c;

  int x;
  int y;

  int id;  // its ID 
//Window#1
  int winAX;
  int winAY;

  int winAW;
  int winAH;
  int winAD;
//Window#2
  int winBX;
  int winBY;

  int winBW;
  int winBH;
  int winBD;
//Window#3  
  int winCX;
  int winCY;

  int winCW;
  int winCH;
  int winCD;
//Window#4
  int winDX;
  int winDY;

  int winDW;
  int winDH;
  int winDD;
//Window#5
  int winEX;
  int winEY;

  int winEW;
  int winEH;
  int winED;
//Window#6
  int winFX;
  int winFY;

  int winFW;
  int winFH;
  int winFD;
//Window#7
  int winGX;
  int winGY;

  int winGW;
  int winGH;
  int winGD;
//Window#8
  int winHX;
  int winHY;

  int winHW;
  int winHH;
  int winHD;
//Window#9
  int winIX;
  int winIY;

  int winIW;
  int winIH;
  int winID;
//Window#10
  int winJX;
  int winJY;

  int winJW;
  int winJH;
  int winJD;
//

  //DEFINE CONSTRUCTORS
  GWindow( int x_, int y_, 
    int id_) {

    // color 
    c = color(random(255), random(100), 0);

    // pos
    x = x_;
    y = y_;

    id=id_;
//Row#1
//AA
    winAX = 73;   //translate x location
    winAY = 985;  //translate y location

    winAW = 25;   //box A width   
    winAH = 15;   //box A height
    winAD = 5;   //box A depth       
//BB
    winBX = 106;   //translate x location
    winBY = 985;  //translate y location

    winBW = 25;   //box A width   
    winBH = 15;   //box A height
    winBD = 5;   //box A depth       
//CC
    winCX = 139;   //translate x location
    winCY = 985;  //translate y location

    winCW = 25;   //box A width   
    winCH = 15;   //box A height
    winCD = 5;   //box A depth
//Row#2
//DD
    winDX = 75;   //translate x location
    winDY = 862;  //translate y location

    winDW = 55;   //box A width   
    winDH = 194;   //box A height
    winDD = 5;   //box A depth       
//EE
    winEX = 136;   //translate x location
    winEY = 862;  //translate y location

    winEW = 55;   //box A width   
    winEH = 194;   //box A height
    winED = 5;   //box A depth
//Row#3
//FF
    winFX = 59;   //translate x location
    winFY = 745;  //translate y location

    winFW = 19;   //box A width   
    winFH = 19;   //box A height
    winFD = 5;   //box A depth 
//GG
    winGX = 106;   //translate x location
    winGY = 745;  //translate y location

    winGW = 65;   //box A width   
    winGH = 19;   //box A height
    winGD = 5;   //box A depth       
//HH
    winHX = 153;   //translate x location
    winHY = 745;  //translate y location

    winHW = 19;   //box A width   
    winHH = 19;   //box A height
    winHD = 5;   //box A depth
//Row#4
//II
    winIX = 106;   //translate x location
    winIY = 667;  //translate y location

    winIW = 107;   //box A width   
    winIH = 92;   //box A height
    winID = 5;   //box A depth       
//JJ
    winJX = 106;   //translate x location
    winJY = 598;  //translate y location

    winJW = 107;   //box A width   
    winJH = 23;   //box A height
    winJD = 5;   //box A depth
  }
  
  //DEFINE FUNCTIONS
  void Display1() {
    pushMatrix(); 
    translate(x, y); 
    fill(199,212,214,100); 
    //if (selected==id)
      //stroke(240,0,0); 
    //else 
    stroke(232,232,232,100);
    pushMatrix();
    translate(winAX, winAY);
    box(winAW, winAH, winAD);
    popMatrix();
    pushMatrix();
    translate(winBX, winBY);
    box(winBW, winBH, winBD);
    popMatrix();
    pushMatrix();
    translate(winCX, winCY);
    box(winCW, winCH, winCD);
    popMatrix();
    pushMatrix();
    translate(winDX, winDY);
    box(winDW, winDH, winDD);
    popMatrix();
    pushMatrix();
    translate(winEX, winEY);
    box(winEW, winEH, winED);
    popMatrix();
    pushMatrix();
    translate(winFX, winFY);
    box(winFW, winFH, winFD);
    popMatrix();
    pushMatrix();
    translate(winGX, winGY);
    box(winGW, winGH, winGD);
    popMatrix();
    pushMatrix();
    translate(winHX, winHY);
    box(winHW, winHH, winHD);
    popMatrix();
    pushMatrix();
    translate(winIX, winIY);
    box(winIW, winIH, winID);
    popMatrix();
    pushMatrix();
    translate(winJX, winJY);
    box(winJW, winJH, winJD);
    popMatrix();
    popMatrix(); 
  }//method

  boolean over() {
    return mouseX>x-winGX-winGW/2 && 
      mouseX<x+winGX+winGW/2 && 
      mouseY>y+winGY-winGH/2 && 
      mouseY<y+winGY+winGH/2  ;
  }//method 
  //
}//class
//

// (NEW TAB "TBeam") ================================
//CLASS NAME
class TBeam {

  //VARIABLES
  color c;

  int x;
  int y;

  int id;  // its ID 
//Window#1
  int tbeamAX;
  int tbeamAY;

  int tbeamAW;
  int tbeamAH;
  int tbeamAD;
//Window#2
  int tbeamBX;
  int tbeamBY;

  int tbeamBW;
  int tbeamBH;
  int tbeamBD;
//Window#3  
  int tbeamCX;
  int tbeamCY;

  int tbeamCW;
  int tbeamCH;
  int tbeamCD;
//Window#4
  int tbeamDX;
  int tbeamDY;

  int tbeamDW;
  int tbeamDH;
  int tbeamDD;
//
  
  //DEFINE CONSTRUCTORS
  TBeam( int x_, int y_, 
    int id_) {

    // color 
    c = color(172, 133, 175);

    // pos
    x = x_;
    y = y_;

    id=id_;
//Row#1
//AA
    tbeamAX = 500;   //translate x location
    tbeamAY = 565;  //translate y location

    tbeamAW = 1000;   //box A width   
    tbeamAH = 28;   //box A height
    tbeamAD = 56;   //box A depth       
//BB
    tbeamBX = 500;   //translate x location
    tbeamBY = 551;  //translate y location

    tbeamBW = 1000;   //box A width   
    tbeamBH = 5;   //box A height
    tbeamBD = 56;   //box A depth       
//CC
    tbeamCX = 500;   //translate x location
    tbeamCY = 546;  //translate y location

    tbeamCW = 1000;   //box A width   
    tbeamCH = 5;   //box A height
    tbeamCD = 56;   //box A depth
//DD
    tbeamDX = 500;   //translate x location
    tbeamDY = 541;  //translate y location

    tbeamDW = 1000;   //box A width   
    tbeamDH = 5;   //box A height
    tbeamDD = 56;   //box A depth
}
  
  //DEFINE FUNCTIONS
  void Display3() {
    pushMatrix(); 
    translate(x, y); 
    fill(c); 
    if (selected==id)
      stroke(240,0,0); 
    else 
    stroke(232,232,232,100);
    pushMatrix();
    translate(tbeamAX, tbeamAY);
    box(tbeamAW, tbeamAH, tbeamAD);
    popMatrix();
    pushMatrix();
    translate(tbeamBX, tbeamBY);
    box(tbeamBW, tbeamBH, tbeamBD);
    popMatrix();
    pushMatrix();
    translate(tbeamCX, tbeamCY);
    box(tbeamCW, tbeamCH, tbeamCD);
    popMatrix();
    pushMatrix();
    translate(tbeamDX, tbeamDY);
    box(tbeamDW, tbeamDH, tbeamDD);
    popMatrix();
    popMatrix(); 
  }//method

  boolean over() {
    return mouseX>x-tbeamBX-tbeamBW/2 && 
      mouseX<x+tbeamBX+tbeamBW/2 && 
      mouseY>y+tbeamBY-tbeamBH/2 && 
      mouseY<y+tbeamBY+tbeamBH/2  ;
  }//method 
  //
}//class
//
  
  

  
1 Like

Code edited to display full extent of what i desire to be selectable and movable using the mouse functions.
Is it possible to select a class and move it to the mouses new location?

(A)

Peasycam has beginHUD

See Peasycam documentation

http://mrfeinberg.com/peasycam/reference/index.html

(B) make a invisible PGraphics pg that you draw your stuff on but each with a unique color that you store. When mouse is pressed use pg.get() to retrieve the color of the position. Look up the index of the item that has this color. It’s a picking technique.

(C) you can temporarily deactivate Peasycam, I think with cam.setActive(false);

Use lights(); - looks much better

1 Like

I don’t have time.

Sorry.

just draw everything on a PGraphics with the same size like your window

That is all good, not sure what PGraphics is but ill look into it.
Thanks again

See reference for PGraphics

there are different ways of doing it.

You could also look at screenX

I want to apply something like this to my script so i am able to move objects around as well

class Entity{
  float x;
  float y;
  float destx;
  float desty;
  float speed;
  float movingAngle;
  boolean selected;
  boolean moving;
  public Entity(){
    x = random(width);
    y = random(height);
    speed = 5;
    selected = moving = false;
  }
  public Entity(float startx,float starty){
    x = startx;
    y = starty;
    speed = 5;
    selected = moving = false;
  }
  public void tick(){
    if(moving){
      x += cos(movingAngle)*speed;
      y += sin(movingAngle)*speed;
      if(sqrt(sq(destx-x)+sq(desty-y)) <= speed){
        moving = false;
      }
    }
    if(selected){
      stroke(255);
      fill(255,0,0);
    }
    else{
      stroke(0);
      fill(0,100,200);
    } 
    rect(x,y,50,50);
  }
  public void goTo(float dx,float dy){
    destx = dx;
    desty = dy;
    movingAngle = atan2(dy-y,dx-x);
    moving = true;
  }
}
ArrayList entities;
void setup(){
  size(800,800);
  smooth();
  background(128);
  entities = new ArrayList();
  for(int i = 0; i< 50;i++){
    entities.add(new Entity());
  }
}
void draw(){
  background(0);
  for(int i = 0; i< entities.size();i++){
    Entity e = (Entity)entities.get(i);
    e.tick();
  }
}
void mousePressed(){
  //If no entity was pressed then we want them to move wherever the user clicked
  boolean entityPressed = false;
  //loop in reverse order so that if two square are on top of each other the one on top is selected
  for(int i = entities.size()-1; i>= 0 ;i--){
    Entity e = (Entity)entities.get(i);
    if(mouseX > e.x && mouseX < e.x+50 &&mouseY > e.y && mouseY < e.y+50){
      entityPressed = true;
      if(e.selected){
        e.selected = false;
      }
      else{
        e.selected = true;
      }
      break;//break so that no ther square are selected, this can be removed
    }
  }
  //No entity was pressed - move all selected entities to where the user clicked and unselect them.
  if(!entityPressed){
    for(int i = 0; i< entities.size();i++){
      Entity e = (Entity)entities.get(i);
      if(e.selected){
        e.goTo(mouseX+random(-50,50),mouseY+random(-50,50));
        e.selected = false;
      }
    }
  }
}
void keyPressed(){
  if(key == 'n'){
      entities.add(new Entity(pmouseX,pmouseY));
}
}
void keyReleased(){
      if(key == 'd'){
            for(int i = 0; i<entities.size();i++){
                  Entity e = (Entity)entities.get(i);
                  if(e.selected){
                        entities.remove(i);
                        i--; //this is important
                  }
            }
      }
}

here is an peasy cam example with HUD and select items.

but no change of position. I leave this to you.


import peasy.*;

PeasyCam cam;
ArrayList<SphereClass> list = new ArrayList();  

boolean camActiveFlag =true; 

void setup() {
  size(1300, 900, P3D);

  cam=new PeasyCam(this, 400); 

  // init all spheres ------------------------------
  SphereClass newSphere;

  newSphere=new SphereClass(23, 23, -230, 
    5, 5, 5);
  list.add(newSphere);

  newSphere=new SphereClass(273, 23, -230, 
    10, 5, 20);
  list.add(newSphere);

  newSphere=new SphereClass(3, 153, 30, 
    3, 3, 3);
  list.add(newSphere);
} // func 

void draw() {
  background(0);
  lights(); 

  // loop over all spheres 
  for (SphereClass sphere : list) { 
    // show it
    sphere.display();
  }

  // HUD
  cam.beginHUD();
  fill(255);
  text("use peasycam, click on speroids", 23, 23);
  cam.endHUD();
} // func 

//----------------------------------------------------------

void keyPressed() {
  camActiveFlag = 
    ! camActiveFlag; 
  cam.setActive(camActiveFlag);
}

void mousePressed() {
  if (mouseButton==RIGHT) {
    return;
  }
  //
  // loop over all spheres 
  for (SphereClass sphere : list) {
    // select / unselect depending on mouse pos 
    sphere.selectWhenMouseOver();
  }//for
}

// ===========================================================

class SphereClass {

  PVector pos; // 3D vector
  PVector sizeSphere; // 3D vector
  PVector screenPos=new PVector(0, 0); // 2D vector  

  boolean selected=false; //yes/no

  // constr (pos and size)
  SphereClass(float x, float y, float z, 
    float w, float h, float d) {
    pos = new PVector(x, y, z); // 3D vector
    sizeSphere = new PVector(w, h, d); // 3D vector
  }// constr

  void display() {
    // draw sphere at pos (x, y, z) coordinate and store 2D screen pos

    pushMatrix();
    translate(pos.x, pos.y, pos.z);
    noStroke(); 
    // we choose the color depending on selected 
    if (selected)
      fill(255, 0, 0); // red 
    else
      fill(0, 0, 255); // blue 
    // draw the sphere
    scale(sizeSphere.x, sizeSphere.y, sizeSphere.z); 
    sphere(11);
    // we monitor the 2D screen pos throughout and store it
    screenPos.set(screenX(0, 0, 0), screenY(0, 0, 0));  // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    popMatrix();

    // show the 2D screen pos
    if (keyPressed)
      drawSignX(screenPos);
  }

  void drawSignX( PVector pos ) { 
    // Draw a "X" sign
    // 
    float sizeHalf=60; 
    float x=pos.x;
    float y=pos.y;
    float z=pos.z;  
    stroke(255);
    line(x-sizeHalf, y-sizeHalf, z, x+sizeHalf, y+sizeHalf, z); 
    line(x+sizeHalf, y-sizeHalf, z, x-sizeHalf, y+sizeHalf, z);
  }

  boolean selectWhenMouseOver() {
    // select / unselect
    if (mouseOver()) 
      selected=true;
    else 
    selected=false;

    return selected;
  }

  boolean mouseOver() {
    // makes use of the stored positions of screenX and screenY !!!!!!!!!!!!!!!!!!!!!!!!!!
    return 
      dist(mouseX, mouseY, screenPos.x, screenPos.y) < 50;
  }
  //
}//class
//
2 Likes

new version with select (with left mouse button) then drag (with right mouse button on selected) x/y

when holding a key during this it’s x/z

(switch peasycam off first using the space bar (cam on/off))


import peasy.*;

PeasyCam cam;
ArrayList<SphereClass> list = new ArrayList();  

int selected_i ; 
boolean hold=false; 

boolean camActiveFlag =true; 

void setup() {
  size(1300, 900, P3D);

  cam=new PeasyCam(this, 400); 

  // init all spheres ------------------------------
  SphereClass newSphere;

  newSphere=new SphereClass(23, 23, -230, 
    5, 5, 5);
  list.add(newSphere);

  newSphere=new SphereClass(273, 23, -230, 
    10, 5, 20);
  list.add(newSphere);

  newSphere=new SphereClass(3, 153, 30, 
    3, 3, 3);
  list.add(newSphere);
} // func 

void draw() {
  background(0);
  lights(); 

  // loop over all spheres 
  for (SphereClass sphere : list) { 
    // show it
    sphere.display();
  }

  // drag 
  if (hold) {
    list.get(selected_i).pos.x += mouseX-pmouseX;
    if (keyPressed)
      list.get(selected_i).pos.z += mouseY-pmouseY;
    else list.get(selected_i).pos.y += mouseY-pmouseY;
  }

  // HUD
  cam.beginHUD();
  fill(255);
  if (hold) 
    text("move mouse x/y; hold any key and move mouse y to change z", 23, 23);
  else text("use peasycam, click on speroids. Set Peasycam active/inactive with Space Bar. Peasycam is active: "
    +camActiveFlag, 23, 23);
  cam.endHUD();
} // func 

//----------------------------------------------------------

void keyPressed() {
  //if (keyCode==UP) {
  //  list.get(selected_i).pos.z += 33;
  //} else if (keyCode==DOWN) { 
  //  list.get(selected_i).pos.z -= 33;
  //}//
  switch(key) {
  case ' ':
    camActiveFlag = 
      ! camActiveFlag; 
    cam.setActive(camActiveFlag);
    break;
  }
}

void mousePressed() { 
  if (mouseButton==RIGHT) {
    hold=true; 
    return;
  }//if 

  // loop over all spheres
  int i=0; 
  for (SphereClass sphere : list) {
    // select / unselect depending on mouse pos 
    if ( sphere.selectWhenMouseOver() ) 
      selected_i = i; 
    i++;
  }//for
}

void mouseReleased() {
  hold=false;
}

// ===========================================================

class SphereClass {

  PVector pos; // 3D vector
  PVector sizeSphere; // 3D vector
  PVector screenPos=new PVector(0, 0); // 2D vector  

  boolean selected=false; //yes/no

  // constr (pos and size)
  SphereClass(float x, float y, float z, 
    float w, float h, float d) {
    pos = new PVector(x, y, z); // 3D vector
    sizeSphere = new PVector(w, h, d); // 3D vector
  }// constr

  void display() {
    // draw sphere at pos (x, y, z) coordinate and store 2D screen pos

    pushMatrix();
    translate(pos.x, pos.y, pos.z);
    noStroke(); 
    // we choose the color depending on selected 
    if (selected)
      fill(255, 0, 0); // red 
    else
      fill(0, 0, 255); // blue 
    // draw the sphere
    scale(sizeSphere.x, sizeSphere.y, sizeSphere.z); 
    sphere(11);
    // we monitor the 2D screen pos throughout and store it
    screenPos.set(screenX(0, 0, 0), screenY(0, 0, 0));  // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    popMatrix();

    // show the 2D screen pos
    if (keyPressed) {
      //drawSignX(screenPos);
    }
  }

  void drawSignX( PVector pos ) { 
    // Draw a "X" sign
    // 
    float sizeHalf=60; 
    float x=pos.x;
    float y=pos.y;
    float z=pos.z;  
    stroke(255);
    line(x-sizeHalf, y-sizeHalf, z, x+sizeHalf, y+sizeHalf, z); 
    line(x+sizeHalf, y-sizeHalf, z, x-sizeHalf, y+sizeHalf, z);
  }

  boolean selectWhenMouseOver() {
    // select / unselect
    if (mouseOver()) 
      selected=true;
    else 
    selected=false;

    return selected;
  }

  boolean mouseOver() {
    // makes use of the stored positions of screenX and screenY !!!!!!!!!!!!!!!!!!!!!!!!!!
    return 
      dist(mouseX, mouseY, screenPos.x, screenPos.y) < 50;
  }
  //
}//class
//
2 Likes

Note that (unfortunately) the movement is always and only along x,y and z axis, no matter how the peasycam shows the scene