collision using mesh?
in
Contributed Library Questions
•
11 months ago
so- i have this function i wrote in my code. the purpose of the code is to have an imported stl mesh in an enclosed environment, where these 'spiders' that i have will bounce around. i currently have it set so that there is a list of faces of the mesh, and then [cID] is the index number of the face. it's set by default to -9 so that it doesn't reference any face if none are within the collision distance [MIN_COL_DIST], which right now is set to 20 in a difference class.
okay. so what happens near the bottom of the code that is surrounded by //'s is that it finds the distance between the 'spider' and the nearest point on each of the edges on the faces, [cID]. if this distance is less than [mDist], then it refreshes mDist to be a new distance. as the distance between the 'spider' and the face's nearest edge point gets smaller and smaller, mDist continuously re-adjusts to the new, smaller distance, until it reaches the minimum collision distance to be considered a collision. which, as i said, right now, is 20. the velocity of the spider is currently 10. now i don't see why it wouldn't change directions and collide with the mesh. i have an inkling of what it might be, but i can't say for sure.
i think what the problem is though, is that i'm getting the closest point to the 'spider' from the EDGE of the face. and if the face has larger dimensions, like, say, larger than twice the minimum collision distance, (40), it won't register it as a collision. however i can't say for sure that this is the problem because it seems to just go through some of the faces and not through others and it's gone through near the edges before. so i'm just confused. what am i doing wrong!? is there a more accurate way to do this??
void checkWalls() { //checks for hits
List<Face> faces = new Vector<Face>();
int cID = -9; //-9 is a default, incase there is no hit
float mDist = 1000000;
try {
faces.addAll( tree.getFaces(loc, SEARCH_RADIUS));
}
catch (NullPointerException e) {
// no faces to add.
return;
}
if ( debug) {
for (Face f : faces) {// debug
fill(150);// debug
gfx.triangle(f.toTriangle());// debug
}// debug
}
////////////////////////////////////////////////////////////////////////////////////////////////
for (int i = 0; i < faces.size(); i++) {
if ( debug) {
fill(255, 0, 255);// debug
gfx.sphere(new Sphere(faces.get(i).toTriangle()
.getClosestPointTo(loc), 2), 5); // debug
}
float dista = loc.distanceTo(faces.get(i).toTriangle()
.getClosestPointTo(loc));
if (dista < mDist && dista <= MIN_COL_DIST) {
cID = i; //set cID to the face # that was hit
println("cID = " + cID);
mDist = dista;
}
}
if (cID == -9) //no hit, so just return
return;
if ( debug) {
fill(0, 255, 0); //debug
gfx.triangle(faces.get(cID).toTriangle());//debug
}
vel = vel.reflect(faces.get(cID).normal).scale(-1); // use that CID to reflect the movement of the pt
//off the normal of that face
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
spiderWeb.addPoint(loc.copy());
}
1