Exemplo n.º 1
0
 public Box2D(Vector2_ _center, double _width, double _height)
 {
     m_x      = _center.x;
     m_y      = _center.y;
     m_width  = _width;
     m_height = _height;
 }
Exemplo n.º 2
0
 public Box2D(Vector2_ _center, Vector2_ _size)
 {
     m_x      = _center.x;
     m_y      = _center.y;
     m_width  = _size.x;
     m_height = _size.y;
 }
Exemplo n.º 3
0
 public void SetRange(Vector4 range)
 {
     size     = new Vector2_(range.z, range.w);
     position = new Vector3_(range.x, range.y);
     offset   = new Vector2_(0, range.w * 0.5f);
     PlayParticle();
 }
Exemplo n.º 4
0
    public Vector3_ Check2D(Vector3_ tp, Creature target)
    {
        var n = new Vector2_(creature.x, creature.cy);
        var t = new Vector2_(target.x, target.cy);

        var c = creature.colliderSize + target.colliderSize;

        if (t.y > n.y + creature.colliderHeight || Mathd.Abs(n.x - t.x) < c * 0.025)
        {
            return(tp);
        }

        var nx = n.x;
        var tx = tp.x;

        if (tx > nx && t.x < n.x || tx < nx && t.x > n.x)
        {
            return(tp);
        }

        var max = tx > nx ? t.x - c : t.x + c;

        nx   = tx > nx && tx > max || tx < nx && tx < max ? max : tx;
        tp.x = nx;

        return(tp);
    }
Exemplo n.º 5
0
    /// <summary>
    /// Multiplies every component of this vector by the same component of scale.
    /// </summary>
    public void Scale(Vector2_ scale)
    {
        m_x *= scale.x;
        m_y *= scale.y;

        _CalculateParams();
    }
Exemplo n.º 6
0
    /// <summary>
    /// Moves a point current in a straight line towards a target point.
    /// </summary>
    public static Vector2_ MoveTowards(Vector2_ current, Vector2_ target, double maxDistanceDelta)
    {
        var a         = target - current;
        var magnitude = a.magnitude;

        return((magnitude <= maxDistanceDelta || magnitude == 0) ? target : current + a / magnitude * maxDistanceDelta);
    }
Exemplo n.º 7
0
    public override void OnUpdate(int diff)
    {
        //更新方向
        if (ParentCreature != null && Level.current)
        {
            if (moduleAI.GetDistance(this, ParentCreature, false, false) > 4)
            {
                if (Level.current != null && Level.current.isNormal)
                {
                    direction = ParentCreature.position.x > position.x
                        ? CreatureDirection.FORWARD
                        : CreatureDirection.BACK;

                    if (direction == CreatureDirection.FORWARD)
                    {
                        followEdge = new Vector2_(Level.current.edge.x, ParentCreature.position.x - 0.5);
                    }
                    else
                    {
                        followEdge = new Vector2_(ParentCreature.position.x + 0.5, Level.current.edge.y);
                    }
                }
                else
                {
                    direction = ParentCreature.position_.x > position_.x
                        ? CreatureDirection.FORWARD
                        : CreatureDirection.BACK;

                    if (direction == CreatureDirection.FORWARD)
                    {
                        followEdge = new Vector2_(Level.current?.edge.x ?? -1000, ParentCreature.position_.x - 0.5);
                    }
                    else
                    {
                        followEdge = new Vector2_(ParentCreature.position_.x + 0.5, Level.current?.edge.y ?? 1000);
                    }
                }
            }
            else
            {
                if (Level.current != null && Level.current.isNormal)
                {
                    followEdge = new Vector2_(ParentCreature.position.x - 0.5, ParentCreature.position.x + 0.5);
                }
                else
                {
                    followEdge = new Vector2_(ParentCreature.position_.x - 0.5, ParentCreature.position_.x + 0.5);
                }
            }
        }
        var dis = moduleAI.GetDistance(this, ParentCreature, false, false);

        ///距离数值不要随便改。与AI配置有关
        m_realMoving = !m_currentState.isIdle && (dis <= 4 || dis >= 6);

        base.OnUpdate(diff);
    }
Exemplo n.º 8
0
    public static double SignedAngle(Vector2_ from, Vector2_ to)
    {
        var nf = from.normalized;
        var nt = to.normalized;

        var num  = Mathd.Acos(Mathd.Clamp(Dot(nf, nt), -1, 1)) * 57.29578;
        var num2 = Mathd.Sign(nf.x * nt.y - nf.y * nt.x);

        return(num * num2);
    }
Exemplo n.º 9
0
    public static bool Cross(Sphere2D sphere, Sphere2D other, Vector2_ tp, ref Vector2_ hit)
    {
        if (sphere.Intersect(other))
        {
            return(true);
        }

        var o = other.center_;

        tp -= o;
        sphere.SetCenter(sphere.center_ - o);
        other.SetCenter(Vector2_.zero);

        other.radius += sphere.radius;

        var dir = tp - sphere.center_;
        var rr  = other.radius * other.radius * dir.sqrMagnitude;
        var t   = sphere.center_ + dir;
        var d   = sphere.x * t.y - t.x * sphere.y;
        var dd  = d * d;

        if (rr - dd > 0)
        {
            var proj = Vector2_.Project(-sphere.center_, dir.normalized);
            if (proj.magnitude <= dir.magnitude && Vector2_.Dot(proj, dir) > 0)
            {
                hit = proj + sphere.center_ + o;
                return(true);
            }
        }

        sphere.SetCenter(tp);

        if (sphere.Intersect(other))
        {
            hit = tp + o;
            return(true);
        }

        return(false);
    }
Exemplo n.º 10
0
 /// <summary>
 /// Dot Product of two vectors.
 /// </summary>
 public static double Dot(Vector2_ lhs, Vector2_ rhs)
 {
     return(lhs.x * rhs.x + lhs.y * rhs.y);
 }
Exemplo n.º 11
0
    /// <summary>
    /// Returns the distance between a and b.
    /// </summary>
    public static double Distance(Vector2_ a, Vector2_ b)
    {
        var vector = new Vector2_(a.x - b.x, a.y - b.y);

        return(Mathd.Sqrt(vector.x * vector.x + vector.y * vector.y));
    }
Exemplo n.º 12
0
 /// <summary>
 /// Returns a copy of vector with its magnitude clamped to maxLength.
 /// </summary>
 public static Vector2_ ClampMagnitude(Vector2_ vector, double maxLength)
 {
     return(vector.sqrMagnitude > maxLength * maxLength ? vector.normalized * maxLength : vector);
 }
Exemplo n.º 13
0
 /// <summary>
 /// Returns the angle in degrees between from and to.
 /// </summary>
 /// <param name="from">The vector from which the angular difference is measured.</param>
 /// <param name="to">The vector to which the angular difference is measured.</param>
 public static double Angle(Vector2_ from, Vector2_ to)
 {
     return(Mathd.Acos(Mathd.Clamp(Dot(from.normalized, to.normalized), -1, 1)) * 57.29578);
 }
Exemplo n.º 14
0
 public void Set(Vector2_ _center, double _width, double _height)
 {
     SetCenter(_center);
     SetSize(_width, _height);
 }
Exemplo n.º 15
0
    public static bool Cross(Box2D box, Sphere2D other, Vector2_ tp, ref Vector2_ hit)
    {
        if (box.Intersect(other))
        {
            return(true);
        }

        var o = other.center_;

        tp -= o;
        box.SetCenter(box.center_ - o);
        other.SetCenter(Vector2_.zero);

        var dir = tp - box.center_;

        var rr = other.radius * other.radius * dir.sqrMagnitude;
        var t  = box.topLeft + dir;
        var d  = box.leftEdge * t.y - t.x * box.topEdge;
        var dd = d * d;

        if (rr - dd > 0)
        {
            var proj = Vector2_.Project(-box.topLeft, dir.normalized);
            if (proj.magnitude <= dir.magnitude && Vector2_.Dot(proj, dir) > 0)
            {
                hit = Vector2_.Project(-box.center_, dir.normalized) + box.center_ + o;
                return(true);
            }
        }

        t  = box.bottomRight + dir;
        d  = box.rightEdge * t.y - t.x * box.bottomEdge;
        dd = d * d;

        if (rr - dd > 0)
        {
            var proj = Vector2_.Project(-box.bottomRight, dir.normalized);
            if (proj.magnitude <= dir.magnitude && Vector2_.Dot(proj, dir) > 0)
            {
                hit = Vector2_.Project(-box.center_, dir.normalized) + box.center_ + o;
                return(true);
            }
        }

        t  = box.topRight + dir;
        d  = box.rightEdge * t.y - t.x * box.topEdge;
        dd = d * d;

        if (rr - dd > 0)
        {
            var proj = Vector2_.Project(-box.topRight, dir.normalized);
            if (proj.magnitude <= dir.magnitude && Vector2_.Dot(proj, dir) > 0)
            {
                hit = Vector2_.Project(-box.center_, dir.normalized) + box.center_ + o;
                return(true);
            }
        }

        t  = box.bottomLeft + dir;
        d  = box.leftEdge * t.y - t.x * box.bottomEdge;
        dd = d * d;

        if (rr - dd > 0)
        {
            var proj = Vector2_.Project(-box.bottomLeft, dir.normalized);
            if (proj.magnitude <= dir.magnitude && Vector2_.Dot(proj, dir) > 0)
            {
                hit = Vector2_.Project(-box.center_, dir.normalized) + box.center_ + o;
                return(true);
            }
        }

        box.SetCenter(tp);

        if (box.Intersect(other))
        {
            hit = tp + o;
            return(true);
        }

        return(false);
    }
Exemplo n.º 16
0
 /// <summary>
 /// Reflects a vector off the plane defined by a normal.
 /// </summary>
 public static Vector2_ Reflect(Vector2_ inDirection, Vector2_ inNormal)
 {
     return(-2 * Dot(inNormal, inDirection) * inNormal + inDirection);
 }
Exemplo n.º 17
0
    public void Set(Vector2_ _center, double _radius)
    {
        SetCenter(_center);

        m_radius = _radius;
    }
Exemplo n.º 18
0
    public static bool Cross(Sphere2D sphere, Box2D other, Vector2_ tp, ref Vector2_ hit)
    {
        var box = new Box2D(sphere.center_, sphere.radius * 2, sphere.radius * 2);

        return(Cross(box, other, tp, ref hit));
    }
Exemplo n.º 19
0
 /// <summary>
 /// Returns a vector that is made from the smallest components of two vectors.
 /// </summary>
 public static Vector2_ Min(Vector2_ lhs, Vector2_ rhs)
 {
     return(new Vector2_(Mathd.Min(lhs.x, rhs.x), Mathd.Min(lhs.y, rhs.y)));
 }
Exemplo n.º 20
0
 /// <summary>
 /// Linearly interpolates between two vectors.
 /// </summary>
 public static Vector2_ LerpUnclamped(Vector2_ a, Vector2_ b, double t)
 {
     return(new Vector2_(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t));
 }
Exemplo n.º 21
0
 /// <summary>
 /// Linearly interpolates between two vectors.
 /// </summary>
 public static Vector2_ Lerp(Vector2_ a, Vector2_ b, double t)
 {
     t = Mathd.Clamp01(t);
     return(new Vector2_(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t));
 }
Exemplo n.º 22
0
 public void Set(Vector2_ _center, Vector2_ _size)
 {
     SetCenter(_center);
     SetSize(_size);
 }
Exemplo n.º 23
0
 public Sphere2D(Vector2_ _center, double _radius)
 {
     m_x      = _center.x;
     m_y      = _center.y;
     m_radius = _radius;
 }
Exemplo n.º 24
0
 public bool Contains(Vector2_ point)
 {
     return((point - center_).magnitude <= radius);
 }
Exemplo n.º 25
0
 /// <summary>
 /// Multiplies two vectors component-wise.
 /// </summary>
 public static Vector2_ Scale(Vector2_ a, Vector2_ b)
 {
     return(new Vector2_(a.x * b.x, a.y * b.y));
 }
Exemplo n.º 26
0
 public void SetCenter(Vector2_ _center)
 {
     m_x = _center.x;
     m_y = _center.y;
 }
Exemplo n.º 27
0
 public void SetSize(Vector2_ _size)
 {
     m_width  = _size.x;
     m_height = _size.y;
 }
Exemplo n.º 28
0
    public static bool Cross(Box2D box, Box2D other, Vector2_ tp, ref Vector2_ hit)
    {
        if (box.Intersect(other))
        {
            return(true);
        }

        var o = box.center_;

        tp -= o;
        other.Set(other.center_ - o, other.size_ + box.size_);
        box.SetCenter(Vector2_.zero);

        double minx = Mathd.Min(0, tp.x), maxx = Mathd.Max(0, tp.x), miny = Mathd.Min(0, tp.y), maxy = Mathd.Max(0, tp.y);

        var l1 = Mathd.Abs(other.leftEdge) < Mathd.Abs(other.rightEdge) ? other.leftEdge : other.rightEdge;
        var l2 = Mathd.Abs(other.topEdge) < Mathd.Abs(other.bottomEdge) ? other.topEdge : other.bottomEdge;

        bool b1 = false, b2 = false;

        if (other.leftEdge < 0 && other.rightEdge > 0)
        {
            goto _l2;
        }

_l1:
        b1 = true;
        if (l1 > minx && l1 < maxx)
        {
            var yy = tp.y / tp.x * l1;
            if (yy > other.bottomEdge && yy < other.topEdge)
            {
                hit.Set(l1, yy);
                hit += o;

                return(true);
            }
        }

        if (b2)
        {
            return(false);
        }

_l2:
        b2 = true;
        if (l2 > miny && l2 < maxy)
        {
            var xx = l2 * tp.x / tp.y;
            if (xx > other.leftEdge && xx < other.rightEdge)
            {
                hit.Set(xx, l2);
                hit += o;

                return(true);
            }
        }

        if (!b1)
        {
            goto _l1;
        }

        return(false);
    }
Exemplo n.º 29
0
    /// <summary>
    /// Projects a vector onto another vector.
    /// </summary>
    public static Vector2_ Project(Vector2_ vector, Vector2_ onNormal)
    {
        var dot = Dot(onNormal, onNormal);

        return(dot < Mathd.Epsilon ? zero : onNormal *Dot(vector, onNormal) / dot);
    }