viromedia / virocore

ViroCore cross-platform AR/VR renderer

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Polygonal 2D Physics Body

marcspraragen opened this issue · comments

Environment

(Windows 10, Android Studio 3.4.1 compiling SDK 28, ViroCore 1_14.
Testing on Samsung S8+)

Description

I'm trying to set up a polygonal physics body spanning a detected horizontal plane anchor.

I'm extracting vertices from a found PlaneAnchor, and creating an associated Node with a new Polygon geometry based on these vertices. Then using physicsShapeAutoCompound() for the shape variable in the Node's initPhysicsBody call.

Unfortunately, I can't see what happens to the vertices once they pass from the explicit "Vertices" array in the PlaneAnchor, and the rendering is really not working.

Could someone please take a look at this? Even to say "Sorry, can't help you..." ?

Thanks,
--Marc

Reproducible Demo

Below is code for the scene listener I'm using, along with Android Studio + Samsung S8+ screenshots taken at the same frame.

The studio screenshot shows that the anchor has many vertices, but the rendering on the device shows something different.


plane

/**
 * Tracks and renders polygonal surface planes
 */
public class ARSurfacesHandler implements ARScene.Listener {

     private HashMap<String, Node> surfaces = new HashMap<String, Node>();

    @Override
    public void onTrackingUpdated(ARScene.TrackingState trackingState, ARScene.TrackingStateReason trackingStateReason) {
        //no-op
    }

    @Override
    public void onAnchorFound(ARAnchor arAnchor, ARNode arNode) {

        // Spawn a visual plane if a PlaneAnchor was found
        if (arAnchor.getType() == ARAnchor.Type.PLANE) {

            ARPlaneAnchor planeAnchor = (ARPlaneAnchor) arAnchor;

            // Attach it to the node
            Node surfaceNode = new Node();
            surfaceNode.setGeometry(new Polygon(planeAnchor.getVertices(),0,0,1,1));

            // Set a default material for this surface
            Material material = new Material();

            //... with a random color
            Random rand = new Random();
            float r = rand.nextFloat();
            float g = rand.nextFloat();
            float b = rand.nextFloat();
            material.setDiffuseColor(Color.rgb(r, g, b));

            surfaceNode.getGeometry().setMaterials(Arrays.asList(material));

            //Give it a static physics body shaped as polygon
            surfaceNode.initPhysicsBody(PhysicsBody.RigidBodyType.STATIC,0, new PhysicsShapeAutoCompound());

            // Attach this planeNode to the anchor's arNode
            arNode.addChildNode(surfaceNode);
            surfaceNode.setPosition(planeAnchor.getCenter());
            surfaces.put(arAnchor.getAnchorId(), surfaceNode);
        }
    }

    @Override
    public void onAnchorUpdated(ARAnchor arAnchor, ARNode arNode) {
        if (arAnchor.getType() == ARAnchor.Type.PLANE) {
            ARPlaneAnchor planeAnchor = (ARPlaneAnchor) arAnchor;

            // Update the mesh surface geometry
            Node surfaceNode = surfaces.get(arAnchor.getAnchorId());
            surfaceNode.setGeometry(new Polygon(planeAnchor.getVertices(),0,0,1,1));
            surfaceNode.getPhysicsBody().setShape(new PhysicsShapeAutoCompound());
        }
    }

    @Override
    public void onAnchorRemoved(ARAnchor arAnchor, ARNode arNode) {
        surfaces.remove(arAnchor.getAnchorId());
    }

    @Override
    public void onTrackingInitialized() {
        //No-op
    }

    @Override
    public void onAmbientLightUpdate(float lightIntensity, Vector lightColor) {
        //No-op
    }
}

Lastly, here is some suspicious logging from debug session:

I/Viro: Detected new anchor tied to plane
W/Viro: Duplicated vertex found and removed in VROPolygon!
W/native: localization_manager.cc:342 Map tracking detected failure condition that despite good tracking metrics gravity check failed. Reporting corrupted state.
I/native: performance_monitor.cc:115 Event: FeatureExtraction is taking too long, it took 110.452ms
W/native: plane_estimator.cc:290 Got a new plane, id: 6, with a invalid statistics.

Update: I realized that the red line was the debug draw; running without that switch on showed no planes at all.

Hey @marcspraragen, thanks for reaching out.

Looking at your debug output from planeAnchor.getVertices(), it looks like the output points are somewhat reasonable. However, it is strange that the polygon, with those vertex inputs, isn't being rendered at all. To debug could you try:

1 - Removing / commenting out any physics code (so that we can try and get the geometry rendering first).

2 - With the List of vector points above, take a few of them (say roughly 10) and try to create a Polygon manually in your scene (Just simply placing them in the scene, not hooked up to an ARAnchor). If there's an issue with the polygon rendering, we'll see them here.

3 - Instead of creating a new polygonal surface within onAnchorUpdated, instead create the polygonal surface within onAnchorFound and see if that works. If so, then there might be an issue with using onAnchorUpdate to update polygons.

Hey--

It turned out to be a simple fix, but tricky to find. Your troubleshooting breakdown really helped.

The Polygon constructor collapses each 3D Vector "vertex" into float[2], but populates the 2nd element with vertex.y, which in the case of a detected horizontal plane in a ViroCore Scene is always 0. Copying each vertex.z into vertex.y before calling constructor did the trick.

Thanks,
--Marc