Ejemplo n.º 1
0
        protected void CreateRigidbody()
        {
            obiRigidbody = null;

            // find the first rigidbody up our hierarchy:
            Rigidbody rb = GetComponentInParent <Rigidbody>();

            // if we have an rigidbody above us, see if it has a ObiRigidbody component and add one if it doesn't:
            if (rb != null)
            {
                obiRigidbody = rb.GetComponent <ObiRigidbody>();

                if (obiRigidbody == null)
                {
                    obiRigidbody = rb.gameObject.AddComponent <ObiRigidbody>();
                }

                Oni.SetColliderRigidbody(oniCollider, obiRigidbody.OniRigidbody);
            }
            else
            {
                Oni.SetColliderRigidbody(oniCollider, IntPtr.Zero);
            }
        }
Ejemplo n.º 2
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.");
                }
            }
        }
Ejemplo n.º 3
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));
                }
            }
        }