示例#1
0
    internal List <GameObjectsPair> CheckForPossibleCollisions()
    {
        List <GameObjectsPair> result     = new List <GameObjectsPair>();
        List <SearchNode>      searchList = new List <SearchNode>();

        foreach (GameObject gameObject in gameObjects)
        {
            CustomCollider collider = gameObject.GetComponent <CustomCollider>();
            searchList.Add(new SearchNode(collider.GetMinXYZ((int)SEARCH_AXIS), collider.GetMaxXYZ((int)SEARCH_AXIS), gameObject));
        }
        searchList = searchList.OrderBy(o => o.startValue).ToList();
        for (int i = 0; i < searchList.Count; i++)
        {
            SearchNode searchNodeI = searchList[i];
            for (int j = i + 1; j < searchList.Count; j++)
            {
                SearchNode searchNodeJ = searchList[j];
                if (IsOverlapping(searchNodeI.startValue, searchNodeI.endValue, searchNodeJ.startValue, searchNodeJ.endValue))
                {
                    result.Add(new GameObjectsPair
                    {
                        gameObject1 = searchNodeI.attachedGameobject,
                        gameObject2 = searchNodeJ.attachedGameobject
                    });
                }
            }
        }
        return(result);
    }
示例#2
0
    void Start()
    {
        collisionTest = GetComponent <CustomCollider>();

        initialPos = transform.localPosition;
        initialRot = transform.localRotation;
    }
    private static Vector3 MeanSupport(CustomCollider collider, Vector3 vector)
    {
        Vector3        support   = Vector3.zero;
        List <Vector3> supports  = new List <Vector3>();
        float          dotValue  = -10000000;
        float          tolerance = 0.0001f;

        Matrix4x4 worldMatrix = collider.gameObject.transform.localToWorldMatrix;

        Vector3[] verts = collider.mesh.vertices;

        foreach (Vector3 v in verts)
        {
            Vector3 globalV = worldMatrix.MultiplyPoint3x4(v);
            if (Mathf.Abs(Vector3.Dot(vector, globalV) - dotValue) <= tolerance)
            {
                supports.Add(globalV);
            }
            else if (Vector3.Dot(vector, globalV) > dotValue)
            {
                supports.Clear();
                dotValue = Vector3.Dot(vector, globalV);
                supports.Add(globalV);
            }
        }

        foreach (Vector3 v in supports)
        {
            support += v;
        }

        support /= supports.Count;

        return(support);
    }
示例#4
0
 public void AddCollider(CustomCollider collider)
 {
     if (collider != null)
     {
         colliders.Add(collider);
     }
 }
示例#5
0
        internal CollisionInfo AreColliding()
        {
            CustomCollider collider1 = gameObject1.GetComponent <CustomCollider>();
            CustomCollider collider2 = gameObject2.GetComponent <CustomCollider>();

            return(collider1.IsColliding(collider2));
        }
示例#6
0
 public SCollisionInfo(CustomCollider other, Vector2 point, float depth, Vector2 norm)
 {
     otherObj       = other;
     collisionPoint = point;
     collisionDepth = depth;
     normal         = norm;
 }
示例#7
0
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        GUILayout.BeginVertical();

        CustomCollider col = target as CustomCollider;

        //顶点坐标
        if (col.Bound != null)
        {
            showBound = EditorGUILayout.Foldout(showBound, "Bound");
            if (showBound)
            {
                GUILayoutOption bound_Option          = GUILayout.Width(30);
                GUILayoutOption bound_xyzOption       = GUILayout.Width(10);
                GUILayoutOption bound_xyz_valueOption = GUILayout.Width(70);
                for (int i = 0; i < col.Bound.Count; i++)
                {
                    GUILayout.BeginHorizontal();
                    GUILayout.Label("p" + (i + 1), bound_Option);
                    GUILayout.Label("x", bound_xyzOption);
                    GUILayout.Label(col.Bound[i].x.value.ToString(), bound_xyz_valueOption);
                    GUILayout.Label("y", bound_xyzOption);
                    GUILayout.Label(col.Bound[i].y.value.ToString(), bound_xyz_valueOption);
                    GUILayout.Label("z", bound_xyzOption);
                    GUILayout.Label(col.Bound[i].z.value.ToString(), bound_xyz_valueOption);
                    GUILayout.EndHorizontal();
                }
            }
        }

        GUILayout.EndVertical();
    }
 public override bool IsCollide(CustomCollider otherCollider)
 {
     if (otherCollider.GetType() == typeof(CustomRectCollider))
     {
         return(IsCollideWithCustomRectCollider((CustomRectCollider)otherCollider));
     }
     return(false);
 }
示例#9
0
 public void NotifyObjectCollision(CustomCollider g, SCollisionInfo collInfo)
 {
     ICollideable[] listeners = g.gameObject.GetComponents <ICollideable> ();
     foreach (ICollideable l in listeners)
     {
         l.OnCollide(collInfo);
     }
 }
示例#10
0
 private void Start()
 {
     if (_customCollider == null)
     {
         _customCollider = GetComponent <CustomCollider>();
         Assert.IsNotNull(_customCollider);
     }
 }
示例#11
0
 public override void OnCollide(CustomCollider collider)
 {
     if (collider.CompareTag("Player"))
     {
         ScoreManager.Instance.CurrentScore += 1;
         gameObject.SetActive(false);
     }
 }
示例#12
0
    void Start()
    {
        circle = GetComponent <CircleCollider2D> ();
        box    = GetComponent <BoxCollider2D> ();
        poly   = GetComponent <PolygonCollider2D> ();
        custom = GetComponent <CustomCollider> ();

        SwitchTypes();
    }
    public void OnCollide(CustomCollider otherCollider)
    {
        CustomBehavior customBehavior = GetComponent <CustomBehavior>();

        if (customBehavior != null)
        {
            customBehavior.OnCollide(otherCollider);
        }
    }
示例#14
0
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        if (sheepAgent == null)
        {
            sheepAgent = animator.GetComponentInChildren <NavMeshAgent>();

            if (sheepAgent == null)
            {
                Debug.LogError("[IdleBehaviour] OnStateEnter: no nav mesh agent found");
            }
        }

        if (sheepTransform == null)
        {
            sheepTransform = animator.gameObject.transform;

            if (sheepTransform == null)
            {
                Debug.LogError("[IdleBehaviour] OnStateEnter: no sheep transform found");
            }
        }

        if (sensors == null)
        {
            sensors = animator.GetComponentInChildren <SensorsLinker>();

            if (sensors == null)
            {
                Debug.LogError("[IdleBehaviour] OnStateEnter: no sensors linker found");
            }
        }

        if (visionRadius == null)
        {
            visionRadius = sensors.visionCollider;
        }

        if (mediumRadius == null)
        {
            mediumRadius = sensors.mediumCollider;
        }

        if (closeRadius == null)
        {
            closeRadius = sensors.closeCollider;
        }

        currentStep = Random.Range(0, frequency);

        if (sheepAgent.isActiveAndEnabled)
        {
            sheepAgent.ResetPath();
        }
        //sheepAgent.isStopped = true;

        //Debug.Log(sheepTransform.name + " entered IDLE");
    }
示例#15
0
    void ResolveCollision(CustomCollider other)
    {
        // TODO: -------------
        // Teleport and apply impulses to the colliding objects
        // to resolve the collision.



        // -------------------
    }
示例#16
0
    static void Push(CustomCollider a, CustomCollider b, FixedPointF offsetfac, CustomVector2 offsetvec, bool a_at_left)
    {
        FixedPointF fac = offsetfac;

        CustomVector3 offset_pos;

        offset_pos.x = fac * offsetvec.x;
        offset_pos.y = new FixedPointF(0);
        offset_pos.z = fac * offsetvec.y;

        //Actor actor = null;
        //bool actorisA = false;
        //if (a.mIsobstacle && !b.mIsobstacle)
        //    actor = b.GetComponentInParent<Actor>();
        //else if (!a.mIsobstacle && b.mIsobstacle)
        //{
        //    actor = a.GetComponentInParent<Actor>();
        //    actorisA = true;
        //}
        //if (actor != null)
        //{
        //    if (a_at_left == actorisA)
        //        actor.Position -= offset_pos;
        //    else
        //        actor.Position += offset_pos;
        //}

        if (!a.mIsobstacle && !b.mIsobstacle)
        {
            return;
        }

        if (a.mIsobstacle && !b.mIsobstacle)
        {
            if (a_at_left)
            {
                b.AddOffsetPosition(offset_pos);
            }
            else
            {
                b.AddOffsetPosition(-offset_pos);
            }
        }
        else if (!a.mIsobstacle && b.mIsobstacle)
        {
            if (a_at_left)
            {
                a.AddOffsetPosition(-offset_pos);
            }
            else
            {
                a.AddOffsetPosition(offset_pos);
            }
        }
    }
示例#17
0
    static void DrawGizmos_NoSelected(Transform transform, GizmoType gizmoType)
    {
        CustomCollider collider = transform.GetComponent <CustomCollider>();

        if (collider == null)
        {
            return;
        }
        collider.DrawGizmos(no_select_color);
        Handles.Label(transform.position, transform.gameObject.name);
    }
示例#18
0
 public void Regist(CustomCollider tCollider)
 {
     if (!mIsOpen)
     {
         return;
     }
     if (!mColliderList.Contains(tCollider))
     {
         mColliderList.Add(tCollider);
     }
 }
示例#19
0
 public void UnRegist(CustomCollider tCollider)
 {
     if (!mIsOpen)
     {
         return;
     }
     if (mColliderList.Contains(tCollider))
     {
         mColliderList.Remove(tCollider);
     }
 }
 public override void OnCollide(CustomCollider collider)
 {
     if (collider.CompareTag("Obstacle"))
     {
         ToState((int)PlayerState.Falling);
     }
     if (collider.CompareTag("Floor"))
     {
         ToState((int)PlayerState.Dead);
     }
 }
 public void RegisterCollider(CustomCollider newCollider, bool immovable)
 {
     if (immovable)
     {
         immovablecolliders.Add(newCollider);
     }
     else
     {
         colliders.Add(newCollider);
     }
     allColliders.Add(newCollider);
 }
    public bool DoesBordersCollide(CustomCollider otherCollider)
    {
        this.BorderPoint(out Vector2 min1, out Vector2 max1);
        otherCollider.BorderPoint(out Vector2 min2, out Vector2 max2);
        if (max1.x <= min2.x || max2.x <= min1.x)
        {
            return(false);
        }
        if (max1.y <= min2.y || max2.y <= min1.y)
        {
            return(false);
        }

        return(true);
    }
    private static bool GJK(CustomCollider colliderA, CustomCollider colliderB)
    {
        /*Find the first point of the simplex*/
        searchDir = colliderA.transform.position - colliderB.transform.position;
        third     = Support(colliderB, searchDir) - Support(colliderA, -searchDir);

        /*Find the second point of the simplex*/
        searchDir = -third;
        second    = Support(colliderB, searchDir) - Support(colliderA, -searchDir);

        /*If the new simplex point have negative dot value, then the CSO must NOT contain the origin*/
        if (Vector3.Dot(second, searchDir) < 0)
        {
            return(false);
        }

        searchDir = Vector3.Cross(Vector3.Cross(third - second, -second), third - second);
        if (searchDir == Vector3.zero)
        {
            searchDir = Vector3.Cross(third - second, Vector3.right); //normal with x-axis
            if (searchDir == Vector3.zero)
            {
                searchDir = Vector3.Cross(third - second, Vector3.forward); //normal with z-axis
            }
        }
        simplexCount = 2;

        /*run through the iteration.*/
        for (int i = 0; i < maxGJKLoop; i++)
        {
            first = Support(colliderB, searchDir) - Support(colliderA, -searchDir);
            if (Vector3.Dot(first, searchDir) < 0)
            {
                return(false);
            }
            simplexCount++;
            if (simplexCount == 3)
            {
                UpdateTriangle();
            }
            else if (TetrahedralContainOrigin())
            {
                return(true);
            }
        }

        return(false);
    }
示例#24
0
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        if (sheepAgent == null)
        {
            sheepAgent = animator.GetComponentInChildren <NavMeshAgent>();

            if (sheepAgent == null)
            {
                Debug.LogError("[FollowBehaviour] OnStateEnter: no nav mesh agent found");
            }
        }

        if (sensors == null)
        {
            sensors = animator.GetComponentInChildren <SensorsLinker>();

            if (sensors == null)
            {
                Debug.LogError("[FollowBehaviour] OnStateEnter: no sensors linker found");
            }
        }

        if (sheepTransform == null)
        {
            sheepTransform = animator.gameObject.transform;

            if (sheepTransform == null)
            {
                Debug.LogError("[FollowBehaviour] OnStateEnter: no sheep transform found");
            }
        }

        if (visionRadius == null)
        {
            visionRadius = sensors.visionCollider;
        }
        if (mediumRadius == null)
        {
            mediumRadius = sensors.mediumCollider;
        }
        if (closeRadius == null)
        {
            closeRadius = sensors.closeCollider;
        }

        //Debug.Log(sheepTransform.name + " entered FOLLOW");
        currentTimer = decisionTimer;
    }
示例#25
0
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        if (sheepAgent == null)
        {
            sheepAgent = animator.GetComponentInChildren <NavMeshAgent>();

            if (sheepAgent == null)
            {
                Debug.LogError("[FleeBehaviour] OnStateEnter: no nav mesh agent found");
            }
        }

        if (sensors == null)
        {
            sensors = animator.GetComponentInChildren <SensorsLinker>();

            if (sensors == null)
            {
                Debug.LogError("[FleeBehaviour] OnStateEnter: no sensors linker found");
            }
        }

        if (sheepTransform == null)
        {
            sheepTransform = animator.gameObject.transform;

            if (sheepTransform == null)
            {
                Debug.LogError("[FleeBehaviour] OnStateEnter: no sheep transform found");
            }
        }

        if (visionRadius == null)
        {
            visionRadius = sensors.visionCollider;
        }
        if (mediumRadius == null)
        {
            mediumRadius = sensors.mediumCollider;
        }
        if (closeRadius == null)
        {
            closeRadius = sensors.closeCollider;
        }

        previousSpeed    = sheepAgent.speed;
        sheepAgent.speed = agentSpeedModifier * previousSpeed;
    }
    public void DeregisterCollider(CustomCollider requestedCollider, bool immovable)
    {
        if (immovable)
        {
            immovablecolliders.Remove(requestedCollider);
            immovablecolliders.TrimExcess();
        }
        else
        {
            colliders.Remove(requestedCollider);
            colliders.TrimExcess();
        }

        allColliders.Remove(requestedCollider);
        allColliders.TrimExcess();
    }
示例#27
0
    private void OnEnable()
    {
        _lastTool     = Tools.current;
        Tools.current = Tool.None;

        _customCollider = (CustomCollider)target;

        _serializedObject = serializedObject;
        _collisionPoints  = _serializedObject.FindProperty("CollisionPoints");
        _checkBoxes       = _serializedObject.FindProperty("CollisionPointCheckBoxes");
        _color            = _serializedObject.FindProperty("PointColor");
        _pointRadius      = _serializedObject.FindProperty("PointRadius");
        _collisionType    = _serializedObject.FindProperty("CollisionType");

        Selection.selectionChanged += Repaint;
        SceneView.duringSceneGui   += DuringSceneGUI;
    }
示例#28
0
 public void OnColliderMove(CustomCollider targetCollider)
 {
     if (!targetCollider.CheckCollide)
     {
         return;
     }
     foreach (CustomCollider collider in colliders)
     {
         if (collider.enabled && collider.gameObject.activeInHierarchy && collider != targetCollider)
         {
             if (targetCollider.DoesBordersCollide(collider) && targetCollider.IsCollide(collider))
             {
                 targetCollider.OnCollide(collider);
                 collider.OnCollide(targetCollider);
             }
         }
     }
 }
    private void SetPosition(Vector3 newPos)
    {
        if (!calculated)
        {
            UpdateSelf();
        }
        if (AdaptToParent)
        {
            pMax += (Vector2)(newPos - transform.position);
            pMin += (Vector2)(newPos - transform.position);
            transform.position = (anchorMaxPoint + pMax + anchorMinPoint + pMin) / 2;
        }
        CustomCollider customCollider = GetComponent <CustomCollider>();

        if (customCollider != null)
        {
            customCollider.OnMoving();
        }
    }
    private static void CalculateContactInfo(CustomCollider a, CustomCollider b, Vector3[] face)
    {
        currentContact.globalContactA   = MeanSupport(a, -searchDir);
        currentContact.localContactA    = a.gameObject.transform.worldToLocalMatrix.MultiplyPoint3x4(currentContact.globalContactA);
        currentContact.globalContactB   = MeanSupport(b, searchDir);
        currentContact.localContactB    = b.gameObject.transform.worldToLocalMatrix.MultiplyPoint3x4(currentContact.globalContactB);
        currentContact.contactNormal    = face[3];
        currentContact.penetrationDepth = Vector3.Dot(face[0], face[3]);

        /*  The method of calulating orthonormal basis is based on this website :
         *  http://allenchou.net/2013/12/game-physics-contact-generation-epa/
         */
        currentContact.contactTangent1 = currentContact.contactNormal.x >= 0.55735f ?
                                         new Vector3(currentContact.contactNormal.y, -currentContact.contactNormal.x, 0).normalized:
                                         new Vector3(0, currentContact.contactNormal.z, -currentContact.contactNormal.y).normalized;
        currentContact.contactTangent2 = Vector3.Cross(currentContact.contactNormal, currentContact.contactTangent1);

        return;
    }