Search in sources :

Example 1 with AllHitsRayResultCallback

use of com.badlogic.gdx.physics.bullet.collision.AllHitsRayResultCallback in project Terasology by MovingBlocks.

the class BulletPhysics method rayTrace.

@Override
public HitResult rayTrace(Vector3f from, Vector3f direction, float distance, Set<EntityRef> excludedEntities, CollisionGroup... collisionGroups) {
    if (excludedEntities == null || excludedEntities.size() == 0) {
        return rayTrace(from, direction, distance, collisionGroups);
    }
    Vector3f to = new Vector3f(direction);
    to.normalize();
    to.mul(distance);
    to.add(from);
    short filter = combineGroups(collisionGroups);
    // lookup all the collision item ids for these entities
    Set<Integer> excludedCollisionIds = Sets.newHashSet();
    for (EntityRef excludedEntity : excludedEntities) {
        if (entityRigidBodies.containsKey(excludedEntity)) {
            excludedCollisionIds.add(entityRigidBodies.get(excludedEntity).rb.getBroadphaseHandle().getUid());
        }
        if (entityColliders.containsKey(excludedEntity)) {
            excludedCollisionIds.add(entityColliders.get(excludedEntity).collider.getBroadphaseHandle().getUid());
        }
        if (entityTriggers.containsKey(excludedEntity)) {
            excludedCollisionIds.add(entityTriggers.get(excludedEntity).getBroadphaseHandle().getUid());
        }
    }
    AllHitsRayResultCallback callback = new AllHitsRayResultCallback(from, to);
    callback.setCollisionFilterGroup(StandardCollisionGroup.ALL.getFlag());
    callback.setCollisionFilterMask(filter);
    discreteDynamicsWorld.rayTest(from, to, callback);
    if (callback.hasHit()) {
        btCollisionObjectConstArray collisionObjects = callback.getCollisionObjects();
        for (int x = 0; x < collisionObjects.size(); x++) {
            btCollisionObject collisionObject = collisionObjects.atConst(x);
            if (!excludedCollisionIds.contains(collisionObject.getBroadphaseHandle().getUid())) {
                Vector3f hitPointWorld = callback.getHitPointWorld().at(x);
                Vector3f hitNormalWorld = callback.getHitNormalWorld().at(x);
                callback.dispose();
                if (collisionObject.userData instanceof EntityRef) {
                    // we hit an other entity
                    return new HitResult((EntityRef) collisionObject.userData, hitPointWorld, hitNormalWorld);
                } else if ((collisionObject.getCollisionFlags() & btCollisionObject.CollisionFlags.CF_VOXEL_OBJECT) > 0) {
                    btVector3i pos = new btVector3i();
                    collisionObject.getVoxelPosition(pos);
                    Vector3i voxelPosition = new Vector3i(pos.getX(), pos.getY(), pos.getZ());
                    final EntityRef entityAt = blockEntityRegistry.getEntityAt(voxelPosition);
                    return new HitResult(entityAt, hitPointWorld, hitNormalWorld, voxelPosition);
                } else {
                    // we hit something we don't understand, assume its nothing and log a warning
                    logger.warn("Unidentified object was hit in the physics engine: {}", collisionObject.userData);
                }
            }
        }
    } else {
        callback.dispose();
    }
    return new HitResult();
}
Also used : HitResult(org.terasology.engine.physics.HitResult) com.badlogic.gdx.physics.bullet.collision.btCollisionObjectConstArray(com.badlogic.gdx.physics.bullet.collision.btCollisionObjectConstArray) Vector3f(org.joml.Vector3f) Vector3i(org.joml.Vector3i) com.badlogic.gdx.physics.bullet.collision.btVector3i(com.badlogic.gdx.physics.bullet.collision.btVector3i) com.badlogic.gdx.physics.bullet.collision.btVector3i(com.badlogic.gdx.physics.bullet.collision.btVector3i) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) AllHitsRayResultCallback(com.badlogic.gdx.physics.bullet.collision.AllHitsRayResultCallback) com.badlogic.gdx.physics.bullet.collision.btManifoldPoint(com.badlogic.gdx.physics.bullet.collision.btManifoldPoint) com.badlogic.gdx.physics.bullet.collision.btCollisionObject(com.badlogic.gdx.physics.bullet.collision.btCollisionObject)

Aggregations

AllHitsRayResultCallback (com.badlogic.gdx.physics.bullet.collision.AllHitsRayResultCallback)1 com.badlogic.gdx.physics.bullet.collision.btCollisionObject (com.badlogic.gdx.physics.bullet.collision.btCollisionObject)1 com.badlogic.gdx.physics.bullet.collision.btCollisionObjectConstArray (com.badlogic.gdx.physics.bullet.collision.btCollisionObjectConstArray)1 com.badlogic.gdx.physics.bullet.collision.btManifoldPoint (com.badlogic.gdx.physics.bullet.collision.btManifoldPoint)1 com.badlogic.gdx.physics.bullet.collision.btVector3i (com.badlogic.gdx.physics.bullet.collision.btVector3i)1 Vector3f (org.joml.Vector3f)1 Vector3i (org.joml.Vector3i)1 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)1 HitResult (org.terasology.engine.physics.HitResult)1