Search in sources :

Example 1 with Ray

use of com.badlogic.gdx.math.collision.Ray in project libgdx-inGameConsole by StrongJoshua.

the class Box2DTest method createExplosion.

/**
	 * Creates an explosion that applies forces to the bodies relative to their
	 * position and the given x and y values.
	 * 
	 * @param maxForce
	 *            The maximum force to be applied to the bodies (diminishes as
	 *            distance from touch increases).
	 */
private void createExplosion(float x, float y, float maxForce) {
    float force;
    Vector2 touch = new Vector2(x, y);
    for (int i = 0; i < bodies.length; i++) {
        Body b = bodies[i];
        Vector2 v = b.getPosition();
        float dist = v.dst2(touch);
        if (dist == 0)
            force = maxForce;
        else
            force = MathUtils.clamp(maxForce / dist, 0, maxForce);
        float angle = v.cpy().sub(touch).angle();
        float xForce = force * MathUtils.cosDeg(angle);
        float yForce = force * MathUtils.sinDeg(angle);
        Vector3 touch3, v3, boundMin, boundMax, intersection;
        touch3 = new Vector3(touch.x, touch.y, 0);
        v3 = new Vector3(v.x, v.y, 0);
        boundMin = new Vector3(v.x - 1, v.y - 1, 0);
        boundMax = new Vector3(v.x + 1, v.y + 1, 0);
        intersection = Vector3.Zero;
        Intersector.intersectRayBounds(new Ray(touch3, v3), new BoundingBox(boundMin, boundMax), intersection);
        b.applyForce(new Vector2(xForce, yForce), new Vector2(intersection.x, intersection.y), true);
    }
}
Also used : Vector2(com.badlogic.gdx.math.Vector2) BoundingBox(com.badlogic.gdx.math.collision.BoundingBox) Vector3(com.badlogic.gdx.math.Vector3) Ray(com.badlogic.gdx.math.collision.Ray) Body(com.badlogic.gdx.physics.box2d.Body)

Example 2 with Ray

use of com.badlogic.gdx.math.collision.Ray in project libgdx by libgdx.

the class BaseBulletTest method shoot.

public BulletEntity shoot(final String what, final float x, final float y, final float impulse) {
    // Shoot a box
    Ray ray = camera.getPickRay(x, y);
    BulletEntity entity = world.add(what, ray.origin.x, ray.origin.y, ray.origin.z);
    entity.setColor(0.5f + 0.5f * (float) Math.random(), 0.5f + 0.5f * (float) Math.random(), 0.5f + 0.5f * (float) Math.random(), 1f);
    ((btRigidBody) entity.body).applyCentralImpulse(ray.direction.scl(impulse));
    return entity;
}
Also used : com.badlogic.gdx.physics.bullet.dynamics.btRigidBody(com.badlogic.gdx.physics.bullet.dynamics.btRigidBody) Ray(com.badlogic.gdx.math.collision.Ray)

Example 3 with Ray

use of com.badlogic.gdx.math.collision.Ray in project libgdx by libgdx.

the class TriangleRaycastTest method tap.

@Override
public boolean tap(float screenX, float screenY, int count, int button) {
    Ray ray = camera.getPickRay(screenX, screenY);
    rayFrom.set(ray.origin);
    rayTo.set(ray.direction).scl(100).add(rayFrom);
    // Clean the callback object for reuse.
    triangleRaycastCallback.setHitFraction(1);
    triangleRaycastCallback.clearReport();
    triangleRaycastCallback.setFrom(rayFrom);
    triangleRaycastCallback.setTo(rayTo);
    // Ray casting is performed directly on the collision shape.
    // The callback specifies the intersected MeshPart as well as triangle.
    triangleShape.performRaycast(triangleRaycastCallback, rayFrom, rayTo);
    int currentTriangleIndex = triangleRaycastCallback.triangleIndex;
    int currentPartId = triangleRaycastCallback.partId;
    if (currentTriangleIndex == -1 || currentPartId == -1) {
        // No intersection was found.
        return false;
    }
    // Get the position coordinates of the vertices belonging to intersected triangle.
    Mesh mesh = model.meshParts.get(currentPartId).mesh;
    FloatBuffer verticesBuffer = mesh.getVerticesBuffer();
    ShortBuffer indicesBuffer = mesh.getIndicesBuffer();
    int posOffset = mesh.getVertexAttributes().findByUsage(VertexAttributes.Usage.Position).offset / 4;
    int vertexSize = mesh.getVertexSize() / 4;
    int currentTriangleFirstVertexIndex = currentTriangleIndex * 3;
    // Store the three vertices belonging to the selected triangle.
    for (int i = 0; i < 3; i++) {
        int currentVertexIndex = indicesBuffer.get(currentTriangleFirstVertexIndex + i);
        int j = currentVertexIndex * vertexSize + posOffset;
        float x = verticesBuffer.get(j++);
        float y = verticesBuffer.get(j++);
        float z = verticesBuffer.get(j);
        selectedTriangleVertices[i].set(x, y, z);
    }
    return true;
}
Also used : Mesh(com.badlogic.gdx.graphics.Mesh) FloatBuffer(java.nio.FloatBuffer) Ray(com.badlogic.gdx.math.collision.Ray) ShortBuffer(java.nio.ShortBuffer)

Example 4 with Ray

use of com.badlogic.gdx.math.collision.Ray in project libgdx by libgdx.

the class RayCastTest method tap.

@Override
public boolean tap(float x, float y, int count, int button) {
    Ray ray = camera.getPickRay(x, y);
    rayFrom.set(ray.origin);
    // 50 meters max from the origin
    rayTo.set(ray.direction).scl(50f).add(rayFrom);
    // Because we reuse the ClosestRayResultCallback, we need reset it's values
    rayTestCB.setCollisionObject(null);
    rayTestCB.setClosestHitFraction(1f);
    rayTestCB.setRayFromWorld(rayFrom);
    rayTestCB.setRayToWorld(rayTo);
    world.collisionWorld.rayTest(rayFrom, rayTo, rayTestCB);
    if (rayTestCB.hasHit()) {
        final btCollisionObject obj = rayTestCB.getCollisionObject();
        if (!obj.isStaticOrKinematicObject()) {
            final btRigidBody body = (btRigidBody) (obj);
            body.activate();
            body.applyCentralImpulse(tmpV2.set(ray.direction).scl(20f));
        }
    }
    return true;
}
Also used : com.badlogic.gdx.physics.bullet.dynamics.btRigidBody(com.badlogic.gdx.physics.bullet.dynamics.btRigidBody) Ray(com.badlogic.gdx.math.collision.Ray) com.badlogic.gdx.physics.bullet.collision.btCollisionObject(com.badlogic.gdx.physics.bullet.collision.btCollisionObject)

Example 5 with Ray

use of com.badlogic.gdx.math.collision.Ray in project libgdx by libgdx.

the class RayPickRagdollTest method touchDragged.

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    boolean result = false;
    if (pickConstraint != null) {
        Ray ray = camera.getPickRay(screenX, screenY);
        tmpV1.set(ray.direction).scl(pickDistance).add(camera.position);
        pickConstraint.setPivotB(tmpV1);
        result = true;
    }
    return result ? result : super.touchDragged(screenX, screenY, pointer);
}
Also used : Ray(com.badlogic.gdx.math.collision.Ray)

Aggregations

Ray (com.badlogic.gdx.math.collision.Ray)7 com.badlogic.gdx.physics.bullet.dynamics.btRigidBody (com.badlogic.gdx.physics.bullet.dynamics.btRigidBody)3 Mesh (com.badlogic.gdx.graphics.Mesh)1 Vector2 (com.badlogic.gdx.math.Vector2)1 Vector3 (com.badlogic.gdx.math.Vector3)1 BoundingBox (com.badlogic.gdx.math.collision.BoundingBox)1 Body (com.badlogic.gdx.physics.box2d.Body)1 ClosestRayResultCallback (com.badlogic.gdx.physics.bullet.collision.ClosestRayResultCallback)1 com.badlogic.gdx.physics.bullet.collision.btCollisionObject (com.badlogic.gdx.physics.bullet.collision.btCollisionObject)1 com.badlogic.gdx.physics.bullet.dynamics.btConstraintSetting (com.badlogic.gdx.physics.bullet.dynamics.btConstraintSetting)1 com.badlogic.gdx.physics.bullet.dynamics.btDynamicsWorld (com.badlogic.gdx.physics.bullet.dynamics.btDynamicsWorld)1 com.badlogic.gdx.physics.bullet.dynamics.btPoint2PointConstraint (com.badlogic.gdx.physics.bullet.dynamics.btPoint2PointConstraint)1 Viewport (com.nilunder.bdx.gl.Viewport)1 FloatBuffer (java.nio.FloatBuffer)1 ShortBuffer (java.nio.ShortBuffer)1 Vector3f (javax.vecmath.Vector3f)1