3D Picker (Selector) with ArrayList
in
Contributed Library Questions
•
10 months ago
Processing Newbie... but I've done my fair share of forum searching and looking at examples online and using Open Processing.
I'm trying to use an ArrayList, not an Array, to build a 3D point selector like
Johan's example. I tried to replace my ArrayList name in the Array spots but no luck. I'm using an ArrayList because I may be doing a lot of adding/deleting of points. Can someone take a quick look? I'm a few days into banging my head here. Thank you so much!
- // 3D POINT CREATOR
- // Simple 3D World in prep for making a 3D vector field
- // Currently, I can make points but cannot select them
- import peasy.*;
- import controlP5.*;
- import processing.opengl.*;
- PeasyCam cam;
- PeasyDragHandler PanDragHandler;
- PeasyDragHandler ZoomDragHandler;
- PeasyDragHandler RotateDragHandler;
- PeasyWheelHandler ZoomWheelHandler;
- ControlP5 controlP5;
- PMatrix3D currCameraMatrix;
- PGraphics3D g3;
- float PointX;
- float PointY;
- float PointZ;
- Slider x1, y1, z1;
- Bang b1;
- ArrayList myCharges;
- void setup() {
- size(700, 700, OPENGL);
- g3 = (PGraphics3D)g;
- cam = new PeasyCam(this, 300);
- // Sets center or rotation
- cam.lookAt(0, 0, 0);
- //camera.setRotations(-0.75, -0.5, 0.5);
- cam.setMinimumDistance(100);
- cam.setMaximumDistance(1000);
- perspectiveView();
- //topView();
- // Remap PeaseyCam Mouse like Rhino3D
- PanDragHandler = cam.getPanDragHandler();
- ZoomDragHandler = cam.getZoomDragHandler();
- RotateDragHandler = cam.getRotateDragHandler();
- ZoomWheelHandler = cam.getZoomWheelHandler();
- cam.setLeftDragHandler(null);
- cam.setCenterDragHandler(PanDragHandler);
- cam.setRightDragHandler(RotateDragHandler);
- cam.setWheelHandler(ZoomWheelHandler);
- // Interface Sliders
- controlP5 = new ControlP5(this);
- x1 = controlP5.addSlider("X Value", -100, 100, PointX, 20, 620, 200, 10); // name, min, max, default, x, y, w, h
- x1.setNumberOfTickMarks(81)
- .showTickMarks(false)
- .setSliderMode(Slider.FLEXIBLE);
- y1 = controlP5.addSlider("Y Value", -100, 100, PointY, 20, 640, 200, 10);
- y1.setNumberOfTickMarks(81)
- .showTickMarks(false)
- .setSliderMode(Slider.FLEXIBLE);
- z1 = controlP5.addSlider("Z Value", -100, 100, PointZ, 20, 660, 200, 10);
- z1.setNumberOfTickMarks(81)
- .showTickMarks(false)
- .setSliderMode(Slider.FLEXIBLE);
- b1 = controlP5.addBang("bang", 300, 620, 20, 20); // name, x, y, width, height
- controlP5.setAutoDraw(false);
- myCharges = new ArrayList(); // Create an empty ArrayList
- //myCharges.add(new Charge(0, 0, 0)); // Start by adding one element
- }
- void draw() {
- background(0);
- // Origin Display (RGB) like Rhino3D
- strokeWeight(1);
- stroke(255, 0, 0);
- line(0, 0, 10, 0);
- stroke(0, 255, 0);
- line(0, 0, 0, 10);
- stroke(0, 0, 255);
- line(0, 0, 0, 0, 0, 10);
- cam.beginHUD();
- gui();
- cam.endHUD();
- currPosition();
- // Simple Picking in 3D
- // http://www.processing.org/discourse/beta/num_1243897116.html
- theSelector();
- for (int i = myCharges.size()-1; i >= 0; i--) {
- Charge charge = (Charge) myCharges.get(i);
- charge.drawCharge();
- }
- }
- void gui() {
- currCameraMatrix = new PMatrix3D(g3.camera);
- camera();
- controlP5.draw();
- g3.camera = currCameraMatrix;
- // Crosshair
- pushMatrix();
- translate(width/2, height/2, 0);
- stroke(#FF0000, 150);
- line(-10, 0, 10, 0);
- line(0, -10, 0, 10);
- popMatrix();
- // Interface Background
- noStroke();
- fill(255, 255, 255, 25);
- rect(0, 600, 700, 700);
- }
- void topView() {
- cam.setRotations(0, PI/2, PI); // pitch, yaw, roll rotations
- }
- void bottomView() {
- cam.setRotations(0, -PI, PI); // pitch, yaw, roll rotations
- }
- void sideView() {
- cam.setRotations(PI/2, PI/2, PI); // pitch, yaw, roll rotations
- }
- void frontView() {
- cam.setRotations(PI/2, PI, PI); // pitch, yaw, roll rotations
- }
- void perspectiveView() {
- //perspective(PI/3.0, (float)width/height, 1, 100000);
- cam.setRotations(-PI/6, -PI/6, PI/4.5); // pitch, yaw, roll rotations
- }
- void currPosition() {
- strokeWeight(1);
- stroke(128, 128, 128);
- point(PointX, PointY, PointZ);
- }
- void keyPressed() {
- if (key == 't' || key == 'T') {
- topView();
- }
- if (key == 'b' || key == 'B') {
- topView();
- }
- if (key == 's' || key == 'S') {
- sideView();
- }
- if (key == 'f' || key == 'F') {
- frontView();
- }
- if (key == 'p' || key == 'P') {
- perspectiveView();
- }
- }
- /* Old selection method (might still be useful)
- void mousePressed() {
- for (int i = myCharges.size()-1; i >= 0; i--) {
- Charge charge = (Charge) myCharges.get(i);
- charge.checkMousePress();
- }
- }
- */
- void controlEvent(ControlEvent theEvent) {
- /* events triggered by controllers are automatically forwarded to
- the controlEvent method. by checking the name of a controller one can
- distinguish which of the controllers has been changed.
- */
- /* check if the event is from a controller otherwise you'll get an error
- when clicking other interface elements like Radiobutton that don't support
- the controller() methods
- */
- if (theEvent.isController()) {
- print("control event from : "+theEvent.controller().name());
- println(", value : "+theEvent.controller().value());
- if (theEvent.controller().name()=="bang") {
- myCharges.add(new Charge(PointX, PointY, PointZ));
- }
- if (theEvent.controller().name()=="X Value") {
- PointX = theEvent.controller().value();
- }
- if (theEvent.controller().name()=="Y Value") {
- PointY = theEvent.controller().value();
- }
- if (theEvent.controller().name()=="Z Value") {
- PointZ = theEvent.controller().value();
- }
- }
- }
- void theSelector() {
- // SELECTION BY MOUSE
- int tolerance = 10; // to make it easy to pick the shape
- for (int i = 0; i < myCharges.size(); i++) {
- // get objects screenX coords
- float checkX = myCharges(i).getScreenX();
- // if it matches the mouseX decently proceed to get Y
- if (checkX >= (mouseX-tolerance) && checkX <= (mouseX+tolerance)) {
- boolean yes = true;
- if (yes == true) {
- // get the Y one
- float checkY = myCharges(i).getScreenY();
- // if that one matches decently with the mouseY do something with that shape
- if (checkY >= (mouseY-tolerance) && checkY <= (mouseY+tolerance)) {
- // make new cursor indicating selection is possible
- cursor(MOVE);
- if (mousePressed == true && mouseButton == LEFT) {
- // here the action happens if it gets this far
- myCharges(i).setSelected(i);
- }
- }
- }
- }
- }
- }
- class Charge {
- float x;
- float y;
- float z;
- float r;
- boolean isSelected = false;
- // constructor
- Charge(float xpos, float ypos, float zpos) {
- x = xpos;
- y = ypos;
- z = zpos;
- r = 3; // strokeWeight
- }
- void drawCharge() {
- if (isSelected == true) {
- strokeWeight(r*2);
- stroke(255, 0, 0);
- }
- else {
- strokeWeight(r);
- stroke(255, 255, 255);
- }
- point(x, y, z);
- }
- void setSelected(int i) {
- this.isSelected = true;
- println("Got it " + i);
- }
- float getScreenX() {
- float f = screenX(x, y, z);
- return f;
- }
- float getScreenY() {
- float f = screenY(x, y, z);
- return f;
- }
- }
1