Trouble with Rotate 3d models Xamarin.Android Arcore UrhoSharp

angrydogangrydog Member ✭✭

Helo, I am start learning ARCore with UrhoSharp.
Now i try rotate model by users click on display.
It is my method`s when i try rotate models. But it work not correctly. Can you help me?

` public bool RotateObjectToNewPoints(Vector3 pose)
{
var radius = (float)Math.Sqrt((Math.Pow((pose.X - oldValue.X), 2) + Math.Pow((pose.Y - oldValue.Y), 2) + Math.Pow((pose.Z - oldValue.Z), 2)));
var nullDegreeX = new Vector3(oldValue.X + radius, oldValue.Y, oldValue.Z);
var lenghtSector = (float)Math.Sqrt(Math.Pow((pose.X - nullDegreeX.X), 2) + Math.Pow((pose.Y - nullDegreeX.Y), 2) + Math.Pow((pose.Z - nullDegreeX.Z), 2));//AB
var cosL = (Math.Pow(lenghtSector, 2) + Math.Pow(radius, 2) - Math.Pow(radius, 2)) / (2 * lenghtSector * radius);
float degree = (float)RadianToDegree(Math.Acos(cosL));
if (pose.Y <= oldValue.Y)
{
degree = 360 - degree;
}
System.Console.WriteLine(degree);
oldValue = pose;
mutantNode.Rotation = new Quaternion(0, 360-oldRotation, 0);

        if (double.IsNaN(degree))
        {
            return false;
        }

        //float VectorLength(Vector3 v) => (float)Math.Sqrt(v.X * v.X + v.Y * v.Y + v.Z * v.Z);

        //float DotProduct(Vector3 v1, Vector3 v2) => v1.X * v2.X + v1.Y * v2.Y + v1.Z * v2.Z;

        //Vector3 VectorSubtract(Vector3 v1, Vector3 v2) => new Vector3(v1.X - v2.X, v1.Y - v2.Y, v1.Z - v2.Z);

        //nullDegreeX = VectorSubtract(nullDegreeX, oldValue);
        //pose = VectorSubtract(pose, oldValue);


        //float VectorsToAngle(Vector3 v1, Vector3 v2) => (float)Math.Acos(DotProduct(v1, v2) / VectorLength(v1) / VectorLength(v2));

        //double degree = VectorsToAngle(nullDegreeX, pose);
        //degree = RadianToDegree(degree);

        mutantNode.Rotation = new Quaternion(0, (int)degree, 0);
        oldRotation = (float)degree;
        return true;
    }
    private double RadianToDegree(double angle)
    {
        return angle * (180.0 / Math.PI);
    }
    async void OnTouchEnd(TouchEndEventArgs e)
    {
        if (scaling) return;
        var hitTest = currentFrame.HitTest(e.X, e.Y);
        if (hitTest != null && hitTest.Count > 0)
        {
            var hitPos = hitTest[0].HitPose;
           bool result = RotateObjectToNewPoints(new Vector3(hitPos.Tx(), hitPos.Ty(), hitPos.Tz()));
            if (result)
            {
                musicSource.Stop();
                ani.Stop("Models/Wolf_seat_.ani");
                ani.Play("Models/Wolf_Walk_cycle_.ani", 0, true, 1f);

                var gotoPoint = new MoveTo(3, new Vector3(hitPos.Tx(), hitPos.Ty(), -hitPos.Tz()));
                await mutantNode.RunActionsAsync(gotoPoint);
                //mutantNode.Position = new Vector3(hitPos.X, hitPos.Y, -hitPos.Tz());
                if (mutantNode.Position == new Vector3(hitPos.Tx(), hitPos.Ty(), -hitPos.Tz()))
                {
                    ani.Stop("Models/Wolf_Walk_cycle_.ani");
                    ani.Play("Models/Wolf_Idle_.ani", 0, true, 1f);
                }
            }
        }
    }`

Best Answer

  • angrydogangrydog ✭✭
    Accepted Answer

    public bool RotateObjectToNewPoints(Vector3 pose)
    {
    var radius = (float)Math.Sqrt((Math.Pow((oldValue.X - pose.X), 2) + Math.Pow((oldValue.Z - pose.Z), 2)));
    var nullDegreeX = new Vector3(oldValue.X + radius, oldValue.Y, oldValue.Z);
    var lenghtSector = (float)Math.Sqrt(Math.Pow((nullDegreeX.X - pose.X), 2) + Math.Pow((nullDegreeX.Z - pose.Z), 2));//AB

            //true
            var cosL = (Math.Pow(radius, 2) + Math.Pow(radius, 2) - Math.Pow(lenghtSector, 2)) / (2 * radius * radius);
            float degree = (float)RadianToDegree(Math.Acos(cosL));
    
            if (pose.Z < oldValue.Z)
            {
                degree = 360 - degree;
    
            }
    
            System.Console.WriteLine(degree);
            oldValue = pose;
            //mutantNode.Rotation = new Quaternion(0, -oldRotation, 0);
    
            if (double.IsNaN(degree))
            {
                return false;
            }
    
            mutantNode.Rotation = new Quaternion(0, degree, 0);
            oldRotation = (float)degree;
            return true;
        }
    

    Understood, the problem was that in UrhoSharp the height axis Y.

Answers

  • angrydogangrydog Member ✭✭
    Accepted Answer

    public bool RotateObjectToNewPoints(Vector3 pose)
    {
    var radius = (float)Math.Sqrt((Math.Pow((oldValue.X - pose.X), 2) + Math.Pow((oldValue.Z - pose.Z), 2)));
    var nullDegreeX = new Vector3(oldValue.X + radius, oldValue.Y, oldValue.Z);
    var lenghtSector = (float)Math.Sqrt(Math.Pow((nullDegreeX.X - pose.X), 2) + Math.Pow((nullDegreeX.Z - pose.Z), 2));//AB

            //true
            var cosL = (Math.Pow(radius, 2) + Math.Pow(radius, 2) - Math.Pow(lenghtSector, 2)) / (2 * radius * radius);
            float degree = (float)RadianToDegree(Math.Acos(cosL));
    
            if (pose.Z < oldValue.Z)
            {
                degree = 360 - degree;
    
            }
    
            System.Console.WriteLine(degree);
            oldValue = pose;
            //mutantNode.Rotation = new Quaternion(0, -oldRotation, 0);
    
            if (double.IsNaN(degree))
            {
                return false;
            }
    
            mutantNode.Rotation = new Quaternion(0, degree, 0);
            oldRotation = (float)degree;
            return true;
        }
    

    Understood, the problem was that in UrhoSharp the height axis Y.

  • jezhjezh Member, Xamarin Team Xamurai

    Congrats, and thanks for sharing the solution.

Sign In or Register to comment.