Android ArCore. How I can make a rotation for some object along a line
I develop an android application with ArCore library. I need make two points, draw a line between they and set a text labels near each line.
The text labels need to be rotated face-to-camera and have positions along the line.
1) Add a line between 2 points:
AnchorNode previousPoint = /* point 0 */
AnchorNode currentPoint = /* point 1 */
Vector3 previousPosition = previousPoint.getWorldPosition();
Vector3 currentPosition = currentPoint.getWorldPosition();
Quaternion rotation = currentPoint.getWorldRotation();
float f1 = new floatcurrentPosition.x, currentPosition.y, currentPosition.z;
float f2 = new floatrotation.x, rotation.y, rotation.z, rotation.w;
Vector3 difference = Vector3.subtract(previousPosition, currentPosition);
Vector3 directionFromTopToBottom = difference.normalized();
Quaternion rotationFromAToB = Quaternion.lookRotation(directionFromTopToBottom, Vector3.up());
ModelRenderable model = ShapeFactory.makeCube(new Vector3(.0025f, .0025f, difference.length()), Vector3.zero(), /* material */ );
Anchor anchor = getSession().createAnchor(new Pose(f1, f2));
AnchorNode anchorNode = new AnchorNode(anchor);
anchorNode.setParent(getScene());
Vector3 lineCenter = Vector3.add(previousPosition, currentPosition).scaled(.5f);
Node lineNode = new Node();
lineNode.setParent(anchorNode);
lineNode.setRenderable(model);
lineNode.setWorldPosition(lineCenter);
lineNode.setWorldRotation(rotationFromAToB);
- Add a text label
Quaternion cameraRotation = getCamera().getWorldRotation();
float distance = Vector3.subtract(cameraPosition, lineCenter).length();
String result = String.format(Locale.ENGLISH, "%.3f", difference.length()) + ".m";
ViewRenderable textRenderable = mArModelCreator.getTextView();
((TextView)textRenderable.getView()
.findViewById(R.id.textLabel)).setText(result);
Node textNode = new Node();
textNode.setParent(lineNode);
textNode.setRenderable(textRenderable);
textNode.setWorldScale(new Vector3(1f, 1f, 1f).scaled(distance));
textNode.setWorldRotation(Quaternion.multiply(rotationFromAToB, cameraRotation));
But I do not have correct result.
My current labels:
What I need:
Please help me.
android rotation quaternions arcore
add a comment |
I develop an android application with ArCore library. I need make two points, draw a line between they and set a text labels near each line.
The text labels need to be rotated face-to-camera and have positions along the line.
1) Add a line between 2 points:
AnchorNode previousPoint = /* point 0 */
AnchorNode currentPoint = /* point 1 */
Vector3 previousPosition = previousPoint.getWorldPosition();
Vector3 currentPosition = currentPoint.getWorldPosition();
Quaternion rotation = currentPoint.getWorldRotation();
float f1 = new floatcurrentPosition.x, currentPosition.y, currentPosition.z;
float f2 = new floatrotation.x, rotation.y, rotation.z, rotation.w;
Vector3 difference = Vector3.subtract(previousPosition, currentPosition);
Vector3 directionFromTopToBottom = difference.normalized();
Quaternion rotationFromAToB = Quaternion.lookRotation(directionFromTopToBottom, Vector3.up());
ModelRenderable model = ShapeFactory.makeCube(new Vector3(.0025f, .0025f, difference.length()), Vector3.zero(), /* material */ );
Anchor anchor = getSession().createAnchor(new Pose(f1, f2));
AnchorNode anchorNode = new AnchorNode(anchor);
anchorNode.setParent(getScene());
Vector3 lineCenter = Vector3.add(previousPosition, currentPosition).scaled(.5f);
Node lineNode = new Node();
lineNode.setParent(anchorNode);
lineNode.setRenderable(model);
lineNode.setWorldPosition(lineCenter);
lineNode.setWorldRotation(rotationFromAToB);
- Add a text label
Quaternion cameraRotation = getCamera().getWorldRotation();
float distance = Vector3.subtract(cameraPosition, lineCenter).length();
String result = String.format(Locale.ENGLISH, "%.3f", difference.length()) + ".m";
ViewRenderable textRenderable = mArModelCreator.getTextView();
((TextView)textRenderable.getView()
.findViewById(R.id.textLabel)).setText(result);
Node textNode = new Node();
textNode.setParent(lineNode);
textNode.setRenderable(textRenderable);
textNode.setWorldScale(new Vector3(1f, 1f, 1f).scaled(distance));
textNode.setWorldRotation(Quaternion.multiply(rotationFromAToB, cameraRotation));
But I do not have correct result.
My current labels:
What I need:
Please help me.
android rotation quaternions arcore
add a comment |
I develop an android application with ArCore library. I need make two points, draw a line between they and set a text labels near each line.
The text labels need to be rotated face-to-camera and have positions along the line.
1) Add a line between 2 points:
AnchorNode previousPoint = /* point 0 */
AnchorNode currentPoint = /* point 1 */
Vector3 previousPosition = previousPoint.getWorldPosition();
Vector3 currentPosition = currentPoint.getWorldPosition();
Quaternion rotation = currentPoint.getWorldRotation();
float f1 = new floatcurrentPosition.x, currentPosition.y, currentPosition.z;
float f2 = new floatrotation.x, rotation.y, rotation.z, rotation.w;
Vector3 difference = Vector3.subtract(previousPosition, currentPosition);
Vector3 directionFromTopToBottom = difference.normalized();
Quaternion rotationFromAToB = Quaternion.lookRotation(directionFromTopToBottom, Vector3.up());
ModelRenderable model = ShapeFactory.makeCube(new Vector3(.0025f, .0025f, difference.length()), Vector3.zero(), /* material */ );
Anchor anchor = getSession().createAnchor(new Pose(f1, f2));
AnchorNode anchorNode = new AnchorNode(anchor);
anchorNode.setParent(getScene());
Vector3 lineCenter = Vector3.add(previousPosition, currentPosition).scaled(.5f);
Node lineNode = new Node();
lineNode.setParent(anchorNode);
lineNode.setRenderable(model);
lineNode.setWorldPosition(lineCenter);
lineNode.setWorldRotation(rotationFromAToB);
- Add a text label
Quaternion cameraRotation = getCamera().getWorldRotation();
float distance = Vector3.subtract(cameraPosition, lineCenter).length();
String result = String.format(Locale.ENGLISH, "%.3f", difference.length()) + ".m";
ViewRenderable textRenderable = mArModelCreator.getTextView();
((TextView)textRenderable.getView()
.findViewById(R.id.textLabel)).setText(result);
Node textNode = new Node();
textNode.setParent(lineNode);
textNode.setRenderable(textRenderable);
textNode.setWorldScale(new Vector3(1f, 1f, 1f).scaled(distance));
textNode.setWorldRotation(Quaternion.multiply(rotationFromAToB, cameraRotation));
But I do not have correct result.
My current labels:
What I need:
Please help me.
android rotation quaternions arcore
I develop an android application with ArCore library. I need make two points, draw a line between they and set a text labels near each line.
The text labels need to be rotated face-to-camera and have positions along the line.
1) Add a line between 2 points:
AnchorNode previousPoint = /* point 0 */
AnchorNode currentPoint = /* point 1 */
Vector3 previousPosition = previousPoint.getWorldPosition();
Vector3 currentPosition = currentPoint.getWorldPosition();
Quaternion rotation = currentPoint.getWorldRotation();
float f1 = new floatcurrentPosition.x, currentPosition.y, currentPosition.z;
float f2 = new floatrotation.x, rotation.y, rotation.z, rotation.w;
Vector3 difference = Vector3.subtract(previousPosition, currentPosition);
Vector3 directionFromTopToBottom = difference.normalized();
Quaternion rotationFromAToB = Quaternion.lookRotation(directionFromTopToBottom, Vector3.up());
ModelRenderable model = ShapeFactory.makeCube(new Vector3(.0025f, .0025f, difference.length()), Vector3.zero(), /* material */ );
Anchor anchor = getSession().createAnchor(new Pose(f1, f2));
AnchorNode anchorNode = new AnchorNode(anchor);
anchorNode.setParent(getScene());
Vector3 lineCenter = Vector3.add(previousPosition, currentPosition).scaled(.5f);
Node lineNode = new Node();
lineNode.setParent(anchorNode);
lineNode.setRenderable(model);
lineNode.setWorldPosition(lineCenter);
lineNode.setWorldRotation(rotationFromAToB);
- Add a text label
Quaternion cameraRotation = getCamera().getWorldRotation();
float distance = Vector3.subtract(cameraPosition, lineCenter).length();
String result = String.format(Locale.ENGLISH, "%.3f", difference.length()) + ".m";
ViewRenderable textRenderable = mArModelCreator.getTextView();
((TextView)textRenderable.getView()
.findViewById(R.id.textLabel)).setText(result);
Node textNode = new Node();
textNode.setParent(lineNode);
textNode.setRenderable(textRenderable);
textNode.setWorldScale(new Vector3(1f, 1f, 1f).scaled(distance));
textNode.setWorldRotation(Quaternion.multiply(rotationFromAToB, cameraRotation));
But I do not have correct result.
My current labels:
What I need:
Please help me.
android rotation quaternions arcore
android rotation quaternions arcore
edited Nov 12 '18 at 20:18
Andrew Grow
asked Nov 12 '18 at 20:10
Andrew GrowAndrew Grow
20638
20638
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
For resolving the issue I have make a line between vectors by cylinder.
private void addLineBetweenPoints(Scene scene, Vector3 from, Vector3 to)
// prepare an anchor position
Quaternion camQ = scene.getCamera().getWorldRotation();
float f1 = new floatto.x, to.y, to.z;
float f2 = new floatcamQ.x, camQ.y, camQ.z, camQ.w;
Pose anchorPose = new Pose(f1, f2);
// make an ARCore Anchor
Anchor anchor = mCallback.getSession().createAnchor(anchorPose);
// Node that is automatically positioned in world space based on the ARCore Anchor.
AnchorNode anchorNode = new AnchorNode(anchor);
anchorNode.setParent(scene);
// Compute a line's length
float lineLength = Vector3.subtract(from, to).length();
// Prepare a color
Color colorOrange = new Color(android.graphics.Color.parseColor("#ffa71c"));
// 1. make a material by the color
MaterialFactory.makeOpaqueWithColor(getContext(), colorOrange)
.thenAccept(material ->
// 2. make a model by the material
ModelRenderable model = ShapeFactory.makeCylinder(0.0025f, lineLength,
new Vector3(0f, lineLength / 2, 0f), material);
model.setShadowReceiver(false);
model.setShadowCaster(false);
// 3. make node
Node node = new Node();
node.setRenderable(model);
node.setParent(anchorNode);
// 4. set rotation
final Vector3 difference = Vector3.subtract(to, from);
final Vector3 directionFromTopToBottom = difference.normalized();
final Quaternion rotationFromAToB =
Quaternion.lookRotation(directionFromTopToBottom, Vector3.up());
node.setWorldRotation(Quaternion.multiply(rotationFromAToB,
Quaternion.axisAngle(new Vector3(1.0f, 0.0f, 0.0f), 90)));
);
and add a TextNode:
public ViewRenderable getTextView()
// return current and make new;
ViewRenderable renderable = mTextView.makeCopy();
makeTextModel();
return renderable;
private void makeTextModel()
ViewRenderable.builder()
.setView(LvgApplication.getContext(), R.layout.view_ar_text)
.build()
.thenAccept(renderable ->
renderable.setShadowCaster(false);
renderable.setShadowReceiver(false);
DpToMetersViewSizer viewSizer = new DpToMetersViewSizer(500);
renderable.setSizer(viewSizer);
renderable.setHorizontalAlignment(ViewRenderable.HorizontalAlignment.CENTER);
renderable.setVerticalAlignment(ViewRenderable.VerticalAlignment.CENTER);
mTextView = renderable;
);
private void addTextNodeToLine(Node node, float length)
// make model
String result = String.format(Locale.ENGLISH, "%.3f", length) + ".m";
ViewRenderable textRenderable = mArModelCreator.getTextView();
((TextView) textRenderable.getView().findViewById(R.id.textLabel)).setText(result);
Node textNode = new Node();
textNode.setParent(node);
textNode.setRenderable(textRenderable);
textNode.setWorldScale(new Vector3(1f, 1f, 1f).scaled(0));
textNode.setLocalPosition(new Vector3(
/* x - width from parent (left/right) */ -0.025f,
/* y - depth from parent (forward/back) */ length / 2,
/* z - height from parent (top/down) */ -0.002f));
Quaternion alongLine = Quaternion.axisAngle(new Vector3(0.0f, 0.0f, 1.0f), -90);
textNode.setLocalRotation(alongLine);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269393%2fandroid-arcore-how-i-can-make-a-rotation-for-some-object-along-a-line%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
For resolving the issue I have make a line between vectors by cylinder.
private void addLineBetweenPoints(Scene scene, Vector3 from, Vector3 to)
// prepare an anchor position
Quaternion camQ = scene.getCamera().getWorldRotation();
float f1 = new floatto.x, to.y, to.z;
float f2 = new floatcamQ.x, camQ.y, camQ.z, camQ.w;
Pose anchorPose = new Pose(f1, f2);
// make an ARCore Anchor
Anchor anchor = mCallback.getSession().createAnchor(anchorPose);
// Node that is automatically positioned in world space based on the ARCore Anchor.
AnchorNode anchorNode = new AnchorNode(anchor);
anchorNode.setParent(scene);
// Compute a line's length
float lineLength = Vector3.subtract(from, to).length();
// Prepare a color
Color colorOrange = new Color(android.graphics.Color.parseColor("#ffa71c"));
// 1. make a material by the color
MaterialFactory.makeOpaqueWithColor(getContext(), colorOrange)
.thenAccept(material ->
// 2. make a model by the material
ModelRenderable model = ShapeFactory.makeCylinder(0.0025f, lineLength,
new Vector3(0f, lineLength / 2, 0f), material);
model.setShadowReceiver(false);
model.setShadowCaster(false);
// 3. make node
Node node = new Node();
node.setRenderable(model);
node.setParent(anchorNode);
// 4. set rotation
final Vector3 difference = Vector3.subtract(to, from);
final Vector3 directionFromTopToBottom = difference.normalized();
final Quaternion rotationFromAToB =
Quaternion.lookRotation(directionFromTopToBottom, Vector3.up());
node.setWorldRotation(Quaternion.multiply(rotationFromAToB,
Quaternion.axisAngle(new Vector3(1.0f, 0.0f, 0.0f), 90)));
);
and add a TextNode:
public ViewRenderable getTextView()
// return current and make new;
ViewRenderable renderable = mTextView.makeCopy();
makeTextModel();
return renderable;
private void makeTextModel()
ViewRenderable.builder()
.setView(LvgApplication.getContext(), R.layout.view_ar_text)
.build()
.thenAccept(renderable ->
renderable.setShadowCaster(false);
renderable.setShadowReceiver(false);
DpToMetersViewSizer viewSizer = new DpToMetersViewSizer(500);
renderable.setSizer(viewSizer);
renderable.setHorizontalAlignment(ViewRenderable.HorizontalAlignment.CENTER);
renderable.setVerticalAlignment(ViewRenderable.VerticalAlignment.CENTER);
mTextView = renderable;
);
private void addTextNodeToLine(Node node, float length)
// make model
String result = String.format(Locale.ENGLISH, "%.3f", length) + ".m";
ViewRenderable textRenderable = mArModelCreator.getTextView();
((TextView) textRenderable.getView().findViewById(R.id.textLabel)).setText(result);
Node textNode = new Node();
textNode.setParent(node);
textNode.setRenderable(textRenderable);
textNode.setWorldScale(new Vector3(1f, 1f, 1f).scaled(0));
textNode.setLocalPosition(new Vector3(
/* x - width from parent (left/right) */ -0.025f,
/* y - depth from parent (forward/back) */ length / 2,
/* z - height from parent (top/down) */ -0.002f));
Quaternion alongLine = Quaternion.axisAngle(new Vector3(0.0f, 0.0f, 1.0f), -90);
textNode.setLocalRotation(alongLine);
add a comment |
For resolving the issue I have make a line between vectors by cylinder.
private void addLineBetweenPoints(Scene scene, Vector3 from, Vector3 to)
// prepare an anchor position
Quaternion camQ = scene.getCamera().getWorldRotation();
float f1 = new floatto.x, to.y, to.z;
float f2 = new floatcamQ.x, camQ.y, camQ.z, camQ.w;
Pose anchorPose = new Pose(f1, f2);
// make an ARCore Anchor
Anchor anchor = mCallback.getSession().createAnchor(anchorPose);
// Node that is automatically positioned in world space based on the ARCore Anchor.
AnchorNode anchorNode = new AnchorNode(anchor);
anchorNode.setParent(scene);
// Compute a line's length
float lineLength = Vector3.subtract(from, to).length();
// Prepare a color
Color colorOrange = new Color(android.graphics.Color.parseColor("#ffa71c"));
// 1. make a material by the color
MaterialFactory.makeOpaqueWithColor(getContext(), colorOrange)
.thenAccept(material ->
// 2. make a model by the material
ModelRenderable model = ShapeFactory.makeCylinder(0.0025f, lineLength,
new Vector3(0f, lineLength / 2, 0f), material);
model.setShadowReceiver(false);
model.setShadowCaster(false);
// 3. make node
Node node = new Node();
node.setRenderable(model);
node.setParent(anchorNode);
// 4. set rotation
final Vector3 difference = Vector3.subtract(to, from);
final Vector3 directionFromTopToBottom = difference.normalized();
final Quaternion rotationFromAToB =
Quaternion.lookRotation(directionFromTopToBottom, Vector3.up());
node.setWorldRotation(Quaternion.multiply(rotationFromAToB,
Quaternion.axisAngle(new Vector3(1.0f, 0.0f, 0.0f), 90)));
);
and add a TextNode:
public ViewRenderable getTextView()
// return current and make new;
ViewRenderable renderable = mTextView.makeCopy();
makeTextModel();
return renderable;
private void makeTextModel()
ViewRenderable.builder()
.setView(LvgApplication.getContext(), R.layout.view_ar_text)
.build()
.thenAccept(renderable ->
renderable.setShadowCaster(false);
renderable.setShadowReceiver(false);
DpToMetersViewSizer viewSizer = new DpToMetersViewSizer(500);
renderable.setSizer(viewSizer);
renderable.setHorizontalAlignment(ViewRenderable.HorizontalAlignment.CENTER);
renderable.setVerticalAlignment(ViewRenderable.VerticalAlignment.CENTER);
mTextView = renderable;
);
private void addTextNodeToLine(Node node, float length)
// make model
String result = String.format(Locale.ENGLISH, "%.3f", length) + ".m";
ViewRenderable textRenderable = mArModelCreator.getTextView();
((TextView) textRenderable.getView().findViewById(R.id.textLabel)).setText(result);
Node textNode = new Node();
textNode.setParent(node);
textNode.setRenderable(textRenderable);
textNode.setWorldScale(new Vector3(1f, 1f, 1f).scaled(0));
textNode.setLocalPosition(new Vector3(
/* x - width from parent (left/right) */ -0.025f,
/* y - depth from parent (forward/back) */ length / 2,
/* z - height from parent (top/down) */ -0.002f));
Quaternion alongLine = Quaternion.axisAngle(new Vector3(0.0f, 0.0f, 1.0f), -90);
textNode.setLocalRotation(alongLine);
add a comment |
For resolving the issue I have make a line between vectors by cylinder.
private void addLineBetweenPoints(Scene scene, Vector3 from, Vector3 to)
// prepare an anchor position
Quaternion camQ = scene.getCamera().getWorldRotation();
float f1 = new floatto.x, to.y, to.z;
float f2 = new floatcamQ.x, camQ.y, camQ.z, camQ.w;
Pose anchorPose = new Pose(f1, f2);
// make an ARCore Anchor
Anchor anchor = mCallback.getSession().createAnchor(anchorPose);
// Node that is automatically positioned in world space based on the ARCore Anchor.
AnchorNode anchorNode = new AnchorNode(anchor);
anchorNode.setParent(scene);
// Compute a line's length
float lineLength = Vector3.subtract(from, to).length();
// Prepare a color
Color colorOrange = new Color(android.graphics.Color.parseColor("#ffa71c"));
// 1. make a material by the color
MaterialFactory.makeOpaqueWithColor(getContext(), colorOrange)
.thenAccept(material ->
// 2. make a model by the material
ModelRenderable model = ShapeFactory.makeCylinder(0.0025f, lineLength,
new Vector3(0f, lineLength / 2, 0f), material);
model.setShadowReceiver(false);
model.setShadowCaster(false);
// 3. make node
Node node = new Node();
node.setRenderable(model);
node.setParent(anchorNode);
// 4. set rotation
final Vector3 difference = Vector3.subtract(to, from);
final Vector3 directionFromTopToBottom = difference.normalized();
final Quaternion rotationFromAToB =
Quaternion.lookRotation(directionFromTopToBottom, Vector3.up());
node.setWorldRotation(Quaternion.multiply(rotationFromAToB,
Quaternion.axisAngle(new Vector3(1.0f, 0.0f, 0.0f), 90)));
);
and add a TextNode:
public ViewRenderable getTextView()
// return current and make new;
ViewRenderable renderable = mTextView.makeCopy();
makeTextModel();
return renderable;
private void makeTextModel()
ViewRenderable.builder()
.setView(LvgApplication.getContext(), R.layout.view_ar_text)
.build()
.thenAccept(renderable ->
renderable.setShadowCaster(false);
renderable.setShadowReceiver(false);
DpToMetersViewSizer viewSizer = new DpToMetersViewSizer(500);
renderable.setSizer(viewSizer);
renderable.setHorizontalAlignment(ViewRenderable.HorizontalAlignment.CENTER);
renderable.setVerticalAlignment(ViewRenderable.VerticalAlignment.CENTER);
mTextView = renderable;
);
private void addTextNodeToLine(Node node, float length)
// make model
String result = String.format(Locale.ENGLISH, "%.3f", length) + ".m";
ViewRenderable textRenderable = mArModelCreator.getTextView();
((TextView) textRenderable.getView().findViewById(R.id.textLabel)).setText(result);
Node textNode = new Node();
textNode.setParent(node);
textNode.setRenderable(textRenderable);
textNode.setWorldScale(new Vector3(1f, 1f, 1f).scaled(0));
textNode.setLocalPosition(new Vector3(
/* x - width from parent (left/right) */ -0.025f,
/* y - depth from parent (forward/back) */ length / 2,
/* z - height from parent (top/down) */ -0.002f));
Quaternion alongLine = Quaternion.axisAngle(new Vector3(0.0f, 0.0f, 1.0f), -90);
textNode.setLocalRotation(alongLine);
For resolving the issue I have make a line between vectors by cylinder.
private void addLineBetweenPoints(Scene scene, Vector3 from, Vector3 to)
// prepare an anchor position
Quaternion camQ = scene.getCamera().getWorldRotation();
float f1 = new floatto.x, to.y, to.z;
float f2 = new floatcamQ.x, camQ.y, camQ.z, camQ.w;
Pose anchorPose = new Pose(f1, f2);
// make an ARCore Anchor
Anchor anchor = mCallback.getSession().createAnchor(anchorPose);
// Node that is automatically positioned in world space based on the ARCore Anchor.
AnchorNode anchorNode = new AnchorNode(anchor);
anchorNode.setParent(scene);
// Compute a line's length
float lineLength = Vector3.subtract(from, to).length();
// Prepare a color
Color colorOrange = new Color(android.graphics.Color.parseColor("#ffa71c"));
// 1. make a material by the color
MaterialFactory.makeOpaqueWithColor(getContext(), colorOrange)
.thenAccept(material ->
// 2. make a model by the material
ModelRenderable model = ShapeFactory.makeCylinder(0.0025f, lineLength,
new Vector3(0f, lineLength / 2, 0f), material);
model.setShadowReceiver(false);
model.setShadowCaster(false);
// 3. make node
Node node = new Node();
node.setRenderable(model);
node.setParent(anchorNode);
// 4. set rotation
final Vector3 difference = Vector3.subtract(to, from);
final Vector3 directionFromTopToBottom = difference.normalized();
final Quaternion rotationFromAToB =
Quaternion.lookRotation(directionFromTopToBottom, Vector3.up());
node.setWorldRotation(Quaternion.multiply(rotationFromAToB,
Quaternion.axisAngle(new Vector3(1.0f, 0.0f, 0.0f), 90)));
);
and add a TextNode:
public ViewRenderable getTextView()
// return current and make new;
ViewRenderable renderable = mTextView.makeCopy();
makeTextModel();
return renderable;
private void makeTextModel()
ViewRenderable.builder()
.setView(LvgApplication.getContext(), R.layout.view_ar_text)
.build()
.thenAccept(renderable ->
renderable.setShadowCaster(false);
renderable.setShadowReceiver(false);
DpToMetersViewSizer viewSizer = new DpToMetersViewSizer(500);
renderable.setSizer(viewSizer);
renderable.setHorizontalAlignment(ViewRenderable.HorizontalAlignment.CENTER);
renderable.setVerticalAlignment(ViewRenderable.VerticalAlignment.CENTER);
mTextView = renderable;
);
private void addTextNodeToLine(Node node, float length)
// make model
String result = String.format(Locale.ENGLISH, "%.3f", length) + ".m";
ViewRenderable textRenderable = mArModelCreator.getTextView();
((TextView) textRenderable.getView().findViewById(R.id.textLabel)).setText(result);
Node textNode = new Node();
textNode.setParent(node);
textNode.setRenderable(textRenderable);
textNode.setWorldScale(new Vector3(1f, 1f, 1f).scaled(0));
textNode.setLocalPosition(new Vector3(
/* x - width from parent (left/right) */ -0.025f,
/* y - depth from parent (forward/back) */ length / 2,
/* z - height from parent (top/down) */ -0.002f));
Quaternion alongLine = Quaternion.axisAngle(new Vector3(0.0f, 0.0f, 1.0f), -90);
textNode.setLocalRotation(alongLine);
answered Nov 22 '18 at 20:46
Andrew GrowAndrew Grow
20638
20638
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269393%2fandroid-arcore-how-i-can-make-a-rotation-for-some-object-along-a-line%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown