Untitled

mail@pastecode.io avatar
unknown
plain_text
11 days ago
5.4 kB
6
Indexable
Never
    ModelRenderable.builder()
                .setSource(this, Uri.parse("models/andy.sfb"))
                .build()
                .thenAccept(renderable -> arrowRenderable = renderable)
                .exceptionally(
                        throwable -> {
                            // Handle errors while loading the model
                            return null;
                        }
                );
        
        arFragment.getArSceneView().getScene().addOnUpdateListener(frameTime -> {

            // Get the current AR frame
            com.google.ar.core.Frame frame = arFragment.getArSceneView().getArFrame();
            if (frame == null) {
                return;
            }
            // Get the camera's current position (user's position in AR space)
            Pose cameraPose = frame.getCamera().getPose();
            float[] cameraPosition = cameraPose.getTranslation();

            // Define the destination 50 meters ahead in the Z direction (adjust axis as needed)
            float[] destinationPosition = new float[]{cameraPosition[0], cameraPosition[1], cameraPosition[2] - 50.0f};

            // Place footsteps along the path
            placeFootstepsAlongPath(cameraPosition, destinationPosition, arFragment);
        });

    }

    // Rotate the arrow node to point towards the destination
    private void placeFootstepsAlongPath(float[] startPosition, float[] endPosition, ArFragment arFragment) {

        // Calculate the direction vector from start to end
        float[] directionVector = new float[]{
                endPosition[0] - startPosition[0],
                endPosition[1] - startPosition[1],
                endPosition[2] - startPosition[2]
        };

        // Calculate the total distance between the start and end positions
        float totalDistance = calculateDistanceBetweenPoints(startPosition, endPosition);

        // Normalize the direction vector (unit length)
        float[] unitVector = new float[3];
        for (int i = 0; i < 3; i++) {
            unitVector[i] = directionVector[i] / totalDistance;
        }

        // Define the spacing for footsteps (e.g., every 1 meter)
        float footstepSpacing = 20.0f;
        float distanceCovered = 0.0f;

        // Loop to place footsteps along the path
        while (distanceCovered < totalDistance) {
            // Calculate the next position for the footstep
            float[] nextPosition = new float[]{
                    startPosition[0] + unitVector[0] * distanceCovered,
                    startPosition[1] + unitVector[1] * distanceCovered,
                    startPosition[2] + unitVector[2] * distanceCovered
            };

            // Place a footstep object at this position
            placeFootstepAtLocation(nextPosition, arFragment);

            // Increment the distance covered
            distanceCovered += footstepSpacing;
        }
    }

    // Function to place a footstep object at a given location
    private void placeFootstepAtLocation(float[] position, ArFragment arFragment) {
        // Create a Pose at the given position (adjust for rotation if needed)
        if (arFragment.getArSceneView().getArFrame() != null &&
                arFragment.getArSceneView().getArFrame().getCamera().getTrackingState() == TrackingState.TRACKING) {
            // ARCore is ready, proceed to create the anchor
            Pose pose = new Pose(position, new float[]{0f, 0f, 0f, 1f});
           currentAnchor = arFragment.getArSceneView().getSession().createAnchor(pose);
            // Create an AnchorNode and attach it to the AR scene
            AnchorNode anchorNode = new AnchorNode(currentAnchor);
            anchorNode.setParent(arFragment.getArSceneView().getScene());

            // Add a TransformableNode with a footstep model
            TransformableNode footstepNode = new TransformableNode(arFragment.getTransformationSystem());
            footstepNode.setParent(anchorNode);
            footstepNode.setLocalScale(new Vector3(0.2f, 0.2f, 0.2f));  // Adjust scale for the footstep
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                ModelRenderable.builder()
                        .setSource(arFragment.getContext(), Uri.parse("models/andy.sfb")) // Replace with footstep model
                        .build()
                        .thenAccept(footstepNode::setRenderable)
                        .exceptionally(throwable -> {
                            Toast.makeText(arFragment.getContext(), "Error loading footstep model", Toast.LENGTH_LONG).show();
                            return null;
                        });
            }

        } else {
            // ARCore is not yet tracking, handle this state (e.g., prompt the user to move the device)
            Toast.makeText(this, "AR is not tracking. Move your device to find a surface.", Toast.LENGTH_SHORT).show();
        }

    }

    // Function to calculate the distance between two points in AR space
    private float calculateDistanceBetweenPoints(float[] start, float[] end) {
        float dx = end[0] - start[0];
        float dy = end[1] - start[1];
        float dz = end[2] - start[2];
        return (float) Math.sqrt(dx * dx + dy * dy + dz * dz);
    }
Leave a Comment