Need help.. (Arraylist, PVector) getting the distance between vertices from Arraylist and vertex of sphere
in
Contributed Library Questions
•
1 month ago
Hi there,
I want to get the distance between vertices from Arraylist and vertex of a sphere. So there will be multiple distance value to the sphere. I've tried some of the tips from this forum but i didn't get the correct distance value.
In my codes, I'm trying to get the distance between "b0.p"(which is an arraylist of vertices from Bone class) and "pos"(variable that holds the "s0.getPosition()", which is the vertex of the sphere). The reason I'm trying to get the distance is because I want to set a rule to change the color of the sphere should the distance between "b0.p" and "pos" is equal or less than the sphere radius ("s0.sR" is the variable for the sphere radius).
here are the obj file and mtl file accompanying it
here are the codes
**the bold lines are the one i'm stuck and got confused
**the long dash seperates the classes code
- import remixlab.proscene.*;
- import saito.objloader.*;
- import java.util.HashMap;
- Scene scene;
- ArrayList bones;
- ArrayList stems;
- Button2D button1, button2;
- int myColor;
- Bone b0;
- Stem s0;
- float d;
- float e;
- void setup() {
- size(800, 600, P3D);
- scene = new Scene(this);
- scene.setShortcut('f', Scene.KeyboardAction.DRAW_FRAME_SELECTION_HINT);
- button1 = new ClickButton(scene, new PVector(10,10), "+", 32, true, true);
- button2 = new ClickButton(scene, new PVector((10 + button1.myWidth + 5), 10), "-", 32, false, false);
- scene.setGridIsDrawn(true);
- scene.setCameraType(Camera.Type.ORTHOGRAPHIC);
- scene.setRadius(150);
- scene.showAll();
- b0 = new Bone(this, "femur.model");
- s0 = new Stem();
- myColor = 125;
- bones = new ArrayList();
- addBone();
- myColor = 130;
- stems = new ArrayList();
- addStem();
- }
- void draw() {
- background(0);
- lights();
- button1.display();
- button2.display();
- //get cood of objfile vertices
- b0.cood();
- // println(b0.p);
- //get the radius of sphere ftom Stem class
- s0.sphereRadius();
- // println(s0.sR);
- //get the vertex of sphere from Stem class
- PVector pos = s0.getPosition();
- // println(pos);
- for (int i = 0; i < bones.size(); i++) {
- Bone b0 = (Bone) bones.get(i);
- b0.draw(true);
- }
- for (int j = 0; j <stems.size(); j++) {
- Stem s0 = (Stem) stems.get(j);
- s0.draw(true);
- }
- }
- void addBone() {
- b0 = new Bone(this, "femur.model");
- bones.add(b0);
- }
- void removeBone() {
- if(bones.size()>0) {
- scene.removeFromMouseGrabberPool(((Bone)bones.get(0)).iFrame);
- bones.remove(0);
- }
- }
- void addStem() {
- s0 = new Stem();
- stems.add(s0);
- }
- void removeStem() {
- if(stems.size()>0) {
- scene.removeFromMouseGrabberPool(((Stem)stems.get(0)).iFrame);
- stems.remove(0);
- }
- }
- --------------------------------------------------------------
- public class Bone {
- InteractiveFrame iFrame;
- float w, h, d;
- int c;
- float px, py, pz;
- float rx, ry, rz;
- int col;
- Object femur;
- ArrayList <PVector> p;
- Bone(PApplet _applet, String model_name) {
- iFrame = new InteractiveFrame(scene);
- setSize();
- setColor();
- setPosition();
- col = color(255);
- femur = new Object(_applet, "Femurtest2.obj");
- // femur.model.scale(500);
- }
- public void draw() {
- draw(false);
- }
- public void draw(boolean drawAxis) {
- pushMatrix();
- pushStyle();
- iFrame.applyTransformation(); //optimum
- if(drawAxis) scene.drawAxis(max(w,h,d)*1.3f);
- noStroke();
- if (iFrame.grabsMouse()){
- fill(255, 0, 0);
- }
- else {
- fill(getColor());
- }
- femur.model.draw();
- popMatrix();
- }
- public void cood(){
- p = new ArrayList <PVector> ();
- p = femur.list;
- }
- // sets size randomly
- public void setSize() {
- w = random(10, 40);
- h = random(10, 40);
- d = random(10, 40);
- }
- public void setSize(float myW, float myH, float myD) {
- w=myW; h=myH; d=myD;
- }
- public int getColor() {
- return c;
- }
- // sets color randomly
- public void setColor() {
- c = color(random(0, 255), random(0, 255), random(0, 255));
- }
- public void setColor(int myC) {
- c = myC;
- }
- public PVector getPosition() {
- return iFrame.position();
- }
- // sets position randomly
- public void setPosition() {
- float low = -100;
- float high = 100;
- iFrame.setPosition(new PVector(random(low, high), random(low, high), random(low, high)));
- }
- public void setPosition(PVector pos) {
- iFrame.setPosition(pos);
- }
- public Quaternion getOrientation() {
- return iFrame.orientation();
- }
- public void setOrientation(PVector v) {
- PVector to = PVector.sub(v, iFrame.position());
- iFrame.setOrientation(new Quaternion(new PVector(0,1,0), to));
- }
- }
- ------------------------------------------------------------
- public class Button2D extends MouseGrabber {
- String myText;
- PFont myFont;
- int myWidth;
- int myHeight;
- PVector position;
- Button2D(Scene scn, PVector p, int fontSize) {
- this(scn, p, "", fontSize);
- }
- Button2D(Scene scn, PVector p, String t, int fontSize) {
- super(scn);
- position = p;
- myText = t;
- myFont = createFont("FFScala", fontSize);
- textFont(myFont);
- textMode(SCREEN);
- textAlign(CENTER);
- setText(t);
- }
- void setText(String text) {
- myText = text;
- myWidth = (int) textWidth(myText);
- myHeight = (int) (textAscent() + textDescent());
- }
- void display() {
- pushStyle();
- if(grabsMouse())
- fill(255);
- else
- fill(100);
- text(myText, position.x, position.y, myWidth, myHeight);
- popStyle();
- }
- void checkIfGrabsMouse(int x, int y, Camera camera) {
- // Rectangular activation area
- setGrabsMouse( (position.x <= x ) && ( x <= position.x + myWidth ) && (position.y <= y ) && ( y <= position.y + myHeight ) );
- }
- }
- ----------------------------------------------------------------
- public class ClickButton extends Button2D {
- boolean addBone;
- boolean addStem;
- public ClickButton(Scene scn, PVector p, String t, int fontSize, boolean addB, boolean addS) {
- super(scn, p, t, fontSize);
- addBone = addB;
- addStem = addS;
- }
- void mouseClicked(Scene.Button button, int numberOfClicks, Camera camera) {
- if(numberOfClicks == 1) {
- if(addBone)
- addBone();
- else
- removeBone();
- if(addStem)
- addStem();
- else
- removeStem();
- }
- }
- }
- ----------------------------------------
- public class Stem {
- InteractiveFrame iFrame;
- float w, h, d;
- int c;
- float px, py, pz;
- float rx, ry, rz;
- int col;
- int sphereRadius = 10;
- int sR;
- Stem() {
- iFrame = new InteractiveFrame(scene);
- setSize();
- setColor();
- setPosition();
- col = color(255);
- }
- public void draw() {
- draw(false);
- }
- public void draw(boolean drawAxis) {
- pushMatrix();
- pushStyle();
- iFrame.applyTransformation(); //optimum
- if(drawAxis) scene.drawAxis(max(w,h,d)*1.3f);
- noStroke();
- if (iFrame.grabsMouse()){
- fill(255, 0, 0);
- }
- else {
- fill(getColor());
- }
- sphere(sphereRadius);
- popMatrix();
- }
- public void hitColor(){
- col = color(0, 255,0);
- }
- public void sphereRadius(){
- sR = sphereRadius;
- }
- // sets size randomly
- public void setSize() {
- w = random(10, 40);
- h = random(10, 40);
- d = random(10, 40);
- }
- public void setSize(float myW, float myH, float myD) {
- w=myW; h=myH; d=myD;
- }
- public int getColor() {
- return c;
- }
- // sets color randomly
- public void setColor() {
- c = color(0, 0, 255);
- }
- public void setColor(int myC) {
- c = myC;
- }
- public PVector getPosition() {
- return iFrame.position();
- }
- // sets position randomly
- public void setPosition() {
- float low = -100;
- float high = 100;
- iFrame.setPosition(new PVector(random(low, high), random(low, high), random(low, high)));
- }
- public void setPosition(PVector pos) {
- iFrame.setPosition(pos);
- }
- public Quaternion getOrientation() {
- return iFrame.orientation();
- }
- public void setOrientation(PVector v) {
- PVector to = PVector.sub(v, iFrame.position());
- iFrame.setOrientation(new Quaternion(new PVector(0,1,0), to));
- }
- }
- ----------------------------------------------
- import saito.objloader.*;
- // A wrapper for a the OBJModel object
- class Object
- {
- OBJModel model;
- ArrayList <PVector> list;
- Object(PApplet _applet, String model_name)
- {
- model = new OBJModel(_applet, model_name, "relative", POLYGON);
- model.enableDebug();
- model.translateToCenter();
- // model.disableMaterial();
- model.scale(500);
- list = new ArrayList <PVector> ();
- for (int i=0; i<model.getVertexCount(); i++) {
- list.add(model.getVertex(i));
- }
- }
- }
1