Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

How to get id of closest vertex?

$
0
0

I am trying to get a ball moving on a mesh and have it react according to the slope of the mesh. I have calculated all the slopes of the vertexes and saved the values in an arraylist called slope. I can find the closest vertex to my moving ball, but I am not able to figure out how to access the id number of the vertex to get the slope out of my arraylist. Any help will be appreciated.

Thanks

the code i used

import processing.opengl.*;
import toxi.geom.*;
import java.util.Iterator;
import java.util.*;
import peasy.*;
import wblut.math.*;
import wblut.processing.*;
import wblut.core.*;
import wblut.hemesh.*;
import wblut.geom.*;

ArrayList Slope = new ArrayList();
HE_Mesh mesh;
WB_KDTree vertexTree;
WB_Render render;
WB_Coord mnorm;
PeasyCam cam;

int speedx =1;
int speedy =1;


void setup() {

  size(700, 700, P3D);

  frameRate(30);
  smooth();

  cam = new PeasyCam(this, 1200);

  mesh = new HEC_FromOBJFile(sketchPath("meshtoimport2.obj")).create();
  vertexTree = mesh.getVertexTree();

  int novert = mesh.getNumberOfVertices();

  for (int i=0; i< novert; i++) {
    mnorm = mesh.getVertexNormal(i);

    float xnPos = (Float)  mnorm.xf();
    float ynPos = (Float)  mnorm.yf();
    float znPos = (Float)  mnorm.zf();

    Vec3D mnormv = new Vec3D(xnPos, ynPos, znPos);
    Vec3D mvert = new Vec3D(0, 0, 1);

    float slope = mnormv.angleBetween(mvert);
    slope = degrees(slope);

    Slope.add(slope);
  }

  render = new WB_Render( this );
}

void draw() {
  background(0);
  lights();
  noStroke();
  render.drawFaces(mesh);

  stroke(255, 0, 255);
  strokeWeight(.5);
  render.drawFaceNormals(2, mesh);

  speedx = speedx+1;
  speedy = speedy+1;
  WB_Point tpos = new WB_Point(speedx, speedy, 0);
  WB_Coord ptonmesh = mesh.getClosestPoint(tpos, vertexTree);
  HE_Vertex meshPt = mesh.getClosestVertex(tpos, vertexTree);

  float xPos = (Float) ptonmesh.xf();
  float yPos = (Float)  ptonmesh.yf();
  float zPos = (Float)  ptonmesh.zf();

  fill(60, 60, 200, 200);
  translate(xPos, yPos, zPos);
  sphere(15);
}

Viewing all articles
Browse latest Browse all 2896

Trending Articles