Need help and advice on how to set constraints to vertices of 3D object
in
Contributed Library Questions
•
1 month ago
Hi there,
I'm trying to set constraints based on the vertices obtained from the 3D object (in this case from OBJ file) so that a second 3D object(sphere) can't go thru it. I've tried browsing openprocessing to find a way to set constraints to given set of vertices but I can't find one though.
The OBJ file object is not a simple shape (pyramid,sphere,cube, etc). It is bone shape or any other similar non-uniform shape.
The lines that is bold is the code for getting the vertices from OBJ file
Could someone help me to solve this and advice me on which library best suited for it?
Thank you all
Below is the codes. Y
ou can just use any obj file and rename it to test.obj. I'm using Processing 1.5.1
- import remixlab.proscene.*;
- import saito.objloader.*;
- import java.util.HashMap;
- Scene scene;
- ArrayList bones;
- ArrayList stems;
- int myColor;
- Bone b0;
- Stem s0;
- void setup() {
- size(800, 600, P3D);
- scene = new Scene(this);
- scene.setShortcut('f', Scene.KeyboardAction.DRAW_FRAME_SELECTION_HINT);
- 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);
- 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;
- Bone(PApplet _applet, String model_name) {
- iFrame = new InteractiveFrame(scene);
- setSize();
- setColor();
- setPosition();
- col = color(255);
- femur = new Object(_applet, "test.obj");
- // femur.model.scale(0.1);
- }
- 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());
- }
- stroke(col);
- femur.model.draw();
- popMatrix();
- }
- // 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 Stem {
- InteractiveFrame iFrame;
- float w, h, d;
- int c;
- float px, py, pz;
- float rx, ry, rz;
- int col;
- 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());
- }
- stroke(col);
- sphere(10);
- popMatrix();
- }
- // 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));
- }
- }
- ------------------------------------------------------
- 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", POINTS);
- model.disableDebug();
- model.translateToCenter();
- model.disableMaterial();
- model.scale(5);
- list = new ArrayList <PVector> ();
- for (int i=0; i<model.getVertexCount(); i++) {
- list.add(model.getVertex(i));
- }
- }
- }
1