void Start()
        {
            Matrix4x4d T, R;

            double spacing = 0.125;
            double radius  = spacing;
            double mass    = 1.0;

            System.Random rnd = new System.Random(0);

            Vector3 min    = new Vector3(-1, -1, -1);
            Vector3 max    = new Vector3(1, 1, 1);
            Box3d   bounds = new Box3d(min, max);

            ParticlesFromBounds source = new ParticlesFromBounds(spacing, bounds);

            T = Matrix4x4d.Translate(new Vector3d(0.0, 4.0, 0.0));
            R = Matrix4x4d.Rotate(new Vector3d(0.0, 0.0, 25.0));

            RidgidBody3d body = new RidgidBody3d(source, radius, mass, T * R);

            body.Dampning = 1.0;
            body.RandomizePositions(rnd, radius * 0.01);

            Solver = new Solver3d();
            Solver.AddBody(body);
            Solver.AddForce(new GravitationalForce3d());
            Solver.AddCollision(new PlanarCollision3d(Vector3.up, 0));
            Solver.SolverIterations    = 2;
            Solver.CollisionIterations = 2;
            Solver.SleepThreshold      = 1;

            CreateSpheres();
        }
Exemple #2
0
        protected override void SetWorldToCameraMatrix()
        {
            double co = Math.Cos(m_position.x0);             // x0 = longitude
            double so = Math.Sin(m_position.x0);
            double ca = Math.Cos(m_position.y0);             // y0 = latitude
            double sa = Math.Sin(m_position.y0);

            Vector3d po = new Vector3d(co * ca, so * ca, sa) * Radius;
            Vector3d px = new Vector3d(-so, co, 0.0);
            Vector3d py = new Vector3d(-co * sa, -so * sa, ca);
            Vector3d pz = new Vector3d(co * ca, so * ca, sa);

            double ct = Math.Cos(m_position.theta);
            double st = Math.Sin(m_position.theta);
            double cp = Math.Cos(m_position.phi);
            double sp = Math.Sin(m_position.phi);

            Vector3d cx = px * cp + py * sp;
            Vector3d cy = (px * -1.0) * sp * ct + py * cp * ct + pz * st;
            Vector3d cz = px * sp * st - py * cp * st + pz * ct;

            Vector3d worldPos = po + cz * m_position.distance;

            if (worldPos.Magnitude < Radius + 10.0 + GroundHeight)
            {
                double inv = (Radius + 10.0 + GroundHeight) / worldPos.Magnitude;
                worldPos = worldPos * inv;
            }

            Matrix4x4d view = new Matrix4x4d(cx.x, cx.y, cx.z, 0.0,
                                             cy.x, cy.y, cy.z, 0.0,
                                             cz.x, cz.y, cz.z, 0.0,
                                             0.0, 0.0, 0.0, 1.0);

            Matrix4x4d worldToCamera = view * Matrix4x4d.Translate(worldPos * -1.0);

            //Flip first row to match Unitys winding order.
            worldToCamera[0, 0] *= -1.0;
            worldToCamera[0, 1] *= -1.0;
            worldToCamera[0, 2] *= -1.0;
            worldToCamera[0, 3] *= -1.0;

            WorldToCamera = worldToCamera;
            CameraToWorld = worldToCamera.Inverse;

            WorldCameraPos = worldPos;

            GetComponent <Camera>().worldToCameraMatrix = MathConverter.ToMatrix4x4(WorldToCamera);
            GetComponent <Camera>().transform.position  = MathConverter.ToVector3(worldPos);
        }
Exemple #3
0
        protected override void SetWorldToCameraMatrix()
        {
            double co = Math.Cos(m_position.x0);             // x0 = longitude
            double so = Math.Sin(m_position.x0);
            double ca = Math.Cos(m_position.y0);             // y0 = latitude
            double sa = Math.Sin(m_position.y0);

            Vector3d2 po = new Vector3d2(co * ca, so * ca, sa) * m_radius;
            Vector3d2 px = new Vector3d2(-so, co, 0.0);
            Vector3d2 py = new Vector3d2(-co * sa, -so * sa, ca);
            Vector3d2 pz = new Vector3d2(co * ca, so * ca, sa);

            double ct = Math.Cos(m_position.theta);
            double st = Math.Sin(m_position.theta);
            double cp = Math.Cos(m_position.phi);
            double sp = Math.Sin(m_position.phi);

            Vector3d2 cx = px * cp + py * sp;
            Vector3d2 cy = (px * -1.0) * sp * ct + py * cp * ct + pz * st;
            Vector3d2 cz = px * sp * st - py * cp * st + pz * ct;

            m_worldPos = po + cz * m_position.distance;

            if (m_worldPos.Magnitude() < m_radius + 10.0 + m_groundHeight)
            {
                m_worldPos = m_worldPos.Normalized(m_radius + 10.0 + m_groundHeight);
            }

            Matrix4x4d view = new Matrix4x4d(cx.x, cx.y, cx.z, 0.0,
                                             cy.x, cy.y, cy.z, 0.0,
                                             cz.x, cz.y, cz.z, 0.0,
                                             0.0, 0.0, 0.0, 1.0);

            m_worldToCameraMatrix = view * Matrix4x4d.Translate(m_worldPos * -1.0);

            //Flip first row to match Unitys winding order.
            m_worldToCameraMatrix.m[0, 0] *= -1.0;
            m_worldToCameraMatrix.m[0, 1] *= -1.0;
            m_worldToCameraMatrix.m[0, 2] *= -1.0;
            m_worldToCameraMatrix.m[0, 3] *= -1.0;

            m_cameraToWorldMatrix = m_worldToCameraMatrix.Inverse();

            camera.worldToCameraMatrix = m_worldToCameraMatrix.ToMatrix4x4();
            camera.transform.position  = m_worldPos.ToVector3();
        }
Exemple #4
0
        /// <summary>
        /// Computes the world to camera matrix using double precision
        /// and applies it to the camera.
        /// </summary>
        protected virtual void SetWorldToCameraMatrix()
        {
            Vector3d po = new Vector3d(m_position.x0, m_position.y0, 0.0);
            Vector3d px = new Vector3d(1.0, 0.0, 0.0);
            Vector3d py = new Vector3d(0.0, 1.0, 0.0);
            Vector3d pz = new Vector3d(0.0, 0.0, 1.0);

            double ct = Math.Cos(m_position.theta);
            double st = Math.Sin(m_position.theta);
            double cp = Math.Cos(m_position.phi);
            double sp = Math.Sin(m_position.phi);

            Vector3d cx = px * cp + py * sp;
            Vector3d cy = (px * -1.0) * sp * ct + py * cp * ct + pz * st;
            Vector3d cz = px * sp * st - py * cp * st + pz * ct;

            Vector3d worldPos = po + cz * m_position.distance;

            if (worldPos.z < GroundHeight + 10.0)
            {
                worldPos.z = GroundHeight + 10.0;
            }

            Matrix4x4d view = new Matrix4x4d(cx.x, cx.y, cx.z, 0.0,
                                             cy.x, cy.y, cy.z, 0.0,
                                             cz.x, cz.y, cz.z, 0.0,
                                             0.0, 0.0, 0.0, 1.0);

            Matrix4x4d worldToCamera = view * Matrix4x4d.Translate(worldPos * -1.0);

            worldToCamera[0, 0] *= -1.0;
            worldToCamera[0, 1] *= -1.0;
            worldToCamera[0, 2] *= -1.0;
            worldToCamera[0, 3] *= -1.0;

            WorldToCamera = worldToCamera;
            CameraToWorld = worldToCamera.Inverse;

            WorldCameraPos = worldPos;

            Camera camera = GetComponent <Camera>();

            camera.worldToCameraMatrix = MathConverter.ToMatrix4x4(WorldToCamera);
            camera.transform.position  = MathConverter.ToVector3(worldPos);
        }
Exemple #5
0
        void Start()
        {
            TrianglesFromGrid source = new TrianglesFromGrid(radius, width, depth);

            Matrix4x4d T  = Matrix4x4d.Translate(new Vector3d(0.0, height, 0.0));
            Matrix4x4d R  = Matrix4x4d.Rotate(new Vector3d(0.0, 0.0, 0.0));
            Matrix4x4d RT = T * R;

            Body          = new ClothBody3d(source, radius, mass, stretchStiffness, bendStiffness, RT);
            Body.Dampning = 1.0;

            Vector3 min = new Vector3((float)(-width / 2 - 0.1),
                                      (float)(height - 0.1),
                                      (float)(-depth / 2 - 0.1));
            Vector3 max = new Vector3((float)(width / 2 + 0.1),
                                      (float)(height + 0.1),
                                      (float)(-depth / 2 + 0.1));

            StaticBounds = new Box3d(min, max);

            Body.MarkAsStatic(StaticBounds);

            Solver = new Solver3d();
            Solver.AddBody(Body);
            Solver.AddForce(new GravitationalForce3d());
            //Solver.AddCollision(new PlanarCollision3d(Vector3.up, 0));
            Solver.SolverIterations    = 2;
            Solver.CollisionIterations = 2;
            Solver.SleepThreshold      = 1;
            //setting compute shader of solver
            Solver.ApplyExternalForcesShader  = ApplyExternalForces;
            Solver.EstimatePositionsShader    = EstimatePositions;
            Solver.ResolveCollisionsShader    = ResolveCollisions;
            Solver.ConstraintPositionsShader  = ConstraintPositions;
            Solver.UpdateVelocitiesShader     = UpdateVelocities;
            Solver.ConstraintVelocitiesShader = ConstraintVelocities;
            Solver.UpdatePositionsShader      = UpdatePositions;
            Solver.GPUmode = GPUmode;
            Solver.init();

            CreateSpheres();
        }
Exemple #6
0
        protected override void SetWorldToCameraMatrix()
        {
            // NOTE : co - x; so - y; ca - z; sa - w;
            var oa = CalculatelongitudeLatitudeVector(position.X, position.Y);

            var po = new Vector3d(oa.x * oa.z, oa.y * oa.z, oa.w) * Radius;
            var px = new Vector3d(-oa.y, oa.x, 0.0);
            var py = new Vector3d(-oa.x * oa.w, -oa.y * oa.w, oa.z);
            var pz = new Vector3d(oa.x * oa.z, oa.y * oa.z, oa.w);

            // NOTE : ct - x; st - y; cp - z; sp - w;
            var tp = CalculatelongitudeLatitudeVector(position.Theta, position.Phi);

            Vector3d cx = px * tp.z + py * tp.w;
            Vector3d cy = (px * -1.0) * tp.w * tp.x + py * tp.z * tp.x + pz * tp.y;
            Vector3d cz = px * tp.w * tp.y - py * tp.z * tp.y + pz * tp.x;

            worldPosition = po + cz * position.Distance;

            if (worldPosition.Magnitude() < Radius + 10.0 + GroundHeight)
            {
                worldPosition = worldPosition.Normalized(Radius + 10.0 + GroundHeight);
            }

            worldPosition = worldPosition + Origin;

            Matrix4x4d view = new Matrix4x4d(cx.x, cx.y, cx.z, 0.0, cy.x, cy.y, cy.z, 0.0, cz.x, cz.y, cz.z, 0.0, 0.0, 0.0, 0.0, 1.0);

            WorldToCameraMatrix = view * Matrix4x4d.Translate(worldPosition * -1.0);

            //Flip first row to match Unity's winding order.
            WorldToCameraMatrix.m[0, 0] *= -1.0;
            WorldToCameraMatrix.m[0, 1] *= -1.0;
            WorldToCameraMatrix.m[0, 2] *= -1.0;
            WorldToCameraMatrix.m[0, 3] *= -1.0;

            CameraToWorldMatrix = WorldToCameraMatrix.Inverse();

            CameraComponent.worldToCameraMatrix = WorldToCameraMatrix.ToMatrix4x4();
            CameraComponent.transform.position  = worldPosition.ToVector3();
        }
Exemple #7
0
        void Start()
        {
            Matrix4x4d T  = Matrix4x4d.Translate(new Vector3d(0.0, 6.0, 0.0));
            Matrix4x4d R  = Matrix4x4d.Rotate(new Vector3d(0.0, 0.0, 0.0));
            Matrix4x4d TR = T * R;

            double radius    = 0.25;
            double stiffness = 0.2;
            double mass      = 1.0;

            System.Random rnd = new System.Random(0);

            Vector3d min    = new Vector3d(-5.0, -1.0, -0.5);
            Vector3d max    = new Vector3d(5.0, 1.0, 0.5);
            Box3d    bounds = new Box3d(min, max);
            TetrahedronsFromBounds source = new TetrahedronsFromBounds(radius, bounds);

            Body          = new DeformableBody3d(source, radius, mass, stiffness, TR);
            Body.Dampning = 1.0;
            Body.RandomizePositions(rnd, radius * 0.01);
            Body.RandomizeConstraintOrder(rnd);

            Vector3d smin = new Vector3d(min.x, min.y + 6.0, min.z - 0.1);
            Vector3d smax = new Vector3d(min.x + 0.5, max.y + 6.0, max.z + 0.1);

            StaticBounds = new Box3d(smin, smax);
            Body.MarkAsStatic(StaticBounds);

            Solver = new Solver3d();
            Solver.AddBody(Body);
            Solver.AddForce(new GravitationalForce3d());
            Solver.AddCollision(new PlanarCollision3d(Vector3d.UnitY, 0));
            Solver.SolverIterations    = 2;
            Solver.CollisionIterations = 2;
            Solver.SleepThreshold      = 1;

            RenderEvent.AddRenderEvent(Camera.main, DrawOutline);

            CreateSpheres();
        }
        /*
         * Computes the world to camera matrix using double precision
         * and applies it to the camera.
         */
        protected virtual void SetWorldToCameraMatrix()
        {
            Vector3d2 po = new Vector3d2(m_position.x0, m_position.y0, 0.0);
            Vector3d2 px = new Vector3d2(1.0, 0.0, 0.0);
            Vector3d2 py = new Vector3d2(0.0, 1.0, 0.0);
            Vector3d2 pz = new Vector3d2(0.0, 0.0, 1.0);

            double ct = Math.Cos(m_position.theta);
            double st = Math.Sin(m_position.theta);
            double cp = Math.Cos(m_position.phi);
            double sp = Math.Sin(m_position.phi);

            Vector3d2 cx = px * cp + py * sp;
            Vector3d2 cy = (px * -1.0) * sp * ct + py * cp * ct + pz * st;
            Vector3d2 cz = px * sp * st - py * cp * st + pz * ct;

            m_worldPos = po + cz * m_position.distance;

            if (m_worldPos.z < m_groundHeight + 10.0)
            {
                m_worldPos.z = m_groundHeight + 10.0;
            }

            Matrix4x4d view = new Matrix4x4d(cx.x, cx.y, cx.z, 0.0,
                                             cy.x, cy.y, cy.z, 0.0,
                                             cz.x, cz.y, cz.z, 0.0,
                                             0.0, 0.0, 0.0, 1.0);

            m_worldToCameraMatrix = view * Matrix4x4d.Translate(m_worldPos * -1.0);

            m_worldToCameraMatrix.m[0, 0] *= -1.0;
            m_worldToCameraMatrix.m[0, 1] *= -1.0;
            m_worldToCameraMatrix.m[0, 2] *= -1.0;
            m_worldToCameraMatrix.m[0, 3] *= -1.0;

            m_cameraToWorldMatrix = m_worldToCameraMatrix.Inverse();

            camera.worldToCameraMatrix = m_worldToCameraMatrix.ToMatrix4x4();
            camera.transform.position  = m_worldPos.ToVector3();
        }
Exemple #9
0
        void Start()
        {
            double stretchStiffness = 0.25;
            double bendStiffness    = 0.5;
            double mass             = 1.0;
            double radius           = 0.125;

            double width  = 5.0;
            double height = 4.0;
            double depth  = 5.0;

            TrianglesFromGrid source = new TrianglesFromGrid(radius, width, depth);

            Matrix4x4d T  = Matrix4x4d.Translate(new Vector3d(0.0, height, 0.0));
            Matrix4x4d R  = Matrix4x4d.Rotate(new Vector3d(0.0, 0.0, 0.0));
            Matrix4x4d RT = T * R;

            Body          = new ClothBody3d(source, radius, mass, stretchStiffness, bendStiffness, RT);
            Body.Dampning = 1.0;

            Vector3d min = new Vector3d(-width / 2 - 0.1, height - 0.1, -depth / 2 - 0.1);
            Vector3d max = new Vector3d(width / 2 + 0.1, height + 0.1, -depth / 2 + 0.1);

            StaticBounds = new Box3d(min, max);

            Body.MarkAsStatic(StaticBounds);

            Solver = new Solver3d();
            Solver.AddBody(Body);
            Solver.AddForce(new GravitationalForce3d());
            Solver.AddCollision(new PlanarCollision3d(Vector3d.UnitY, 0));
            Solver.SolverIterations    = 2;
            Solver.CollisionIterations = 2;
            Solver.SleepThreshold      = 1;

            RenderEvent.AddRenderEvent(Camera.main, DrawOutline);

            CreateSpheres();
        }
Exemple #10
0
        protected virtual void SetWorldToCameraMatrix()
        {
            var po = GetLookAtPosition();
            var px = Vector3d.right;
            var py = Vector3d.up;
            var pz = Vector3d.forward;

            // NOTE : ct - x; st - y; cp - z; sp - w;
            var tp = CalculatelongitudeLatitudeVector(position.Theta, position.Phi);

            Vector3d cx = px * tp.z + py * tp.w;
            Vector3d cy = (px * -1.0) * tp.w * tp.x + py * tp.z * tp.x + pz * tp.y;
            Vector3d cz = px * tp.w * tp.y - py * tp.z * tp.y + pz * tp.x;

            worldPosition = po + cz * position.Distance;

            if (worldPosition.z < GroundHeight + 10.0)
            {
                worldPosition.z = GroundHeight + 10.0;
            }

            Matrix4x4d view = new Matrix4x4d(cx.x, cx.y, cx.z, 0.0, cy.x, cy.y, cy.z, 0.0, cz.x, cz.y, cz.z, 0.0, 0.0, 0.0, 0.0, 1.0);

            WorldToCameraMatrix = view * Matrix4x4d.Translate(worldPosition * -1.0);

            //Flip first row to match Unity's winding order.
            WorldToCameraMatrix.m[0, 0] *= -1.0;
            WorldToCameraMatrix.m[0, 1] *= -1.0;
            WorldToCameraMatrix.m[0, 2] *= -1.0;
            WorldToCameraMatrix.m[0, 3] *= -1.0;

            CameraToWorldMatrix = WorldToCameraMatrix.Inverse();

            CameraComponent.worldToCameraMatrix = WorldToCameraMatrix.ToMatrix4x4();
            CameraComponent.transform.position  = worldPosition.ToVector3();
        }
Exemple #11
0
 /// <summary>
 /// This differential gives a linear approximation of the deformation around a given point, represented with a matrix.
 /// More precisely, if 'p' is near localPoint, then the deformed point corresponding to 'p' can be approximated with:
 /// <example>
 /// <code>
 /// LocalToDeformedDifferential(localPoint) * (p - localPoint);
 /// </code>
 /// </example>
 /// </summary>
 /// <param name="localPoint"></param>
 /// <param name="clamp">Clamp values to space?</param>
 /// <returns>Returns the differential of the deformation function at the given local point.</returns>
 public virtual Matrix4x4d LocalToDeformedDifferential(Vector3d localPoint, bool clamp = false)
 {
     return(Matrix4x4d.Translate(new Vector3d(localPoint.x, localPoint.y, 0.0)));
 }
Exemple #12
0
 /// <summary>
 /// This reference frame is such that its xy plane is the tangent plane, at deformedPoint to the deformed surface corresponding to the local plane z = 0.
 /// Note that this orthonormal reference frame doesn't give the differential of the inverse deformation funtion, which in general is not an orthonormal transformation.
 /// If 'p' is a deformed point, then <code>DeformedToLocalFrame(deformedPoint) * p</code> gives the coordinates of 'p' in the orthonormal reference frame defined above.
 /// </summary>
 /// <param name="deformedPoint">A point in the deformed (destination) space.</param>
 /// <returns>Returns an orthonormal reference frame of the tangent space at the given deformed point.</returns>
 public virtual Matrix4x4d DeformedToTangentFrame(Vector3d deformedPoint)
 {
     return(Matrix4x4d.Translate(new Vector3d(-deformedPoint.x, -deformedPoint.y, 0.0)));
 }