Esempio n. 1
0
 public void AddConstraint(int index1, ObiCollider body, Vector3 offset, float stiffness)
 {
     activeConstraints.Add(constraintCount);
     pinIndices.Add(index1);
     pinBodies.Add(body);
     pinOffsets.Add(offset);
     stiffnesses.Add(stiffness);
     pinBreakResistance.Add(float.MaxValue);
     constraintCount++;
 }
Esempio n. 2
0
 public ObiTerrainShapeTracker(ObiCollider source, TerrainCollider collider)
 {
     this.source   = source;
     this.collider = collider;
 }
 public ObiCapsuleShapeTracker(ObiCollider source, CapsuleCollider collider)
 {
     this.collider = collider;
     this.source   = source;
 }
 public ObiMeshShapeTracker(ObiCollider source, MeshCollider collider)
 {
     this.source   = source;
     this.collider = collider;
 }
Esempio n. 5
0
        private void UpdateColliders(ObiSolver solver)
        {
            for (int i = 0; i < colliders.Count; i++)
            {
                Collider source = colliders[i];
                if (source == null || !source.enabled || !source.gameObject.activeInHierarchy)
                {
                    continue;
                }

                Rigidbody   rb = source.GetComponentInParent <Rigidbody>();
                ObiCollider oc = source.GetComponent <ObiCollider>();

                // Get the adequate rigidBodyIndex. If several colliders share a rigidbody, they'll get the same rigidBodyIndex.
                int rigidBodyIndex = -1;
                if (rb != null)
                {
                    if (!rigidbodyIDs.TryGetValue(rb.GetInstanceID(), out rigidBodyIndex))
                    {
                        ObiRigidbody or = rb.GetComponent <ObiRigidbody>();

                        rigidBodyIndex = Oni.GetRigidbodyCount(oniColliderGroup);
                        Oni.SetRigidbodies(oniColliderGroup, new Oni.Rigidbody[] {
                            new Oni.Rigidbody(rb, (or != null) ? or.kinematicForParticles : false)
                        }, 1, rigidBodyIndex);
                        rigidbodyIDs[rb.GetInstanceID()] = rigidBodyIndex;
                    }
                }

                float        thickness = (oc != null)?oc.thickness:0;
                Oni.Collider collider  = new Oni.Collider();
                bool         supported = true;

                if (source is SphereCollider)
                {
                    collider = new Oni.Collider(source, Oni.ShapeType.Sphere, thickness, Oni.GetShapeCount(oniColliderGroup, Oni.ShapeType.Sphere), rigidBodyIndex, (oc != null)?oc.materialIndex:0);

                    Oni.SetSphereShapes(oniColliderGroup, new Oni.SphereShape[] {
                        new Oni.SphereShape(source as SphereCollider)
                    }, 1, Oni.GetShapeCount(oniColliderGroup, Oni.ShapeType.Sphere));
                }
                else if (source is BoxCollider)
                {
                    collider = new Oni.Collider(source, Oni.ShapeType.Box, thickness, Oni.GetShapeCount(oniColliderGroup, Oni.ShapeType.Box), rigidBodyIndex, (oc != null)?oc.materialIndex:0);

                    Oni.SetBoxShapes(oniColliderGroup, new Oni.BoxShape[] {
                        new Oni.BoxShape(source as BoxCollider)
                    }, 1, Oni.GetShapeCount(oniColliderGroup, Oni.ShapeType.Box));
                }
                else if (source is CapsuleCollider)
                {
                    collider = new Oni.Collider(source, Oni.ShapeType.Capsule, thickness, Oni.GetShapeCount(oniColliderGroup, Oni.ShapeType.Capsule), rigidBodyIndex, (oc != null)?oc.materialIndex:0);

                    Oni.SetCapsuleShapes(oniColliderGroup, new Oni.CapsuleShape[] {
                        new Oni.CapsuleShape(source as CapsuleCollider)
                    }, 1, Oni.GetShapeCount(oniColliderGroup, Oni.ShapeType.Capsule));
                }
                else if (source is CharacterController)
                {
                    collider = new Oni.Collider(source, Oni.ShapeType.Capsule, thickness, Oni.GetShapeCount(oniColliderGroup, Oni.ShapeType.Capsule), rigidBodyIndex, (oc != null)?oc.materialIndex:0);

                    Oni.SetCapsuleShapes(oniColliderGroup, new Oni.CapsuleShape[] {
                        new Oni.CapsuleShape(source as CharacterController)
                    }, 1, Oni.GetShapeCount(oniColliderGroup, Oni.ShapeType.Capsule));
                }
                else if (source is TerrainCollider)
                {
                    TerrainCollider tc = source as TerrainCollider;

                    Oni.HeightData data;
                    if (!heightData.TryGetValue(tc, out data))
                    {
                        data = heightData[tc] = new Oni.HeightData(tc);
                    }

                    collider = new Oni.Collider(source, Oni.ShapeType.Heightmap, thickness, Oni.GetShapeCount(oniColliderGroup, Oni.ShapeType.Heightmap), rigidBodyIndex, (oc != null)?oc.materialIndex:0);

                    Oni.SetHeightmapShapes(oniColliderGroup, new Oni.HeightmapShape[] {
                        new Oni.HeightmapShape(tc, data.AddrOfHeightData())
                    }, 1, Oni.GetShapeCount(oniColliderGroup, Oni.ShapeType.Heightmap));
                }
                else if (source is MeshCollider)
                {
                    MeshCollider             mc = source as MeshCollider;
                    MeshColliderShapeAndData shapeAndData;

                    if (mc.sharedMesh != null)
                    {
                        // We can share the same triangle data across several instances of the same MeshCollider:
                        if (!meshColliderData.TryGetValue(mc.sharedMesh, out shapeAndData))
                        {
                            // Get current amount of triangle mesh shapes:
                            int shapeIndex = Oni.GetShapeCount(oniColliderGroup, Oni.ShapeType.TriangleMesh);

                            // Generate mesh collider triangle data and shape:
                            Oni.TriangleMeshData  data  = new Oni.TriangleMeshData(mc);
                            Oni.TriangleMeshShape shape = new Oni.TriangleMeshShape(mc,
                                                                                    (oc != null)?oc.meshColliderType:Oni.TriangleMeshShape.MeshColliderType.ThinTwoSided,
                                                                                    thickness,
                                                                                    data.AddrOfVertexData(),
                                                                                    data.AddrOfTriangleData());

                            // Pack both in a small wrapper:
                            shapeAndData = new MeshColliderShapeAndData(data, shape, shapeIndex);

                            // Tell Oni we want to define a new triangle mesh:
                            Oni.SetTriangleMeshShapes(oniColliderGroup,
                                                      new Oni.TriangleMeshShape[] { shapeAndData.meshShape },
                                                      1, shapeIndex);

                            Oni.UpdateTriangleMeshShapes(oniColliderGroup, 1, shapeIndex);

                            // Store mesh collider data:
                            meshColliderData[mc.sharedMesh] = shapeAndData;
                        }

                        collider = new Oni.Collider(source, Oni.ShapeType.TriangleMesh, thickness, shapeAndData.shapeIndex, rigidBodyIndex, (oc != null)?oc.materialIndex:0);
                    }
                }
                else
                {
                    supported = false;
                    Debug.LogWarning("Collider type " + source.GetType() + " not supported by Obi. Ignoring it.");
                }

                if (supported)
                {
                    // convert to colliders to solver's local space:
                    if (solver.simulateInLocalSpace)
                    {
                        collider.SetSpaceTransform(solver.transform);
                    }

                    Oni.SetColliders(oniColliderGroup, new Oni.Collider[] { collider }, 1, Oni.GetColliderCount(oniColliderGroup));
                }
            }
        }
Esempio n. 6
0
        private void Update2DColliders()
        {
            for (int i = 0; i < colliders2D.Count; i++)
            {
                Collider2D source = colliders2D[i];
                if (source == null || !source.enabled || !source.gameObject.activeInHierarchy)
                {
                    continue;
                }

                Rigidbody2D rb = source.GetComponentInParent <Rigidbody2D>();
                ObiCollider oc = source.GetComponent <ObiCollider>();

                // Get the adequate rigidBodyIndex. If several colliders share a rigidbody, they'll get the same rigidBodyIndex.
                int rigidBodyIndex = -1;
                if (rb != null)
                {
                    if (!rigidbodyIDs.TryGetValue(rb.GetInstanceID(), out rigidBodyIndex))
                    {
                        ObiRigidbody or = rb.GetComponent <ObiRigidbody>();

                        rigidBodyIndex = Oni.GetRigidbodyCount(oniColliderGroup);
                        Oni.SetRigidbodies(oniColliderGroup, new Oni.Rigidbody[] {
                            new Oni.Rigidbody(rb, (or != null) ? or.kinematicForParticles : false)
                        }, 1, rigidBodyIndex);
                        rigidbodyIDs[rb.GetInstanceID()] = rigidBodyIndex;
                    }
                }

                float thickness = (oc != null)?oc.thickness:0;

                if (source is CircleCollider2D)
                {
                    Oni.SetColliders(oniColliderGroup, new Oni.Collider[] {
                        new Oni.Collider(source, Oni.ShapeType.Sphere, thickness, Oni.GetShapeCount(oniColliderGroup, Oni.ShapeType.Sphere), rigidBodyIndex, (oc != null)?oc.materialIndex:0)
                    }, 1, Oni.GetColliderCount(oniColliderGroup));
                    Oni.SetSphereShapes(oniColliderGroup, new Oni.SphereShape[] {
                        new Oni.SphereShape(source as CircleCollider2D)
                    }, 1, Oni.GetShapeCount(oniColliderGroup, Oni.ShapeType.Sphere));
                }
                else if (source is BoxCollider2D)
                {
                    Oni.SetColliders(oniColliderGroup, new Oni.Collider[] {
                        new Oni.Collider(source, Oni.ShapeType.Box, thickness, Oni.GetShapeCount(oniColliderGroup, Oni.ShapeType.Box), rigidBodyIndex, (oc != null)?oc.materialIndex:0)
                    }, 1, Oni.GetColliderCount(oniColliderGroup));
                    Oni.SetBoxShapes(oniColliderGroup, new Oni.BoxShape[] {
                        new Oni.BoxShape(source as BoxCollider2D)
                    }, 1, Oni.GetShapeCount(oniColliderGroup, Oni.ShapeType.Box));
                }
                else if (source is EdgeCollider2D)
                {
                    EdgeCollider2D           mc = source as EdgeCollider2D;
                    EdgeColliderShapeAndData shapeAndData;

                    if (!edgeColliderData.TryGetValue(source as EdgeCollider2D, out shapeAndData))
                    {
                        // Get current amount of triangle mesh shapes:
                        int shapeIndex = Oni.GetShapeCount(oniColliderGroup, Oni.ShapeType.EdgeMesh);

                        // Generate mesh collider triangle data and shape:
                        Oni.EdgeMeshData  data  = new Oni.EdgeMeshData(mc);
                        Oni.EdgeMeshShape shape = new Oni.EdgeMeshShape(mc,
                                                                        thickness,
                                                                        data.AddrOfVertexData(),
                                                                        data.AddrOfEdgeData());

                        // Pack both in a small wrapper:
                        shapeAndData = new EdgeColliderShapeAndData(data, shape, shapeIndex);

                        // Tell Oni we want to define a new triangle mesh:
                        Oni.SetEdgeMeshShapes(oniColliderGroup,
                                              new Oni.EdgeMeshShape[] { shapeAndData.meshShape },
                                              1, shapeIndex);

                        Oni.UpdateEdgeMeshShapes(oniColliderGroup, 1, shapeIndex);

                        // Store mesh collider data:
                        edgeColliderData[mc] = shapeAndData;
                    }

                    Oni.SetColliders(oniColliderGroup, new Oni.Collider[] {
                        new Oni.Collider(source, Oni.ShapeType.EdgeMesh, thickness, shapeAndData.shapeIndex, rigidBodyIndex, (oc != null)?oc.materialIndex:0)
                    }, 1, Oni.GetColliderCount(oniColliderGroup));
                }
                else
                {
                    Debug.LogWarning("2D Collider type " + source.GetType() + " not supported by Obi. Ignoring it.");
                }
            }
        }
Esempio n. 7
0
 public ObiCharacterControllerShapeTracker(ObiCollider source, CharacterController collider)
 {
     this.collider = collider;
     this.source   = source;
 }
 public void Awake()
 {
     unityCollider = GetComponent <ObiCollider>();
 }
 public ObiBoxShapeTracker(ObiCollider source, BoxCollider collider)
 {
     this.source   = source;
     this.collider = collider;
 }
Esempio n. 10
0
        public void UpdateSolverMaterials()
        {
            // reset the collision material list:
            collisionMaterials = new List <ObiCollisionMaterial>()
            {
                defaultMaterial
            };

            // Setup all materials used by particle actors:
            foreach (ObiActor actor in actors)
            {
                int materialIndex = 0;

                if (actor.material != null)
                {
                    materialIndex = collisionMaterials.IndexOf(actor.material);

                    // if the material has not been considered before:
                    if (materialIndex < 0)
                    {
                        materialIndex = collisionMaterials.Count;
                        collisionMaterials.Add(actor.material);
                    }
                }

                // Update material index for all actor particles:
                for (int i = 0; i < actor.particleIndices.Length; i++)
                {
                    materialIndices[actor.particleIndices[i]] = materialIndex;
                }
            }

            // Setup all materials used by colliders: //TODO: 2d colliders too!
            if (colliderGroup != null)
            {
                foreach (Collider c in colliderGroup.colliders)
                {
                    if (c == null)
                    {
                        continue;
                    }

                    ObiCollider oc = c.GetComponent <ObiCollider>();

                    if (oc == null)
                    {
                        continue;
                    }

                    oc.materialIndex = 0;             // Colliders with no ObiCollider component should use the default material.

                    if (oc.material == null)
                    {
                        continue;
                    }

                    oc.materialIndex = collisionMaterials.IndexOf(oc.material);

                    // if the material has not been considered before:
                    if (oc.materialIndex < 0)
                    {
                        oc.materialIndex = collisionMaterials.Count;
                        collisionMaterials.Add(oc.material);
                    }
                }
            }

            Oni.SetMaterialIndices(oniSolver, materialIndices, materialIndices.Length, 0);
            Oni.CollisionMaterial[] mArray = collisionMaterials.ConvertAll <Oni.CollisionMaterial>(a => a.GetEquivalentOniMaterial()).ToArray();
            Oni.SetCollisionMaterials(oniSolver, mArray, mArray.Length, 0);
        }
 public ObiSphereShapeTracker(ObiCollider source, SphereCollider collider)
 {
     this.source   = source;
     this.collider = collider;
 }
Esempio n. 12
0
        public void UpdateBodiesInfo()
        {
            oniColliders.Clear();
            oniRigidbodies.Clear();
            oniSpheres.Clear();
            oniBoxes.Clear();
            oniCapsules.Clear();
            oniHeightmaps.Clear();

            rigidbodyIDs.Clear();

            for (int i = 0; i < colliders.Count; i++)
            {
                Collider source = colliders[i];
                if (source == null)
                {
                    continue;
                }

                Rigidbody   rb = colliders[i].GetComponentInParent <Rigidbody>();
                ObiCollider oc = colliders[i].GetComponent <ObiCollider>();

                // Get the adequate rigidBodyIndex. If several colliders share a rigidbody, they'll get the same rigidBodyIndex.
                int rigidBodyIndex = -1;
                if (rb != null)
                {
                    if (!rigidbodyIDs.TryGetValue(rb.GetInstanceID(), out rigidBodyIndex))
                    {
                        rigidBodyIndex = oniRigidbodies.Count;
                        oniRigidbodies.Add(new Oni.Rigidbody(rb));
                        rigidbodyIDs[rb.GetInstanceID()] = rigidBodyIndex;
                    }
                }

                if (source is SphereCollider)
                {
                    oniColliders.Add(new Oni.Collider(source, Oni.ShapeType.Sphere, oniSpheres.Count, rigidBodyIndex, (oc != null)?oc.materialIndex:0));
                    oniSpheres.Add(new Oni.SphereShape(source as SphereCollider));
                }
                else if (source is BoxCollider)
                {
                    oniColliders.Add(new Oni.Collider(source, Oni.ShapeType.Box, oniBoxes.Count, rigidBodyIndex, (oc != null)?oc.materialIndex:0));
                    oniBoxes.Add(new Oni.BoxShape(source as BoxCollider));
                }
                else if (source is CapsuleCollider)
                {
                    oniColliders.Add(new Oni.Collider(source, Oni.ShapeType.Capsule, oniCapsules.Count, rigidBodyIndex, (oc != null)?oc.materialIndex:0));
                    oniCapsules.Add(new Oni.CapsuleShape(source as CapsuleCollider));
                }
                else if (source is CharacterController)
                {
                    oniColliders.Add(new Oni.Collider(source, Oni.ShapeType.Capsule, oniCapsules.Count, rigidBodyIndex, (oc != null)?oc.materialIndex:0));
                    oniCapsules.Add(new Oni.CapsuleShape(source as CharacterController));
                }
                else if (source is TerrainCollider)
                {
                    TerrainCollider tc = source as TerrainCollider;
                    if (!heightData.ContainsKey(tc))
                    {
                        heightData[tc] = new Oni.HeightData(source as TerrainCollider);
                    }

                    oniColliders.Add(new Oni.Collider(source, Oni.ShapeType.Heightmap, oniHeightmaps.Count, rigidBodyIndex, (oc != null)?oc.materialIndex:0));
                    oniHeightmaps.Add(new Oni.HeightmapShape(source as TerrainCollider, heightData[tc].AddrOfHeightData()));
                }
                else
                {
                    Debug.LogWarning(source.GetType() + " not supported by Obi. Ignoring it.");
                }
            }

            UpdateColliders();
            UpdateRigidbodies();
            UpdateSpheres();
            UpdateBoxes();
            UpdateCapsules();
            UpdateHeightmaps();
        }
Esempio n. 13
0
 public void OnEnable()
 {
     collider = (ObiCollider)target;
 }
 public ObiDistanceFieldShapeTracker(ObiCollider source, Component collider, ObiDistanceField distanceField)
 {
     this.source        = source;
     this.collider      = collider;
     this.distanceField = distanceField;
 }
Esempio n. 15
0
        public void UpdateSolverMaterials()
        {
            HashSet <ObiCollisionMaterial> materialsSet = new HashSet <ObiCollisionMaterial>();
            List <ObiCollisionMaterial>    materials    = new List <ObiCollisionMaterial>();

            // The default material must always be present.
            materialsSet.Add(defaultMaterial);
            materials.Add(defaultMaterial);

            // Setup all materials used by particle actors:
            foreach (ObiActor actor in actors)
            {
                int materialIndex = 0;

                if (actor.material != null)
                {
                    if (!materialsSet.Contains(actor.material))
                    {
                        materialIndex = materials.Count;
                        materials.Add(actor.material);
                        materialsSet.Add(actor.material);
                    }
                    else
                    {
                        materialIndex = materials.IndexOf(actor.material);
                    }
                }

                // Update material index for all actor particles:
                for (int i = 0; i < actor.particleIndices.Count; i++)
                {
                    materialIndices[actor.particleIndices[i]] = materialIndex;
                }
            }

            // Setup all materials used by colliders:
            if (colliderGroup != null)
            {
                foreach (Collider c in colliderGroup.colliders)
                {
                    if (c == null)
                    {
                        continue;
                    }

                    ObiCollider oc = c.GetComponent <ObiCollider>();

                    if (oc == null)
                    {
                        continue;
                    }

                    oc.materialIndex = 0;

                    if (oc.material == null)
                    {
                        continue;
                    }

                    if (!materialsSet.Contains(oc.material))
                    {
                        oc.materialIndex = materials.Count;
                        materials.Add(oc.material);
                        materialsSet.Add(oc.material);
                    }
                    else
                    {
                        oc.materialIndex = materials.IndexOf(oc.material);
                    }
                }
            }

            Oni.UnpinMemory(materialsHandle);
            materialsHandle = Oni.PinMemory(materials.ConvertAll <Oni.CollisionMaterial>(a => a.GetEquivalentOniMaterial()).ToArray());
            Oni.SetCollisionMaterials(oniSolver, materialsHandle.AddrOfPinnedObject());
        }