Search in sources :

Example 66 with Vector3

use of org.rajawali3d.math.vector.Vector3 in project Rajawali by Rajawali.

the class Intersector method intersectRaySphere.

/**
	 * Intersects a ray defined by the start and end point and a sphere, returning the intersection point in intersection.
	 * @param rayStart Startpoint of the ray
	 * @param rayEnd Endpoint of the ray
	 * @param sphereCenter The center of the sphere
	 * @param sphereRadius The radius of the sphere
	 * @param hitPoint The intersection point (optional)
	 * @return True if there is an intersection, false otherwise.
	 */
public static boolean intersectRaySphere(Vector3 rayStart, Vector3 rayEnd, Vector3 sphereCenter, double sphereRadius, Vector3 hitPoint) {
    rayStart = new Vector3(rayStart);
    rayEnd = new Vector3(rayEnd);
    Vector3 dir = Vector3.subtractAndCreate(rayEnd, rayStart);
    dir.normalize();
    sphereCenter = new Vector3(sphereCenter);
    double radius2 = sphereRadius * sphereRadius;
    /*
		 * Refer to http://paulbourke.net/geometry/circlesphere/ for mathematics
		 * behind ray-sphere intersection.
		 */
    double a = Vector3.dot(dir, dir);
    double b = 2.0f * Vector3.dot(dir, Vector3.subtractAndCreate(rayStart, sphereCenter));
    double c = Vector3.dot(sphereCenter, sphereCenter) + Vector3.dot(rayStart, rayStart) - 2.0f * Vector3.dot(sphereCenter, rayStart) - radius2;
    // Test for intersection.
    double result = b * b - 4.0f * a * c;
    if (result < 0)
        return false;
    // Starting with this section, the code was referenced from libGDX.
    double distSqrt = Math.sqrt(result);
    double q;
    if (b < 0)
        q = (-b - distSqrt) / 2.0f;
    else
        q = (-b + distSqrt) / 2.0f;
    double t0 = q / 1;
    double t1 = c / q;
    // If t0 is larger than t1, swap them around.
    if (t0 > t1) {
        double temp = t0;
        t0 = t1;
        t1 = temp;
    }
    // and consequently ray misses the sphere.
    if (t1 < 0)
        return false;
    // If t0 is less than zero, intersection point is at t1.
    if (t0 < 0) {
        hitPoint = rayStart.add(Vector3.scaleAndCreate(dir, t1));
        return true;
    } else {
        hitPoint = rayStart.add(Vector3.scaleAndCreate(dir, t0));
        return true;
    }
}
Also used : Vector3(org.rajawali3d.math.vector.Vector3)

Example 67 with Vector3

use of org.rajawali3d.math.vector.Vector3 in project Rajawali by Rajawali.

the class RayPicker method getObjectAt.

public void getObjectAt(float x, float y) {
    Vector3 pointNear = mRenderer.unProject(x, y, 0);
    Vector3 pointFar = mRenderer.unProject(x, y, 1);
    RayPickingVisitor visitor = new RayPickingVisitor(pointNear, pointFar);
    //mRenderer.accept(visitor);
    // TODO: ray-triangle intersection test
    mObjectPickedListener.onObjectPicked(visitor.getPickedObject());
}
Also used : RayPickingVisitor(org.rajawali3d.visitors.RayPickingVisitor) Vector3(org.rajawali3d.math.vector.Vector3)

Example 68 with Vector3

use of org.rajawali3d.math.vector.Vector3 in project Rajawali by Rajawali.

the class Vector3Test method testMultiplyFromMatrix4.

@Test
public void testMultiplyFromMatrix4() throws Exception {
    final Vector3 v = new Vector3(1d, 2d, 3d);
    final double[] matrix = new double[] { 1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d, 10d, 11d, 12d, 13d, 14d, 15d, 16d };
    final Matrix4 matrix4 = new Matrix4(matrix);
    final Vector3 out = v.multiply(matrix4);
    assertNotNull(out);
    assertTrue(out == v);
    assertEquals(51d, v.x, 0);
    assertEquals(58d, v.y, 0);
    assertEquals(65d, v.z, 0);
}
Also used : Vector3(org.rajawali3d.math.vector.Vector3) Matrix4(org.rajawali3d.math.Matrix4) SmallTest(android.test.suitebuilder.annotation.SmallTest) Test(org.junit.Test)

Example 69 with Vector3

use of org.rajawali3d.math.vector.Vector3 in project Rajawali by Rajawali.

the class Vector3Test method testDotFromVector3.

@Test
public void testDotFromVector3() throws Exception {
    final Vector3 v = new Vector3(1d, 2d, 3d);
    final Vector3 v1 = new Vector3(4d, 5d, 6d);
    final double dot = v.dot(v1);
    assertEquals(32d, dot, 0);
}
Also used : Vector3(org.rajawali3d.math.vector.Vector3) SmallTest(android.test.suitebuilder.annotation.SmallTest) Test(org.junit.Test)

Example 70 with Vector3

use of org.rajawali3d.math.vector.Vector3 in project Rajawali by Rajawali.

the class Vector3Test method testLerp.

@Test
public void testLerp() throws Exception {
    final Vector3 v = new Vector3(1d, 0d, 0d);
    final Vector3 vp = new Vector3(0d, 1d, 0d);
    final Vector3 v1 = v.lerp(vp, 0);
    assertNotNull(v1);
    assertTrue(v1 == v);
    assertEquals(1d, v.x, 0);
    assertEquals(0d, v.y, 0);
    assertEquals(0d, v.z, 0);
    v.setAll(1d, 0d, 0d);
    final Vector3 v2 = v.lerp(vp, 1d);
    assertNotNull(v2);
    assertTrue(v2 == v);
    assertEquals(0d, v.x, 0);
    assertEquals(1d, v.y, 0);
    assertEquals(0d, v.z, 0);
    v.setAll(1d, 0d, 0d);
    final Vector3 v3 = v.lerp(vp, 0.5);
    assertNotNull(v3);
    assertTrue(v3 == v);
    assertEquals(0.5d, v.x, 0);
    assertEquals(0.5d, v.y, 0);
    assertEquals(0d, v.z, 0);
}
Also used : Vector3(org.rajawali3d.math.vector.Vector3) SmallTest(android.test.suitebuilder.annotation.SmallTest) Test(org.junit.Test)

Aggregations

Vector3 (org.rajawali3d.math.vector.Vector3)166 SmallTest (android.test.suitebuilder.annotation.SmallTest)106 Test (org.junit.Test)106 Material (org.rajawali3d.materials.Material)9 SkeletonJoint (org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint)6 Matrix4 (org.rajawali3d.math.Matrix4)6 Object3D (org.rajawali3d.Object3D)5 BoundingBox (org.rajawali3d.bounds.BoundingBox)5 DiffuseMethod (org.rajawali3d.materials.methods.DiffuseMethod)4 Quaternion (org.rajawali3d.math.Quaternion)4 ArrayList (java.util.ArrayList)3 DirectionalLight (org.rajawali3d.lights.DirectionalLight)3 ParsingException (org.rajawali3d.loader.ParsingException)3 FileNotFoundException (java.io.FileNotFoundException)2 FloatBuffer (java.nio.FloatBuffer)2 Stack (java.util.Stack)2 BoneVertex (org.rajawali3d.animation.mesh.SkeletalAnimationChildObject3D.BoneVertex)2 BoneWeight (org.rajawali3d.animation.mesh.SkeletalAnimationChildObject3D.BoneWeight)2 BoundingSphere (org.rajawali3d.bounds.BoundingSphere)2 IBoundingVolume (org.rajawali3d.bounds.IBoundingVolume)2