JBullet Jumping Mechanics

I’ve been working out some JBullet jumping mechanics, and I’ve hit a problem. Basically, I have no way of testing for the object touching the ground. I’ve tried using Raycasting, but that’s not turning out so well.

Here’s the player’s rigid body,

CollisionShape boxShape = new BoxShape(new Vector3f(0.5f, 1.7f, 0.5f));
MotionState boxMotionState = new DefaultMotionState();
Vector3f boxInertia = new Vector3f(0, 0, 0);
boxShape.calculateLocalInertia(2.5f, boxInertia);
RigidBodyConstructionInfo boxConstructionInfo = new RigidBodyConstructionInfo(8.5f, boxMotionState, boxShape, boxInertia);
boxConstructionInfo.restitution = 0.2f;
boxConstructionInfo.angularDamping = 0.95f;
boxConstructionInfo.friction = 10.0f;
RigidBody dynBody = new RigidBody(boxConstructionInfo);
dynBody.setActivationState(RigidBody.DISABLE_DEACTIVATION);
dynBody.setAngularFactor(0);

I need to test this if this object is colliding with the ground, and to do so, I used raycasted for the object below the player, then subtracted their transforms.

Solution:

Here’s how I raycast for the collision shape below the player:

RayResultCallback result = new CollisionWorld.ClosestRayResultCallback(dynamicPosition, new Vector3f(dynamicPosition.x, -1f, dynamicPosition.z));
getWorld().getDynWorld().rayTest(dynamicPosition, new Vector3f(dynamicPosition.x, -1f, dynamicPosition.z), result);

if(result.hasHit()) {
        // The important part
+	Vector3f difference = new Vector3f(result.collisionObject.getWorldTransform(new Transform()).origin);
+	difference.negate(dynamicPosition);
+	difference.negate();
				
	if(difference.y < 2.3){     // Touching an object? (Less than the player's height + 0.3)						
		if(Launcher.getInstance().getWindow().getKeyboard().isPressed(GLFWKeyboard.KEY_W)){
			deltaControlVelocity.x += (float) (Math.sin(-cameraRotation.y - 180 * Math.PI / 180) * frameSpeed);
			deltaControlVelocity.z += (float) (Math.cos(-cameraRotation.y - 180 * Math.PI / 180) * frameSpeed);
		}
		if(Launcher.getInstance().getWindow().getKeyboard().isPressed(GLFWKeyboard.KEY_S)){
			deltaControlVelocity.x -= (float) (Math.sin(-cameraRotation.y + 180 * Math.PI / 180) * frameSpeed);
			deltaControlVelocity.z -= (float) (Math.cos(-cameraRotation.y + 180 * Math.PI / 180) * frameSpeed);
		}
		if(Launcher.getInstance().getWindow().getKeyboard().isPressed(GLFWKeyboard.KEY_A)){
			deltaControlVelocity.x += (float) (Math.sin(-cameraRotation.y - 90 * Math.PI / 180) * frameSpeed);
			deltaControlVelocity.z += (float) (Math.cos(-cameraRotation.y - 90 * Math.PI / 180) * frameSpeed);
		}
		if(Launcher.getInstance().getWindow().getKeyboard().isPressed(GLFWKeyboard.KEY_D)){
			deltaControlVelocity.x += (float) (Math.sin(-cameraRotation.y + 90 * Math.PI / 180) * frameSpeed);
			deltaControlVelocity.z += (float) (Math.cos(-cameraRotation.y + 90 * Math.PI / 180) * frameSpeed);
		}
		System.out.println("Able to jump, distance is " + difference);
		if(Launcher.getInstance().getWindow().getKeyboard().isClicked(GLFWKeyboard.KEY_SPACE)){
			System.out.println("Jump");
			deltaControlVelocity.y += SPEED * dt;
		}
	} else {
		System.out.println("Unable to jump, distance is " + difference);
	}
}

Problem:

In fig. 1, The distance is calculated from the distance between the player’s transform and the object found through raycast below
In fig. 2, The distance is calculated from the distance between the player’s transform and the actual distance between collisions

So, in other words, is there a way to find the distance between collisions, and not the object’s transforms?

Thanks in advance!

Does anyone have an answer? I still can’t find anything online… Even checking for if the box is colliding with the object underneath it is acceptable…

I believe you are supposed to use the closestHitFraction float that is returned with the callback result in order to calculate the distance to the collision point.

Thanks for the reply. I’ll go look it up when I get home. Thanks!