use of com.jme3.math.Triangle in project jmonkeyengine by jMonkeyEngine.
the class BoundingCollisionTest method testSphereTriangleCollision.
@Test
public void testSphereTriangleCollision() {
BoundingSphere sphere = new BoundingSphere(1, Vector3f.ZERO);
Geometry geom = new Geometry("geom", new Quad(1, 1));
checkCollision(sphere, geom, 2);
// The box touches the edges of the triangles.
sphere.setCenter(new Vector3f(-1f + FastMath.ZERO_TOLERANCE, 0, 0));
checkCollision(sphere, geom, 2);
// Move it slightly farther..
sphere.setCenter(new Vector3f(-1f - FastMath.ZERO_TOLERANCE, 0, 0));
checkCollision(sphere, geom, 0);
// Parallel triangle / box side, touching
sphere.setCenter(new Vector3f(0, 0, -1f));
checkCollision(sphere, geom, 2);
// Not touching
sphere.setCenter(new Vector3f(0, 0, -1f - FastMath.ZERO_TOLERANCE));
checkCollision(sphere, geom, 0);
// Test collisions only against one of the triangles
sphere.setCenter(new Vector3f(-0.9f, 1.2f, 0f));
checkCollision(sphere, geom, 1);
sphere.setCenter(new Vector3f(1.2f, -0.9f, 0f));
checkCollision(sphere, geom, 1);
}
use of com.jme3.math.Triangle in project jmonkeyengine by jMonkeyEngine.
the class BoundingCollisionTest method testBoxTriangleCollision.
@Test
public void testBoxTriangleCollision() {
BoundingBox box = new BoundingBox(Vector3f.ZERO, 1, 1, 1);
Geometry geom = new Geometry("geom", new Quad(1, 1));
// Both triangles intersect
checkCollision(box, geom, 2);
// The box touches the edges of the triangles.
box.setCenter(new Vector3f(-1f, 0, 0));
checkCollision(box, geom, 2);
// Move it slightly farther..
box.setCenter(new Vector3f(-1f - FastMath.ZERO_TOLERANCE, 0, 0));
checkCollision(box, geom, 0);
// Parallel triangle / box side, touching
box.setCenter(new Vector3f(0, 0, -1f));
checkCollision(box, geom, 2);
// Not touching
box.setCenter(new Vector3f(0, 0, -1f - FastMath.ZERO_TOLERANCE));
checkCollision(box, geom, 0);
// Test collisions only against one of the triangles
box.setCenter(new Vector3f(-1f, 1.5f, 0f));
checkCollision(box, geom, 1);
box.setCenter(new Vector3f(1.5f, -1f, 0f));
checkCollision(box, geom, 1);
}
use of com.jme3.math.Triangle in project jmonkeyengine by jMonkeyEngine.
the class BoundingBox method computeFromTris.
/**
* <code>computeFromTris</code> creates a new Bounding Box from a given
* set of triangles. It is used in OBBTree calculations.
*
* @param tris
* @param start
* @param end
*/
public void computeFromTris(Triangle[] tris, int start, int end) {
if (end - start <= 0) {
return;
}
TempVars vars = TempVars.get();
Vector3f min = vars.vect1.set(new Vector3f(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY));
Vector3f max = vars.vect2.set(new Vector3f(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY));
Vector3f point;
for (int i = start; i < end; i++) {
point = tris[i].get(0);
checkMinMax(min, max, point);
point = tris[i].get(1);
checkMinMax(min, max, point);
point = tris[i].get(2);
checkMinMax(min, max, point);
}
center.set(min.addLocal(max));
center.multLocal(0.5f);
xExtent = max.x - center.x;
yExtent = max.y - center.y;
zExtent = max.z - center.z;
vars.release();
}
use of com.jme3.math.Triangle in project jmonkeyengine by jMonkeyEngine.
the class MikktspaceTangentGenerator method genTangSpace.
public static boolean genTangSpace(MikkTSpaceContext mikkTSpace, final float angularThreshold) {
// count nr_triangles
int[] piTriListIn;
int[] piGroupTrianglesBuffer;
TriInfo[] pTriInfos;
Group[] pGroups;
TSpace[] psTspace;
int iNrTrianglesIn = 0;
int iNrTSPaces, iTotTris, iDegenTriangles, iNrMaxGroups;
int iNrActiveGroups, index;
final int iNrFaces = mikkTSpace.getNumFaces();
//boolean bRes = false;
final float fThresCos = (float) FastMath.cos((angularThreshold * (float) FastMath.PI) / 180.0f);
// count triangles on supported faces
for (int f = 0; f < iNrFaces; f++) {
final int verts = mikkTSpace.getNumVerticesOfFace(f);
if (verts == 3) {
++iNrTrianglesIn;
} else if (verts == 4) {
iNrTrianglesIn += 2;
}
}
if (iNrTrianglesIn <= 0) {
return false;
}
piTriListIn = new int[3 * iNrTrianglesIn];
pTriInfos = new TriInfo[iNrTrianglesIn];
// make an initial triangle -. face index list
iNrTSPaces = generateInitialVerticesIndexList(pTriInfos, piTriListIn, mikkTSpace, iNrTrianglesIn);
// make a welded index list of identical positions and attributes (pos, norm, texc)
generateSharedVerticesIndexList(piTriListIn, mikkTSpace, iNrTrianglesIn);
// Mark all degenerate triangles
iTotTris = iNrTrianglesIn;
iDegenTriangles = 0;
for (int t = 0; t < iTotTris; t++) {
final int i0 = piTriListIn[t * 3 + 0];
final int i1 = piTriListIn[t * 3 + 1];
final int i2 = piTriListIn[t * 3 + 2];
final Vector3f p0 = getPosition(mikkTSpace, i0);
final Vector3f p1 = getPosition(mikkTSpace, i1);
final Vector3f p2 = getPosition(mikkTSpace, i2);
if (p0.equals(p1) || p0.equals(p2) || p1.equals(p2)) {
// degenerate
pTriInfos[t].flag |= MARK_DEGENERATE;
++iDegenTriangles;
}
}
iNrTrianglesIn = iTotTris - iDegenTriangles;
// mark all triangle pairs that belong to a quad with only one
// good triangle. These need special treatment in DegenEpilogue().
// Additionally, move all good triangles to the start of
// pTriInfos[] and piTriListIn[] without changing order and
// put the degenerate triangles last.
degenPrologue(pTriInfos, piTriListIn, iNrTrianglesIn, iTotTris);
// evaluate triangle level attributes and neighbor list
initTriInfo(pTriInfos, piTriListIn, mikkTSpace, iNrTrianglesIn);
// based on the 4 rules, identify groups based on connectivity
iNrMaxGroups = iNrTrianglesIn * 3;
pGroups = new Group[iNrMaxGroups];
piGroupTrianglesBuffer = new int[iNrTrianglesIn * 3];
iNrActiveGroups = build4RuleGroups(pTriInfos, pGroups, piGroupTrianglesBuffer, piTriListIn, iNrTrianglesIn);
psTspace = new TSpace[iNrTSPaces];
for (int t = 0; t < iNrTSPaces; t++) {
TSpace tSpace = new TSpace();
tSpace.os.set(1.0f, 0.0f, 0.0f);
tSpace.magS = 1.0f;
tSpace.ot.set(0.0f, 1.0f, 0.0f);
tSpace.magT = 1.0f;
psTspace[t] = tSpace;
}
// make tspaces, each group is split up into subgroups if necessary
// based on fAngularThreshold. Finally a tangent space is made for
// every resulting subgroup
generateTSpaces(psTspace, pTriInfos, pGroups, iNrActiveGroups, piTriListIn, fThresCos, mikkTSpace);
// degenerate quads with one good triangle will be fixed by copying a space from
// the good triangle to the coinciding vertex.
// all other degenerate triangles will just copy a space from any good triangle
// with the same welded index in piTriListIn[].
DegenEpilogue(psTspace, pTriInfos, piTriListIn, mikkTSpace, iNrTrianglesIn, iTotTris);
index = 0;
for (int f = 0; f < iNrFaces; f++) {
final int verts = mikkTSpace.getNumVerticesOfFace(f);
if (verts != 3 && verts != 4) {
continue;
}
// set data
for (int i = 0; i < verts; i++) {
final TSpace pTSpace = psTspace[index];
float[] tang = { pTSpace.os.x, pTSpace.os.y, pTSpace.os.z };
float[] bitang = { pTSpace.ot.x, pTSpace.ot.y, pTSpace.ot.z };
mikkTSpace.setTSpace(tang, bitang, pTSpace.magS, pTSpace.magT, pTSpace.orient, f, i);
mikkTSpace.setTSpaceBasic(tang, pTSpace.orient == true ? 1.0f : (-1.0f), f, i);
++index;
}
}
return true;
}
use of com.jme3.math.Triangle in project jmonkeyengine by jMonkeyEngine.
the class BresenhamTerrainPicker method getTerrainIntersection.
public Vector3f getTerrainIntersection(Ray worldPick, CollisionResults results) {
worldPickRay.set(worldPick);
List<TerrainPickData> pickData = new ArrayList<TerrainPickData>();
root.findPick(worldPick.clone(), pickData);
Collections.sort(pickData);
if (pickData.isEmpty())
return null;
workRay.set(worldPick);
for (TerrainPickData pd : pickData) {
TerrainPatch patch = pd.targetPatch;
tracer.getGridSpacing().set(patch.getWorldScale());
tracer.setGridOrigin(patch.getWorldTranslation());
workRay.getOrigin().set(worldPick.getDirection()).multLocal(pd.cr.getDistance() - .1f).addLocal(worldPick.getOrigin());
tracer.startWalk(workRay);
final Vector3f intersection = new Vector3f();
final Vector2f loc = tracer.getGridLocation();
if (tracer.isRayPerpendicularToGrid()) {
Triangle hit = new Triangle();
checkTriangles(loc.x, loc.y, workRay, intersection, patch, hit);
float distance = worldPickRay.origin.distance(intersection);
CollisionResult cr = new CollisionResult(intersection, distance);
cr.setGeometry(patch);
cr.setContactNormal(hit.getNormal());
results.addCollision(cr);
return intersection;
}
while (loc.x >= -1 && loc.x <= patch.getSize() && loc.y >= -1 && loc.y <= patch.getSize()) {
//System.out.print(loc.x+","+loc.y+" : ");
// check the triangles of main square for intersection.
Triangle hit = new Triangle();
if (checkTriangles(loc.x, loc.y, workRay, intersection, patch, hit)) {
// we found an intersection, so return that!
float distance = worldPickRay.origin.distance(intersection);
CollisionResult cr = new CollisionResult(intersection, distance);
cr.setGeometry(patch);
results.addCollision(cr);
cr.setContactNormal(hit.getNormal());
return intersection;
}
// because of how we get our height coords, we will
// sometimes be off by a grid spot, so we check the next
// grid space up.
int dx = 0, dz = 0;
Direction d = tracer.getLastStepDirection();
switch(d) {
case PositiveX:
case NegativeX:
dx = 0;
dz = 1;
break;
case PositiveZ:
case NegativeZ:
dx = 1;
dz = 0;
break;
}
if (checkTriangles(loc.x + dx, loc.y + dz, workRay, intersection, patch, hit)) {
// we found an intersection, so return that!
float distance = worldPickRay.origin.distance(intersection);
CollisionResult cr = new CollisionResult(intersection, distance);
results.addCollision(cr);
cr.setGeometry(patch);
cr.setContactNormal(hit.getNormal());
return intersection;
}
tracer.next();
}
}
return null;
}
Aggregations