Log in or register to post comments

Calculate the distance between two 3D object.

July 21, 2012 - 5:47am #13

Hi there~

I want to learn the tutorial demo that "draging a teapot onto a fire and it starts to steam".the tutorial video from July 2011 is here:

https://developer.qualcomm.com/develop/mobile-technologies/augmented-reality-vuforia/videos

I found the thread: https://ar.qualcomm.at/content/teapot-fire-tutorial-files

I used ksiva's code, that finds the distance between two targets to start the steam when the teapot gets close to the fire. but I found the distance changed when my iphone camera far or near from the Image Target. I core code is:

 float teapotToFireDistance = Vector2.Distance(new Vector2(m_Teapot.transform.position.x, m_Teapot.transform.position.z),
                                                      new Vector2(m_Fireplace.transform.position.x, m_Fireplace.transform.position.z));

teapotToFireDistance changed when my iphone camera(ARCamrea) far or near from the Image Target, but I want to a fixed value that just distance between teapot and fire, and unaffected by ARCamrea.

so how to modify the code?

Thanks!

 

Calculate the distance between two 3D object.

July 22, 2012 - 5:48pm #12

This may be due to slight errors in the pose estimation - is the change proportionally small?

The Vector2.Distance() function isn't dependent on the position of the ARCamera, it's using the positions of the teapot and fireplace in world space.

Calculate the distance between two 3D object.

July 22, 2012 - 11:28pm #11

Thanks DavidBeard, now my code is"

using UnityEngine;

using System.Collections;

[RequireComponent(typeof(ParticleEmitter))]

public class BoilingBehaviour : MonoBehaviour {

    public float heatingDistance = 0.0f// set in inspector

    public GameObject m_Fireplace = null// set in inspector

    public GameObject m_Teapot = null; // set in inspector

 

    void Update (){

        float teapotToFireDistance = Vector3.Distance(m_Teapot.transform.position, m_Fireplace.transform.position);

        Debug.Log(teapotToFireDistance);

 

        if (teapotToFireDistance < heatingDistance) {

            this.particleEmitter.emit = true;

        } else {

            this.particleEmitter.emit = false;

        }

    }

}

 

I check the distance from debug log, when the distance between teapot and fireplace was fixed, the distance value still change by the position of the ARCamera. when ARcamera was near, the value reduce, and when ARcamera was far away, the value increase.

So what I want is the value unaffected by ARCamrea.

 

Calculate the distance between two 3D object.

July 23, 2012 - 9:29am #10

Is that script on your ARCamera or an ImageTarget? It should be on the ImageTarget.

If it already is, then try the following script. It will enable you to observe discrete values for the distance measurement on screen. You'll notice that the distance between targets can change slightly even when their distance from the camera is constant. This is the nature of pose estimation. It's not going to give you an exact measurement. Also try testing the same on another device, and you'll likely see slightly different values due to differences in their cameras.

 

using UnityEngine;
using System.Collections;

//[RequireComponent(typeof(ParticleEmitter))]
public class BoilingBehaviour : MonoBehaviour
{
    public float heatingDistance = 0.0f;  // set in inspector
    public GameObject m_Fireplace = null;  // set in inspector
    private GameObject m_Teapot = null;
    float teapotToFireDistance = 0;
    string dist_val = "";

    void Start ()
    {
        m_Teapot = this.transform.root.gameObject;
    }

    void Update ()
    {       
        teapotToFireDistance = Vector2.Distance(new Vector2(m_Teapot.transform.position.x, m_Teapot.transform.position.z),
                                                      new Vector2(m_Fireplace.transform.position.x, m_Fireplace.transform.position.z));
       
        //Debug.Log( teapotToFireDistance.ToString() );
       
        if (teapotToFireDistance < heatingDistance)
        {
            //this.particleEmitter.emit = true;
        }
        else
        {
            //this.particleEmitter.emit = false;
        }
    }
   
    void OnGUI(){
       
        if( GUILayout.Button("Get Distance", GUILayout.Width(200), GUILayout.Height(200) ) ){
           
            dist_val = teapotToFireDistance.ToString();
        }
       
        GUILayout.Label( dist_val );
       
    }
}

 

 

Calculate the distance between two 3D object.

July 23, 2012 - 7:41pm #9

Hi DavidBeard

I use your script on the ImageTarget. the distance value show on screen now. when I put the teapot on the fireplace, the distance value is about 300, and now I move ARcamera closer, the value reduce to about 100, and when ARcamera was far away, the value increase to 600-800.

I don't know why the Vector2.Distance() function dependent on the position of the ARCamera, it should using the positions of the teapot and fireplace in world space. 

Calculate the distance between two 3D object.

July 24, 2012 - 12:28pm #8

Is the other ImageTarget defined in the script's inspector panel?

as public GameObject m_Fireplace = null;  // set in inspector

You really shouldn't be seeing that behavior unless one of the objects in the ARCamera.

Calculate the distance between two 3D object.

July 24, 2012 - 8:08pm #7

Hi DavidBeard,

I've upload the project to skydrive:

https://skydrive.live.com/redir?resid=D1175C749277BDAD!1769&authkey=!AHqpLbdAGpgM1S0

you can download it and have a test.

Thanks!

 

Calculate the distance between two 3D object.

July 27, 2012 - 3:25am #6

Hi DavidBeard,

I've upload the project to skydrive:

https://skydrive.live.com/redir?resid=D1175C749277BDAD!1769&authkey=!AHqpLbdAGpgM1S0

you can download it and have a test.

Thanks!

 

Calculate the distance between two 3D object.

July 31, 2012 - 7:41pm #5

Anyone help??

Calculate the distance between two 3D object.

August 1, 2012 - 1:27am #4

Hi

I am sorry I am very new in AR. And I cannot have you.

I tried the package uploaded by Jove.

The distance calculation works good!!

But I have a problem in start and stop emitting the particle. I deleted the  "//" in front of the condition.

Anyone knows the problem?

Calculate the distance between two 3D object.

August 21, 2012 - 7:12am #3

Any idea?

Calculate the distance between two 3D object.

August 21, 2012 - 7:35am #2

Jove which versions of Unity and XCode are you using, and which version of iOS is your iPhone running?

Calculate the distance between two 3D object.

June 20, 2014 - 8:06am #1

I can't download the package from Sky Drive

Log in or register to post comments