In the Java ARCore Hello AR
sample we can place Android object on the plane by tapping on the screen, how could we use this HitResult
`s information to draw line between these objects?
Thanks for helping!
In the section of code where you are grabbing the anchor to place the object, you should check if you already have a previous anchor. If you do have a previous anchor, grab the worldPosition (as a Vector3 object) from previous and current anchors, then calculate the difference between them, and create a line that length, and attach it to the scene at the halfway point between the two points. Finally, set the previousAnchor to the current one.
Here is some code that I used to solve this:
// Create the Anchor.
Anchor anchor = hitResult.createAnchor();
AnchorNode anchorNode = new AnchorNode(anchor);
// Code to insert object probably happens here
if (lastAnchorNode != null) {
Vector3 point1, point2;
point1 = lastAnchorNode.getWorldPosition();
point2 = anchorNode.getWorldPosition();
Node line = new Node();
/* First, find the vector extending between the two points and define a look rotation in terms of this
Vector. */
final Vector3 difference = Vector3.subtract(point1, point2);
final Vector3 directionFromTopToBottom = difference.normalized();
final Quaternion rotationFromAToB =
Quaternion.lookRotation(directionFromTopToBottom, Vector3.up());
final Renderable[] lineRenderable = new Renderable[1];
/* Then, create a rectangular prism, using ShapeFactory.makeCube() and use the difference vector
to extend to the necessary length. */
MaterialFactory.makeOpaqueWithColor(this, color)
.thenAccept(
material -> {
lineRenderable[0] = ShapeFactory.makeCube(new Vector3(.01f, .01f, difference.length()),
Vector3.zero(), material);
});
/* Last, set the world rotation of the node to the rotation calculated earlier and set the world position to
the midpoint between the given points . */
line.setParent(anchorNode);
line.setRenderable(lineRenderable[0]);
line.setWorldPosition(Vector3.add(point1, point2).scaled(.5f));
line.setWorldRotation(rotationFromAToB);
}
lastAnchorNode = anchorNode;
Anchor
will be helpful for you. thanks to it you can track tapped positions and use those coordinates between points. I've done something similar to count the distance between two points that I've tapped
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With