Exemplo n.º 1
0
        /*--------------------------------------------------Creating terrain--------------------------------------------------*/
        //public void Create_Terrain(Guid guid, string heightmap, Vector3 position, string materialName, double restitution, double friction, double height)
        public AgX_Scene(Guid guid, List <Vector3> vertices, List <int> triangles, Vector3 position, string materialName)
        {
            this.guid = guid;

            //AgX:
            agx.Vec3Vector   agx_vertices = new agx.Vec3Vector();
            agx.UInt32Vector agx_indices  = new agx.UInt32Vector();
            for (int i = 0; i < vertices.Count; i++)
            {
                agx_vertices.Add(Operations.ToAgxVec3(vertices[i]));
            }
            for (int i = 0; i < triangles.Count; i++)
            {
                agx_indices.Add((uint)triangles[i]);
            }
            terrain = new agx.RigidBody();

            //uint optionsMask = (uint)agxCollide.Trimesh.TrimeshOptionsFlags.TERRAIN;
            var terrain_trimesh = new agxCollide.Trimesh(agx_vertices, agx_indices, "handmade terrain");//, optionsMask, height);

            var geometry = new agxCollide.Geometry();

            geometry.add(terrain_trimesh);
            geometry.setMaterial(new agx.Material(materialName));

            terrain.add(geometry);
            terrain.setMotionControl(agx.RigidBody.MotionControl.STATIC);

            //position.y -= height;
            terrain.setLocalPosition(Operations.ToAgxVec3(position));//move right and -height for global 0

            ///Adds terrain to simulation
            //simulation.add(terrain);
            Agx_Simulation.sim_Instance.add(terrain);
        }
Exemplo n.º 2
0
        public AgX_Frame(Guid guid, string shape, Vector3[] vertices, Vector2[] uvs, int[] triangles, double size, Vector3 pos, Quaternion rot, double mass, bool isStatic, string materialName)
        {
            this.guid = guid;

            this.shape        = shape;
            this.size         = size;
            this.materialName = materialName;

            var tri = new agxCollide.Trimesh(Operations.ToAgxVec3Vector(vertices), Operations.ToAgxIntVector(triangles), "stdFrame");
            ///Creates a geometry
            var dynamicRBGeometry = new agxCollide.Geometry();

            dynamicRBGeometry.add(tri);

            dynamicRBGeometry.setMaterial(new agx.Material(materialName));

            agx_Object = new agx.RigidBody();
            ///Adds selected geometry to the rigidbody
            agx_Object.add(dynamicRBGeometry);

            agx_Object.setLocalPosition(Operations.ToAgxVec3(pos));                ///AgX

            agx_Object.setLocalRotation(new agx.Quat(rot.x, rot.y, rot.z, rot.w)); ///AgX

            agx_Object.getMassProperties().setMass(mass);

            if (isStatic)
            {
                agx_Object.setMotionControl(agx.RigidBody.MotionControl.STATIC);
            }

            AddToAssembly();
        }
Exemplo n.º 3
0
        public AgX_Primitive(Guid guid, string shape, Vector3 pos, Quaternion rot, Vector3 size, double mass, string materialName)
        {
            this.guid         = guid;
            this.shape        = shape;
            this.size         = size;
            this.materialName = materialName;

            var dynamicRBGeometry = new agxCollide.Geometry();

            switch (shape)
            {
            case "Box": dynamicRBGeometry.add(new agxCollide.Box(Operations.ToAgxVec3(this.size))); break;

            case "Sphere": dynamicRBGeometry.add(new agxCollide.Sphere((this.size.x + this.size.y + this.size.z) / 3)); break;
            }

            dynamicRBGeometry.setMaterial(new agx.Material(materialName));

            agx_Object = new agx.RigidBody();
            agx_Object.add(dynamicRBGeometry);
            agx_Object.setLocalPosition(Operations.ToAgxVec3(pos));                ///AgX

            agx_Object.setLocalRotation(new agx.Quat(rot.x, rot.y, rot.z, rot.w)); ///AgX

            agx_Object.getMassProperties().setMass(mass);

            AddToSim();
        }
Exemplo n.º 4
0
        public AgX_Frame(Guid guid, string shape, Vector3[] vertices, Vector2[] uvs, int[] triangles, double size, Vector3 pos, Quaternion rot, double mass, bool isStatic, string materialName)
        {
            this.guid = guid;

            this.shape        = shape;
            this.size         = size;
            this.materialName = materialName;

            //scale by 2, to fit unity.

            /* Vector3[] tmp_verts = vertices;
             * for (int i = 0; i < tmp_verts.Length; i++)
             * {
             *   tmp_verts[i].x *= 2;
             *   tmp_verts[i].y *= 2;
             *   tmp_verts[i].z *= 2;
             * }*/

            var tri = new agxCollide.Trimesh(Operations.ToAgxVec3Vector(vertices), Operations.ToAgxIntVector(triangles), "stdFrame");
            ///Creates a geometry
            var dynamicRBGeometry = new agxCollide.Geometry();

            dynamicRBGeometry.add(tri);

            dynamicRBGeometry.setMaterial(new agx.Material(materialName));

            ///Creates the selected shape

            /*switch (shape)
             * {
             *  case "Box": dynamicRBGeometry.add(new agxCollide.Box(Operations.ToAgxVec3(size))); break;
             *  case "Sphere": dynamicRBGeometry.add(new agxCollide.Sphere((size.x + size.y + size.z) / 3)); break;
             * }*/

            agx_Object = new agx.RigidBody();
            ///Adds selected geometry to the rigidbody
            agx_Object.add(dynamicRBGeometry);

            agx_Object.setLocalPosition(Operations.ToAgxVec3(pos));///AgX

            //var y = new agx.EulerAngles(Operations.ToAgxVec3(rot));

            //UnityEngine.Debug.Log("x: " + y.x + ", y: " + y.y + ", z: " + y.z);

            agx_Object.setLocalRotation(new agx.Quat(rot.x, rot.y, rot.z, rot.w));///AgX

            //agx_Object.setLocalRotation(new agx.EulerAngles(Operations.ToAgxVec3(rot)));///AgX

            //UnityEngine.Debug.Log("x: " +agx_Object.getLocalPosition().x + ", y: " + agx_Object.getLocalPosition().y + ", z: " + agx_Object.getLocalPosition().z);

            agx_Object.getMassProperties().setMass(mass);

            if (isStatic)
            {
                agx_Object.setMotionControl(agx.RigidBody.MotionControl.STATIC);
            }

            AddToSim();
        }
Exemplo n.º 5
0
        /// <summary>
        /// Peek at a temporary native instance or the current (if initialized).
        /// </summary>
        /// <param name="callback">Callback with temporary or already initialized native instance. Callback signature ( nativeRb, isTemporary ).</param>
        /// <remarks>
        /// Always assume the native instance to be temporary. It's never safe to cache an instance to the native object.
        /// </remarks>
        public void PeekTemporaryNativeOrGetNative(Action <agx.RigidBody, bool> callback)
        {
            if (callback == null)
            {
                return;
            }

            if (m_rb != null)
            {
                callback(m_rb, false);
            }
            else
            {
                Shape[] shapes = GetComponentsInChildren <Shape>();

                using (agx.RigidBody rb = new agx.RigidBody()) {
                    foreach (Shape shape in shapes)
                    {
                        agxCollide.Shape nativeShape = shape.CreateTemporaryNative();
                        if (nativeShape != null)
                        {
                            agxCollide.Geometry geometry = new agxCollide.Geometry(nativeShape);

                            geometry.setEnable(shape.IsEnabled);

                            if (shape.Material != null)
                            {
                                geometry.setMaterial(shape.Material.CreateTemporaryNative());
                            }
                            rb.add(geometry, shape.GetNativeRigidBodyOffset(this));
                        }
                    }

                    // For center of mass position/rotation to be correct we have to
                    // synchronize the native transform given current game object transform.
                    SyncNativeTransform(rb);

                    callback(rb, true);

                    // Hitting "Update" (mass or inertia in the Inspector) several times
                    // will crash agx if we don't remove the geometries and shapes.
                    while (rb.getGeometries().Count > 0)
                    {
                        agxCollide.Geometry geometry = rb.getGeometries()[0].get();
                        if (geometry.getShapes().Count > 0)
                        {
                            geometry.remove(geometry.getShapes()[0].get());
                        }
                        rb.remove(geometry);
                    }
                }
            }
        }
Exemplo n.º 6
0
        public AgX_Primitive(Guid guid, string shape, Vector3 pos, Quaternion rot, Vector3 size, double mass, string materialName, bool isStatic, bool AddToRobot)
        {
            this.guid         = guid;
            this.shape        = shape;
            this.size         = size;
            this.materialName = materialName;

            var dynamicRBGeometry = new agxCollide.Geometry();

            switch (this.shape)
            {
            case "Box": dynamicRBGeometry.add(new agxCollide.Box(Operations.ToAgxVec3(this.size))); break;

            case "Sphere": dynamicRBGeometry.add(new agxCollide.Sphere((this.size.x + this.size.y + this.size.z) / 3)); break;

            default: dynamicRBGeometry.add(new agxCollide.Box(Operations.ToAgxVec3(this.size))); break;
            }

            dynamicRBGeometry.setMaterial(new agx.Material(this.materialName));

            agx_Object = new agx.RigidBody();
            agx_Object.add(dynamicRBGeometry);
            agx_Object.setLocalPosition(Operations.ToAgxVec3(pos));                ///AgX

            agx_Object.setLocalRotation(new agx.Quat(rot.x, rot.y, rot.z, rot.w)); ///AgX

            agx_Object.getMassProperties().setMass(mass);

            if (isStatic)
            {
                agx_Object.setMotionControl(agx.RigidBody.MotionControl.STATIC);
            }

            if (AddToRobot)
            {
                AddToAssembly();
            }
            else
            {
                Agx_Simulation.sim_Instance.add(agx_Object);
            }
        }
Exemplo n.º 7
0
        public static agx.RigidBody InstantiateTemplate(RigidBody template, Shape[] shapes)
        {
            if (template == null)
            {
                return(null);
            }

            var native = new agx.RigidBody(template.name);

            foreach (var shape in shapes)
            {
                var geometry = shape.CreateTemporaryNative();

                geometry.setEnable(shape.IsEnabled);
                if (shape.Material != null)
                {
                    geometry.setMaterial(shape.Material.GetInitialized <ShapeMaterial>().Native);
                }
                native.add(geometry, shape.GetNativeRigidBodyOffset(template));
            }

            template.SyncNativeTransform(native);

            // MassProperties (synchronization below) wont write any data if UseDefault = true.
            native.getMassProperties().setAutoGenerateMask((uint)agx.MassProperties.AutoGenerateFlags.AUTO_GENERATE_ALL);
            native.updateMassProperties();
            template.MassProperties.SetDefaultCalculated(native);
            native.getMassProperties().setAutoGenerateMask(0u);

            var prevNative = template.m_rb;

            try {
                template.m_rb = native;
                PropertySynchronizer.Synchronize(template);
                PropertySynchronizer.Synchronize(template.MassProperties);
            }
            finally {
                template.m_rb = prevNative;
            }

            return(native);
        }
Exemplo n.º 8
0
        public AgX_Sensor(Guid guid, string materialName, Vector3 pos, Vector3 scale, double mass)
        {
            this.guid  = guid;
            this.scale = scale;

            var dynamicRBGeometry = new agxCollide.Geometry();///AgX

            dynamicRBGeometry.add(new agxCollide.Box(Operations.ToAgxVec3(scale)));

            dynamicRBGeometry.setMaterial(new agx.Material(materialName));

            agx_Object = new agx.RigidBody();

            agx_Object.add(dynamicRBGeometry);

            agx_Object.setLocalPosition(Operations.ToAgxVec3(pos));

            agx_Object.getMassProperties().setMass(mass);

            Agx_Simulation.sim_Instance.add(agx_Object);
        }