This class represents a fixed position with respect to a trackable. The user needs to create them explicitly at any arbitrary position, or at the intersection with the touch pointer.
Creates a new anchor attached to the given trackable with position (x, y, z) with respect to the trackable.
Creates a new anchor at the hit position in the given trackable, which must be returned by ARTracker.get(mx, my).
Releases the resources used by the anchor. It should be done immediately after an anchor is no longer being tracked.
Returns a unique string ID for this anchor.
Returns the pose matrix of this anchor.
Applies the pose matrix of this anchor, so that all the geometry drawn afterwards is fixed with respect to the anchor.
Clears the pose matrix of this anchor, so geometry can be drawn normally.
Returns true if the anchor is currently being tracked.
Returns true if the anchor is currently paused.
Returns true if the anchor is no longer tracked, it should be disposed.
Returns true if the anchor has been disposed.
import processing.ar.*;
ARTracker tracker;
ARAnchor touchAnchor;
ArrayList<ARAnchor> trackAnchors;
float angle;
void setup() {
fullScreen(AR);
tracker = new ARTracker(this);
tracker.start();
trackAnchors = new ArrayList<ARAnchor>();
}
void draw() {
lights();
if (mousePressed) {
// Create new anchor at the current touch point
if (touchAnchor != null) touchAnchor.dispose();
ARTrackable hit = tracker.get(mouseX, mouseY);
if (hit != null) touchAnchor = new ARAnchor(hit);
else touchAnchor = null;
}
// Draw objects attached to each anchor
for (ARAnchor anchor : trackAnchors) {
if (anchor.isTracking()) drawBox(anchor, 255, 255, 255);
// It is very important to dispose anchors once they
// are no longer tracked.
if (anchor.isStopped()) anchor.dispose();
}
if (touchAnchor != null) drawBox(touchAnchor, 255, 0, 0);
// Conveniency function in the tracker object to remove
// disposed anchors from a list
tracker.clearAnchors(trackAnchors);
// Draw trackable planes
for (int i = 0; i < tracker.count(); i++) {
ARTrackable trackable = tracker.get(i);
if (!trackable.isTracking()) continue;
pushMatrix();
trackable.transform();
if (mousePressed && trackable.isSelected(mouseX, mouseY)) {
fill(255, 0, 0, 100);
} else {
fill(255, 100);
}
beginShape();
float[] points = trackable.getPolygon();
for (int n = 0; n < points.length / 2; n++) {
float x = points[2 * n];
float z = points[2 * n + 1];
vertex(x, 0, z);
}
endShape();
popMatrix();
}
angle += 0.1;
}
void drawBox(ARAnchor anchor, int r, int g, int b) {
anchor.attach();
fill(r, g, b);
rotateY(angle);
box(0.15f);
anchor.detach();
}
void trackableEvent(ARTrackable t) {
if (trackAnchors.size() < 10) {
float x0 = 0, y0 = 0;
if (t.isWallPlane()) {
// The new trackable is a wall, so adding the anchor
// 0.3 meters to its side
x0 = 0.3;
} else if (t.isFloorPlane()) {
// The new trackable is a floor plane, so adding the
// anchor 0.3 meters above it
y0 = 0.3;
} else {
// The new trackable is a floor plane, so adding the
// anchor 0.3 meters below it
y0 = -0.3;
}
trackAnchors.add(new ARAnchor(t, x0, y0, 0));
}
}