Search in sources :

Example 21 with Vector3

use of net.drewke.tdme.math.Vector3 in project tdme by andreasdr.

the class CollisionDetection method doCollide.

/**
	 * Check if mesh collides with oriented bounding box
	 * @param convex mesh
	 * @param oriented bounding box
	 * @param movement
	 * @param collision response
	 * @return if collided
	 */
public boolean doCollide(ConvexMesh mesh, OrientedBoundingBox obb, Vector3 movement, CollisionResponse collision) {
    collision.reset();
    // do broad test
    if (doBroadTest(mesh, obb) == false)
        return false;
    //
    Vector3[] obbVertices = obb.getVertices();
    Vector3[] obbAxes = obb.getAxes();
    Vector3[] meshVertices = mesh.getVertices();
    // compute axes and hit points
    resetTriangles();
    for (Triangle triangle : mesh.getTriangles()) {
        collision1.reset();
        // do broad test
        if (doBroadTest(triangle, obb) == false)
            continue;
        //
        Vector3[] triangleVertices = triangle.getVertices();
        triangle1Edge1.set(triangleVertices[1]).sub(triangleVertices[0]).normalize();
        triangle1Edge2.set(triangleVertices[2]).sub(triangleVertices[1]).normalize();
        triangle1Edge3.set(triangleVertices[0]).sub(triangleVertices[2]).normalize();
        Vector3.computeCrossProduct(triangle1Edge1, triangle1Edge2, triangle1Normal).normalize();
        // compute axes to test
        resetSATAxes();
        addSATAxis(satAxis.set(triangle1Normal));
        addSATAxis(satAxis.set(obbAxes[0]));
        addSATAxis(satAxis.set(obbAxes[1]));
        addSATAxis(satAxis.set(obbAxes[2]));
        addSATAxis(satAxis.set(triangle1Edge1));
        addSATAxis(satAxis.set(triangle1Edge2));
        addSATAxis(satAxis.set(triangle1Edge3));
        addSATAxis(Vector3.computeCrossProduct(obbAxes[0], triangle1Edge1, satAxis));
        addSATAxis(Vector3.computeCrossProduct(obbAxes[0], triangle1Edge2, satAxis));
        addSATAxis(Vector3.computeCrossProduct(obbAxes[0], triangle1Edge3, satAxis));
        addSATAxis(Vector3.computeCrossProduct(obbAxes[1], triangle1Edge1, satAxis));
        addSATAxis(Vector3.computeCrossProduct(obbAxes[1], triangle1Edge2, satAxis));
        addSATAxis(Vector3.computeCrossProduct(obbAxes[1], triangle1Edge3, satAxis));
        addSATAxis(Vector3.computeCrossProduct(obbAxes[2], triangle1Edge1, satAxis));
        addSATAxis(Vector3.computeCrossProduct(obbAxes[2], triangle1Edge2, satAxis));
        addSATAxis(Vector3.computeCrossProduct(obbAxes[2], triangle1Edge3, satAxis));
        // do separating axis test for axes
        for (int satAxisIdx = 0; satAxisIdx < satAxesCount; satAxisIdx++) {
            // compute penetrations
            if (separatingAxisTheorem.doSpanIntersect(meshVertices, obbVertices, satAxes[satAxisIdx], satPenetrations, satAxisIdx) == false) {
                resetSATAxes();
                resetTriangles();
                return false;
            }
        }
        // determine sat axis best fit
        determineSatAxisBestFit();
        // store triangles to test
        testTriangles[testTriangleCount++][0] = triangle;
    }
    // create single collision response
    if (haveSatAxisBestFit == true) {
        CollisionResponse.Entity entity = collision.addResponse(satAxisBestFitPenetration);
        entity.getNormal().set(satAxisBestFit);
        for (int i = 0; i < testTriangleCount; i++) {
            computeHitPoints(testTriangles[i][0], obb, entity);
        }
    }
    // reset
    resetTriangles();
    resetSATAxes();
    // we have a collision, return collision response
    return collision.hasEntitySelected();
}
Also used : Triangle(net.drewke.tdme.engine.primitives.Triangle) Vector3(net.drewke.tdme.math.Vector3)

Example 22 with Vector3

use of net.drewke.tdme.math.Vector3 in project tdme by andreasdr.

the class CollisionDetection method doCollide.

/**
	 * Checks if triangle collides with sphere
	 * @param triangle
	 * @param sphere
	 * @param movement
	 * @param collision
	 * @return collision
	 */
public boolean doCollide(Triangle triangle, Sphere sphere, Vector3 movement, CollisionResponse collision) {
    collision.reset();
    //
    Vector3 sphereCenter = sphere.getCenter();
    triangle.computeClosestPointOnBoundingVolume(sphereCenter, closestPoint);
    axis.set(sphereCenter).sub(closestPoint);
    // check if to use movement fallback
    if (checkMovementFallback(axis, movement, collision) == true) {
        // TODO: Hit points
        return true;
    }
    //
    float distance = axis.computeLength() - sphere.getRadius();
    if (distance < 0f) {
        CollisionResponse.Entity collisionEntity = collision.addResponse(distance);
        collisionEntity.getNormal().set(axis).normalize();
        collisionEntity.addHitPoint(hitPoint.set(axis).normalize().scale(-sphere.getRadius()).add(sphere.getCenter()));
        collisionEntity.addHitPoint(hitPoint.set(closestPoint));
        if (CHECK_COLLISIONRESPONSE)
            checkCollision(collision);
        return true;
    } else {
        return false;
    }
}
Also used : Vector3(net.drewke.tdme.math.Vector3)

Example 23 with Vector3

use of net.drewke.tdme.math.Vector3 in project tdme by andreasdr.

the class CollisionDetection method doCollide.

/**
	 * Checks if sphere is colliding with capsule
	 * @param sphere
	 * @param capsule
	 * @param movement
	 * @param collision
	 * @return collision
	 */
public boolean doCollide(Sphere s, Capsule c, Vector3 movement, CollisionResponse collision) {
    //
    collision.reset();
    // do broad test
    if (doBroadTest(s, c) == false)
        return false;
    // compute closest points from C1 to C2.A and C2.B
    Vector3 sphereCenter = s.getCenter();
    c.computeClosestPointOnBoundingVolume(sphereCenter, closestPoint);
    axis.set(closestPoint).sub(sphereCenter);
    // check if to use movement fallback
    if (checkMovementFallback(axis, movement, collision) == true) {
        CollisionResponse.Entity collisionEntity = collision.getEntityAt(0);
        collisionEntity.addHitPoint(hitPoint.set(movement).normalize().scale(s.getRadius()).add(s.getCenter()));
        collisionEntity.addHitPoint(hitPoint.set(movement).normalize().scale(-c.getRadius()).add(closestPoint));
        return true;
    }
    //
    float distance = axis.computeLength();
    float _distance = distance - (c.getRadius() + s.getRadius());
    if (_distance < 0f) {
        CollisionResponse.Entity collisionEntity = collision.addResponse(_distance);
        collisionEntity.getNormal().set(axis).normalize();
        collisionEntity.addHitPoint(hitPoint.set(axis).normalize().scale(s.getRadius()).add(s.getCenter()));
        collisionEntity.addHitPoint(hitPoint.set(axis).normalize().scale(-c.getRadius()).add(closestPoint));
        if (CHECK_COLLISIONRESPONSE)
            checkCollision(collision);
        return true;
    } else {
        return false;
    }
}
Also used : Vector3(net.drewke.tdme.math.Vector3)

Example 24 with Vector3

use of net.drewke.tdme.math.Vector3 in project tdme by andreasdr.

the class ConstraintsEntity method computeJacobian.

/**
	 * Compute jacobian
	 * @param constraint idx
	 * @param jacobian matrices
	 */
protected void computeJacobian(int constraintIdx, Matrix1x6[][] jacobianMatrices) {
    Vector3 body1Position = rb1.getPosition();
    Vector3 body2Position = rb2.getPosition();
    Vector3 n = collision.getNormal();
    Vector3 t1 = frictionVectors[0];
    Vector3 t2 = frictionVectors[1];
    Matrix1x6 jacobianMatrix;
    int currentConstraintIdx = constraintIdx;
    for (int hitPointIdx = 0; hitPointIdx < collision.getHitPointsCount(); hitPointIdx++) {
        Vector3 point = collision.getHitPointAt(hitPointIdx);
        //
        r1.set(point).sub(body1Position);
        r2.set(point).sub(body2Position);
        computeCrossProduct(r1, n, r1CrossN);
        computeCrossProduct(r2, n, r2CrossN);
        jacobianMatrix = jacobianMatrices[currentConstraintIdx][0];
        jacobianMatrix.setValue(0, tmpVector3.set(n).scale(-1f));
        jacobianMatrix.setValue(3, tmpVector3.set(r1CrossN).scale(-1f));
        jacobianMatrix = jacobianMatrices[currentConstraintIdx][1];
        jacobianMatrix.setValue(0, n);
        jacobianMatrix.setValue(3, r2CrossN);
        //
        currentConstraintIdx++;
        computeCrossProduct(r1, t1, r1CrossU1);
        computeCrossProduct(r2, t1, r2CrossU1);
        computeCrossProduct(r1, t2, r1CrossU2);
        computeCrossProduct(r2, t2, r2CrossU2);
        jacobianMatrix = jacobianMatrices[currentConstraintIdx][0];
        jacobianMatrix.setValue(0, tmpVector3.set(t1).scale(-1f));
        jacobianMatrix.setValue(3, tmpVector3.set(r1CrossU1).scale(-1f));
        jacobianMatrix = jacobianMatrices[currentConstraintIdx][1];
        jacobianMatrix.setValue(0, t1);
        jacobianMatrix.setValue(3, r2CrossU1);
        //
        currentConstraintIdx++;
        jacobianMatrix = jacobianMatrices[currentConstraintIdx][0];
        jacobianMatrix.setValue(0, tmpVector3.set(t2).scale(-1f));
        jacobianMatrix.setValue(3, tmpVector3.set(r1CrossU2).scale(-1f));
        jacobianMatrix = jacobianMatrices[currentConstraintIdx][1];
        jacobianMatrix.setValue(0, t2);
        jacobianMatrix.setValue(3, r2CrossU2);
        currentConstraintIdx++;
    }
}
Also used : Vector3(net.drewke.tdme.math.Vector3)

Example 25 with Vector3

use of net.drewke.tdme.math.Vector3 in project tdme by andreasdr.

the class ParticleSystemView method initParticleSystem.

/**
	 * Init particle system
	 */
protected void initParticleSystem(GLAutoDrawable drawable) {
    if (entity == null)
        return;
    //
    particleSystemFile = entity.getEntityFileName() != null ? new File(entity.getEntityFileName()) : null;
    // set up model in engine
    Tools.setupEntity(entity, engine, cameraRotationInputHandler.getLookFromRotations(), cameraRotationInputHandler.getScale());
    // Make model screenshot
    Tools.oseThumbnail(drawable, entity);
    // max axis dimension
    BoundingBox boundingBox = null;
    if (entity.getModel() == null) {
        boundingBox = new BoundingBox(new Vector3(-0.5f, 0f, -0.5f), new Vector3(0.5f, 3f, 0.5f));
    } else {
        boundingBox = entity.getModel().getBoundingBox();
    }
    cameraRotationInputHandler.setMaxAxisDimension(Tools.computeMaxAxisDimension(boundingBox));
    // 
    updateGUIElements();
}
Also used : BoundingBox(net.drewke.tdme.engine.primitives.BoundingBox) Vector3(net.drewke.tdme.math.Vector3) File(java.io.File)

Aggregations

Vector3 (net.drewke.tdme.math.Vector3)75 FacesEntity (net.drewke.tdme.engine.model.FacesEntity)20 Model (net.drewke.tdme.engine.model.Model)20 LevelEditorEntity (net.drewke.tdme.tools.shared.model.LevelEditorEntity)14 ArrayList (java.util.ArrayList)13 Rotation (net.drewke.tdme.engine.Rotation)12 Face (net.drewke.tdme.engine.model.Face)12 BoundingBox (net.drewke.tdme.engine.primitives.BoundingBox)11 Material (net.drewke.tdme.engine.model.Material)10 BoundingVolume (net.drewke.tdme.engine.primitives.BoundingVolume)10 Group (net.drewke.tdme.engine.model.Group)9 PrimitiveModel (net.drewke.tdme.engine.primitives.PrimitiveModel)9 Entity (net.drewke.tdme.engine.Entity)8 Object3D (net.drewke.tdme.engine.Object3D)8 LevelEditorObject (net.drewke.tdme.tools.shared.model.LevelEditorObject)8 File (java.io.File)7 IOException (java.io.IOException)7 Transformations (net.drewke.tdme.engine.Transformations)7 OrientedBoundingBox (net.drewke.tdme.engine.primitives.OrientedBoundingBox)7 Camera (net.drewke.tdme.engine.Camera)6