Exemple #1
0
        public void Unserialize(GMDataReader reader)
        {
            Name                  = reader.ReadStringPointerObject();
            SpriteID              = reader.ReadInt32();
            Visible               = reader.ReadWideBoolean();
            Solid                 = reader.ReadWideBoolean();
            Depth                 = reader.ReadInt32();
            Persistent            = reader.ReadWideBoolean();
            ParentObjectID        = reader.ReadInt32();
            MaskSpriteID          = reader.ReadInt32();
            Physics               = reader.ReadWideBoolean();
            PhysicsSensor         = reader.ReadWideBoolean();
            PhysicsShape          = (CollisionShape)reader.ReadInt32();
            PhysicsDensity        = reader.ReadSingle();
            PhysicsRestitution    = reader.ReadSingle();
            PhysicsGroup          = reader.ReadInt32();
            PhysicsLinearDamping  = reader.ReadSingle();
            PhysicsAngularDamping = reader.ReadSingle();
            int vertexCount = reader.ReadInt32();

            PhysicsFriction  = reader.ReadSingle();
            PhysicsAwake     = reader.ReadWideBoolean();
            PhysicsKinematic = reader.ReadWideBoolean();
            PhysicsVertices  = new List <PhysicsVertex>();
            for (int i = vertexCount; i > 0; i--)
            {
                PhysicsVertex v = new PhysicsVertex();
                v.Unserialize(reader);
                PhysicsVertices.Add(v);
            }
            Events = new GMPointerList <GMPointerList <Event> >();
            Events.Unserialize(reader);
        }
    public void CheckForCollisions()
    {
        if (_collidableObjects == null)
        {
            return;
        }

        for (int i = 0; i < _collidableObjects.Length; i++)
        {
            PhysicsVertex physVertex = _collidableObjects[i].GetComponent <PhysicsVertex>();
            if (physVertex == null)
            {
                Projectile projectile = _collidableObjects[i].GetComponent <Projectile>();
                if (projectile != null)
                {
                    CheckProjectileCollision(projectile);
                }

                continue;
            }

            if (IsOwnedVertex(physVertex))
            {
                continue;
            }

            for (int j = 0; j < _edges.Count; j++)
            {
                Vector3 p  = physVertex.transform.position;
                Vector3 e1 = _edges[j].FirstVertex.transform.position;
                Vector3 e2 = _edges[j].SecondVertex.transform.position;

                bool isWithinX = (p.x >= e1.x && p.x <= e2.x) || (p.x >= e2.x && p.x <= e1.x) ||
                                 (Mathf.Abs(p.x - e1.x) < Mathf.Abs(physVertex.CurrentMoveVector.x));
                bool isWithinY = (p.y >= e1.y && p.y <= e2.y) || (p.y >= e2.y && p.y <= e1.y) ||
                                 (Mathf.Abs(p.y - e1.y) < Mathf.Abs(physVertex.CurrentMoveVector.y));

                if (isWithinX && isWithinY)
                {
                    float currentPerpDotProd  = SignedTriangleArea(e1, e2, p);
                    float previousPerpDotProd = SignedTriangleArea(e1, e2, physVertex.PreviousPosition);

                    if (physVertex.IsPreviousPositionVirtual)
                    {
                        previousPerpDotProd = SignedTriangleArea(e1, e2, p);
                        physVertex.IsPreviousPositionVirtual = false;
                    }

                    bool hasHitPoint = Mathf.Abs((_collidableObjects[i].transform.position - _edges[j].FirstVertex.transform.position).magnitude) < 0.1f ||
                                       Mathf.Abs((_collidableObjects[i].transform.position - _edges[j].SecondVertex.transform.position).magnitude) < 0.1f;

                    if ((Mathf.Abs(previousPerpDotProd) > 0.0001f && (currentPerpDotProd / previousPerpDotProd < 0.0f)) || hasHitPoint)
                    {
                        //physVertex.Collide(e1, e2);
                    }
                }
            }
        }
    }
    public bool IsOwnedVertex(PhysicsVertex vertex)
    {
        for (int i = 0; i < _vertices.Count; i++)
        {
            PhysicsVertex comparand = _vertices[i].transform.GetComponent <PhysicsVertex>();
            if (comparand != null && comparand.Equals(vertex))
            {
                return(true);
            }
        }

        return(false);
    }