Physics Raycast?

I wrote such a thing a few years back, it uses physics raycasts to check how far above the terrain my vehicle was flying:

import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.PhysicsTickListener;
import com.jme3.bullet.collision.PhysicsRayTestResult;
import com.jme3.bullet.objects.PhysicsRigidBody;
...
    private float findHeight(final Vector3f from) {
        Vector3f to = new Vector3f(from.x, from.y - RAYTEST_LENGTH, from.z);
        List<PhysicsRayTestResult> results = new ArrayList<PhysicsRayTestResult>();
        this.physicsSpace.rayTest(from, to, results);
        float height = RAYTEST_LENGTH;
        for (PhysicsRayTestResult result : results) {
            int collisionGroup = result.getCollisionObject().getCollisionGroup();
            if (!Constants.CollisionGroup.isPlayer(collisionGroup)) {
                float intersection = result.getHitFraction() * RAYTEST_LENGTH;
                height = Math.min(height, intersection);
            }
        }
        return height;
    }

It casta a ray of length RAYTEST_LENGTH in the negative-Y direction. It filters out collisions with ‘player’ to eliminate self-intersections.
Hope that’ll get you started.

3 Likes