static void rotateAroundAxis(DoubleTransform dtr, DVec3 localAxis, double angle)
    {
        var axis = (dtr.localRotation * localAxis).normalized;
        var rot  = DQuat.fromAxisAngle(axis, angle).normalized;

        dtr.localRotation = (rot * dtr.localRotation).normalized;
    }
        protected bool _Walk(ref double current, ref DVec3 pos)
        {
            var sd   = param.sd;
            var rand = BoxMuller.Gaussian() * sd;

            DVec3 posNext;

            posNext.x = pos.x + rand.x;
            posNext.y = pos.y + rand.y;
            rand      = BoxMuller.Gaussian() * sd;
            posNext.z = pos.z + rand.x;

            posNext.x -= _size.x * Math.Floor((posNext.x - _min.x) / _size.x);
            posNext.y -= _size.y * Math.Floor((posNext.y - _min.y) / _size.y);
            posNext.z -= _size.z * Math.Floor((posNext.z - _min.z) / _size.z);

            var next   = Height(posNext);
            var change = (float)(new System.Random()).NextDouble() < (next / current);

            if (change)
            {
                pos     = posNext;
                current = next;
            }

            return(change);
        }
    public Vector3 getLocalPos(DVec3 pos, int level)
    {
        var diff        = pos - origin;
        var scaleFactor = getScaleFactor(level);
        var localDPos   = diff / scaleFactor;

        return(localDPos.toVector3());
    }
        protected (double, DVec3) Init(Bounds bounds)
        {
            DVec3 pos     = bounds.randomInside();
            var   current = Height(pos);

            //Walk(ref current, ref pos);

            return(current, pos);
        }
Esempio n. 5
0
    public override bool Equals(object other)
    {
        if (!(other is DVec3))
        {
            return(false);
        }
        DVec3 v = (DVec3)other;

        return(v.x.Equals(x) && v.y.Equals(y) && v.z.Equals(z));
    }
 protected void Walk(ref double current, ref DVec3 pos)
 {
     for (var i = 0; i < param.walkMax; ++i)
     {
         if (_Walk(ref current, ref pos))
         {
             break;
         }
     }
 }
Esempio n. 7
0
    public static DQuat fromAxisAngleRad(DVec3 axis, double radAngle)
    {
        axis.normalize();

        double halfAngle = radAngle * 0.5;
        double sin       = Math.Sin(halfAngle);
        double cos       = Math.Cos(halfAngle);

        return(new DQuat(axis.x * sin, axis.y * sin, axis.z * sin, cos));
    }
Esempio n. 8
0
    // Use this for initialization
    void Start()
    {
        Debug.Log("InitScript::Start");
        Ellipsoid e = new Ellipsoid();

//        double sx = 0, sy = 0, sz = 0;
        foreach (GnomeObject creature in Creatures)
        {
            DVec3 v = e.ToVector(creature.lon * 3.14 / 180.0, creature.lat * 3.14 / 180.0, 0);
            Debug.Log(creature.lon + " " + creature.lat + " " + v.x + " " + v.y + " " + v.z);
//           sx += v.x; sy += v.y; sz += v.z;
        }
//       int n = Creatures.Length;
//       Debug.Log("Centre: " + (sx / n) + " " + (sy / n) + " " + (sz / n));
    }
Esempio n. 9
0
    public void updatePositions(double deltaT)
    {
        var a = getAcceleration();

        lastAcceleration = a;
        lastLinearVel    = velocity.magnitude;
        lastLinearAccel  = a.magnitude;
        lastLinearForce  = accumulatedForce.magnitude;

        var dtr = GetComponent <DoubleTransform>();

        var nextPos      = dtr.position + a * deltaT * deltaT * 0.5 + velocity * deltaT;
        var nextVelocity = velocity + a * deltaT;

        velocity     = nextVelocity;
        dtr.position = nextPos;
    }
Esempio n. 10
0
    public static DVec3 computeGravity(DoublePhysicsBody src, DoublePhysicsBody dst)
    {
        var srcComp = src.GetComponent <DoubleTransform>();
        var dstComp = dst.GetComponent <DoubleTransform>();

        var srcPos = srcComp.position;
        var dstPos = dstComp.position;

        var diff = dstPos - srcPos;
        var r2   = DVec3.dot(diff, diff);

        double fMagnitude = gravitationalConstant * src.mass * dst.mass / r2;

        var result = diff.normalized * fMagnitude;

        return(result);
    }
Esempio n. 11
0
 public bool Equals(DVec3 other)
 {
     return(X == other.X && Y == other.Y && Z == other.Z);
 }
Esempio n. 12
0
 public double distanceTo(DVec3 arg)
 {
     return(distance(this, arg));
 }
Esempio n. 13
0
    void updateObject(SpaceBody obj)
    {
        var diff = obj.position - originPosition;
        var dist = diff.magnitude;

        int numLayers = layerMasks.Length;

        if (numLayers != obj.spawnedObjects.Length)
        {
            obj.destroyObjects();
            obj.spawnedObjects = new GameObject[numLayers];
        }

        var radius = 1.0;

        if (obj.prefab)
        {
            radius = obj.radius;
        }
        var minDist = dist - radius;
        var maxDist = dist - radius;

        var absDiff = DVec3.abs(diff);
        var absMin  = absDiff - new DVec3(radius, radius, radius);
        var absMax  = absDiff + new DVec3(radius, radius, radius);

        double currentScale = 1.0;
        double farClip      = scaleFactor;
        double nearClip     = 0.0;

        for (int curIndex = 0; curIndex < layerMasks.Length; curIndex++)
        {
            var curLayer     = layerIndexes[curIndex];
            var curLayerMask = layerMasks[curIndex];
            var clipped      = ((absMax.x < nearClip) && (absMax.y < nearClip) && (absMax.z < nearClip));

            if (curIndex < (layerMasks.Length - 1))
            {
                clipped = clipped || (absMin.x > farClip) || (absMin.y > farClip) || (absMin.z > farClip);
            }

            if (obj.hidden)
            {
                clipped = true;
            }

            if (clipped)
            {
                if (obj.spawnedObjects[curIndex])
                {
                    Destroy(obj.spawnedObjects[curIndex]);
                    obj.spawnedObjects[curIndex] = null;
                }
            }
            else
            {
                if (obj.prefab)
                {
                    var    prefab   = obj.prefab;
                    double scale    = obj.radius / (prefab.prefabRadius * currentScale);
                    var    localPos = diff / currentScale;

                    if (!obj.spawnedObjects[curIndex])
                    {
                        var displayObj = Instantiate <GameObject>(obj.prefab.gameObject);
                        moveToLayer(displayObj, curLayer);
                        obj.spawnedObjects[curIndex] = displayObj;

                        var lights = displayObj.GetComponentsInChildren <Light>();
                        for (int i = 0; i < lights.Length; i++)
                        {
                            var curLight = lights[i];
                            if (!curLight)
                            {
                                continue;
                            }
                            curLight.cullingMask = curLayerMask;
                            curLight.range      *= (float)scale;
                        }

                        displayObj.name = string.Format("{0}: layer {1}", prefab.gameObject.name, curIndex);
                    }

                    var displayObject = obj.spawnedObjects[curIndex];
                    var spawnedData   = displayObject.GetComponent <SpawnedSpaceObjectData>();
                    if (!spawnedData)
                    {
                        spawnedData = displayObject.AddComponent <SpawnedSpaceObjectData>();
                    }
                    spawnedData.modelScale  = scale;
                    spawnedData.layerIndex  = curIndex;
                    spawnedData.scaleFactor = currentScale;
                    spawnedData.radius      = obj.radius;

                    displayObject.transform.localScale = new Vector3((float)scale, (float)scale, (float)scale);
                    displayObject.transform.position   = new Vector3((float)localPos.x, (float)localPos.y, (float)localPos.z);
                }
            }

            currentScale *= scaleFactor;
            nearClip      = farClip;
            farClip      *= scaleFactor;
        }
    }
Esempio n. 14
0
 public static DVec3 scale(DVec3 arg, double factor)
 {
     return(new DVec3(arg.x * factor, arg.y * factor, arg.z * factor));
 }
Esempio n. 15
0
 public static DVec3 subtract(DVec3 a, DVec3 b)
 {
     return(new DVec3(a.x - b.x, a.y - b.y, a.z - b.z));
 }
Esempio n. 16
0
 public static DVec3 add(DVec3 a, DVec3 b)
 {
     return(new DVec3(a.x + b.x, a.y + b.y, a.z + b.z));
 }
Esempio n. 17
0
 public static double dot(DVec3 a, DVec3 b)
 {
     return(a.x * b.x + a.y * b.y + a.z * b.z);
 }
Esempio n. 18
0
 public void resetForces()
 {
     accumulatedForce = DVec3.zero;
 }
Esempio n. 19
0
 static public DVec3 abs(DVec3 arg)
 {
     return(new DVec3(System.Math.Abs(arg.x), System.Math.Abs(arg.y), System.Math.Abs(arg.z)));
 }
Esempio n. 20
0
 public void addForce(DVec3 force)
 {
     accumulatedForce += force;
 }
Esempio n. 21
0
 public static DVec3 scale(DVec3 a, DVec3 b)
 {
     return(new DVec3(a.x * b.x, a.y * b.y, a.z * b.z));
 }
Esempio n. 22
0
 public static DQuat fromAxisAngle(DVec3 axis, double angle)
 {
     return(fromAxisAngleRad(axis, angle * Math.PI / 180.0));
 }
Esempio n. 23
0
 public static double distance(DVec3 a, DVec3 b)
 {
     return((b - a).magnitude);
 }
Esempio n. 24
0
    void runTests()
    {
        sb.Length = 0;
        var d1 = new DVec3(1.0, 2.0, 3.0);
        var d2 = new DVec3(4.0, 5.0, 6.0);
        var f1 = d1.toVector3();
        var f2 = d2.toVector3();

        Quaternion[] q =
        {
            new Quaternion(1.0f, 0.0f, 0.0f, 0.0f),
            new Quaternion(0.0f, 1.0f, 0.0f, 0.0f),
            new Quaternion(0.0f, 0.0f, 1.0f, 0.0f),
            new Quaternion(0.0f, 0.0f, 0.0f, 1.0f)
        };

        DQuat[] dq =
        {
            new DQuat(1.0, 0.0, 0.0, 0.0),
            new DQuat(0.0, 1.0, 0.0, 0.0),
            new DQuat(0.0, 0.0, 1.0, 0.0),
            new DQuat(0.0, 0.0, 0.0, 1.0)
        };

        Vector3[] v =
        {
            Vector3.left,
            Vector3.right,
            Vector3.up,
            Vector3.down,
            Vector3.forward,
            Vector3.back
        };

        DVec3[] dv =
        {
            DVec3.left,
            DVec3.right,
            DVec3.up,
            DVec3.down,
            DVec3.forward,
            DVec3.back
        };

        for (int i = 0; i < q.Length; i++)
        {
            for (int j = 0; j < dq.Length; j++)
            {
                sb.AppendFormat("{0} * {1} = {2}\n", q[i], q[j], q[i] * q[j]);
                sb.AppendFormat("{0} * {1} = {2}\n\n", dq[i], dq[j], dq[i] * dq[j]);
            }
        }
        sb.AppendFormat("\n\n");

        for (int i = 0; i < q.Length; i++)
        {
            for (int j = 0; j < v.Length; j++)
            {
                sb.AppendFormat("{0} * {1} = {2}\n", q[i], v[j], q[i] * v[j]);
                sb.AppendFormat("{0} * {1} = {2}\n\n", dq[i], dv[j], dq[i] * dv[j]);
            }
        }

        /*
         * for(int i = 0; i < dq.Length; i++){
         *      for (int j = 0; j < dv.Length; j++){
         *      }
         * }
         */

        Debug.Log(sb);
    }
        protected double Height(DVec3 pos)
        {
            var h = SimplexNoise.noise(pos.x * _invNoiseSize, pos.y * _invNoiseSize, pos.z * _invNoiseSize);

            return((h < blackLevel) ? blackLevel : h);
        }
 void setPosition(DVec3 position)
 {
     localPosition = getParentRotation().conjugate *(position - getParentPosition());
 }
    public void updateProxies(DoubleTransformManager manager)
    {
        var origin = manager.origin;
        var dtr    = GetComponent <DoubleTransform>();

        if (!dtr)
        {
            Debug.LogWarningFormat("Double precision transform not present on: {0}", gameObject.name);
            return;
        }

        var position = dtr.position;
        var rotation = dtr.rotation;

        var diff = position - origin;
        var dist = diff.magnitude;

        int numLayers = manager.numLevels;

        setNumProxies(numLayers);

        var minDist = dist - radius;
        var maxDist = dist - radius;

        var absDiff = DVec3.abs(diff);
        var absMin  = absDiff - new DVec3(radius, radius, radius);
        var absMax  = absDiff + new DVec3(radius, radius, radius);

        int numLevels = manager.numLevels;

        for (int levelIndex = 0; levelIndex < numLevels; levelIndex++)
        {
            double nearClip     = manager.getNearClip(levelIndex);
            double farClip      = manager.getFarClip(levelIndex);
            double levelScale   = manager.getScaleFactor(levelIndex);
            var    curLayer     = manager.getLayerIndex(levelIndex);
            var    curLayerMask = manager.getLayerMask(levelIndex);

            var clipped = ((absMax.x < nearClip) && (absMax.y < nearClip) && (absMax.z < nearClip));

            if (levelIndex < (numLevels - 1))
            {
                clipped = clipped || (absMin.x > farClip) || (absMin.y > farClip) || (absMin.z > farClip);
            }

            if (!gameObject.activeInHierarchy)
            {
                clipped = true;
            }

            if (clipped)
            {
                if (proxies[levelIndex])
                {
                    Destroy(proxies[levelIndex]);
                    proxies[levelIndex] = null;
                }
            }
            else
            {
                if (proxyPrefab)
                {
                    var    prefab   = proxyPrefab;
                    double scale    = radius / (prefabRadius * levelScale);
                    var    localPos = diff / levelScale;
                    var    localRot = dtr.rotation;

                    if (!proxies[levelIndex])
                    {
                        var displayObj = Instantiate <GameObject>(prefab);
                        moveToLayer(displayObj, curLayer);
                        proxies[levelIndex] = displayObj;

                        var lights = displayObj.GetComponentsInChildren <Light>();
                        for (int i = 0; i < lights.Length; i++)
                        {
                            var curLight = lights[i];
                            if (!curLight)
                            {
                                continue;
                            }
                            curLight.cullingMask = curLayerMask;
                            curLight.range      *= (float)scale;
                        }

                        displayObj.name = string.Format("{0}: layer {1}", prefab.gameObject.name, levelIndex);
                        displayObj.transform.SetParent(manager.gameObject.transform, false);
                    }

                    var displayObject = proxies[levelIndex];

                    displayObject.transform.localScale    = new Vector3((float)scale, (float)scale, (float)scale);
                    displayObject.transform.localPosition = localPos.toVector3();
                    displayObject.transform.localRotation = localRot.normalized.toQuaternion();
                }
            }
        }
    }