Beispiel #1
0
        protected override void OnSimulate()
        {
            if (!CanMove)
            {
                return;
            }
            if (IsMoving)
            {
                if (canPathfind)
                {
                    if (repathCount <= 0)
                    {
                        if (viableDestination)
                        {
                            if (Pathfinder.GetPathNode(cachedBody.Position.x, cachedBody.Position.y, out currentNode))
                            {
                                if (straightPath)
                                {
                                    if (forcePathfind || Pathfinder.NeedsPath(currentNode, destinationNode))
                                    {
                                        if (Pathfinder.FindPath(Destination, currentNode, destinationNode, myPath))
                                        {
                                            hasPath   = true;
                                            pathIndex = 0;
                                        }
                                        else
                                        {
                                            if (IsFormationMoving)
                                            {
                                                StartMove(MyMovementGroup.Destination);
                                                IsFormationMoving = false;
                                            }
                                        }
                                        straightPath = false;
                                        repathCount  = RepathRate;
                                    }
                                    else
                                    {
                                        repathCount = StraightRepathRate;
                                    }
                                }
                                else
                                {
                                    if (forcePathfind || Pathfinder.NeedsPath(currentNode, destinationNode))
                                    {
                                        if (Pathfinder.FindPath(Destination, currentNode, destinationNode, myPath))
                                        {
                                            hasPath   = true;
                                            pathIndex = 0;
                                        }
                                        else
                                        {
                                            if (IsFormationMoving)
                                            {
                                                StartMove(MyMovementGroup.Destination);
                                                IsFormationMoving = false;
                                            }
                                        }
                                        repathCount = RepathRate;
                                    }
                                    else
                                    {
                                        straightPath = true;
                                        repathCount  = StraightRepathRate;
                                    }
                                }
                            }
                            else
                            {
                            }
                        }
                        else
                        {
                            hasPath = false;
                            if (IsFormationMoving)
                            {
                                StartMove(MyMovementGroup.Destination);
                                IsFormationMoving = false;
                            }
                        }
                    }
                    else
                    {
                        if (hasPath)
                        {
                            repathCount--;
                        }
                        else
                        {
                            repathCount--;
                        }
                    }

                    if (straightPath)
                    {
                        targetPos = Destination;
                    }
                    else if (hasPath)
                    {
                        if (pathIndex >= myPath.Count)
                        {
                            pathIndex = myPath.Count - 1;
                        }
                        targetPos = myPath[pathIndex];
                    }
                    else
                    {
                        targetPos = Destination;
                    }
                }
                else
                {
                    targetPos = Destination;
                }

                movementDirection = targetPos - cachedBody.Position;
                movementDirection.Normalize(out distance);
                if (targetPos.x != lastTargetPos.x || targetPos.y != lastTargetPos.y)
                {
                    lastTargetPos   = targetPos;
                    targetDirection = movementDirection;
                }

                if (distance > closingDistance)
                {
                    desiredVelocity = (movementDirection);
                    if (movementDirection.Cross(lastMovementDirection.x, lastMovementDirection.y) != 0)
                    {
                        lastMovementDirection = movementDirection;
                        cachedTurn.StartTurnRaw(movementDirection);
                    }
                }
                else
                {
                    if (distance < FixedMath.Mul(closingDistance, CollisionStopMultiplier))
                    {
                        Arrive();
                        return;
                    }
                    desiredVelocity = (movementDirection * (distance) / (closingDistance));
                }

                desiredVelocity *= timescaledSpeed;

                cachedBody._velocity += (desiredVelocity - cachedBody._velocity) * timescaledAcceleration;
                if (distance <= closingDistance)
                {
                    pathIndex++;
                }
                cachedBody.VelocityChanged = true;
                if (collidedWithTrackedAgent)
                {
                    if (collidedCount >= CollisionStopCount)
                    {
                        collidedCount  = 0;
                        collidingAgent = null;
                        Arrive();
                    }
                    else
                    {
                        if (lastPosition.FastDistance(cachedBody.Position.x, cachedBody.Position.y)
                            < collisionStopTreshold)
                        {
                            collidedCount++;
                        }
                        else
                        {
                            lastPosition  = cachedBody.Position;
                            collidedCount = 0;
                        }
                    }
                    collidedWithTrackedAgent = false;
                }
                else
                {
                    collidingAgent = null;
                    collidedCount  = 0;
                }
            }
            else
            {
                if (cachedBody.VelocityFastMagnitude > 0)
                {
                    cachedBody._velocity      -= cachedBody._velocity * timescaledAcceleration;
                    cachedBody.VelocityChanged = true;
                }
                stopTime++;
            }
        }
            public static long Cos(long theta)
            {
                long sin = Sin(theta);

                return(FixedMath.Sqrt(FixedMath.One - (sin.Mul(sin))));
            }
Beispiel #3
0
 public static long ToLong(this float f1)
 {
     return(FixedMath.Create(f1));
 }
 public Vector2d(Vector3 vec)
 {
     this.x = FixedMath.Create(vec.x);
     this.y = FixedMath.Create(vec.z);
 }
 public GridSettings()
 {
     Init(256, 256, FixedMath.Create(-128), FixedMath.Create(-128), true);
 }
 public Vector2d(Vector2 vec2)
 {
     this.x = FixedMath.Create(vec2.x);
     this.y = FixedMath.Create(vec2.y);
 }
 public Vector3 ToVector3(float z = 0f)
 {
     return(new Vector3((float)FixedMath.ToDouble(this.x), z, (float)FixedMath.ToDouble(this.y)));
 }
Beispiel #8
0
        public void Initialize(LSBody b1, LSBody b2)
        {
            IsValid = true;
            if (!IsValid)
            {
                return;
            }

            if (OnlyAffectBody1)
            {
                Body1 = b1;
                Body2 = b2;
            }
            else
            {
                if (b1.ID < b2.ID)
                {
                    Body1 = b1;
                    Body2 = b2;
                }
                else
                {
                    Body1 = b2;
                    Body2 = b1;
                }
            }

            _ranIndex = -1;

            _isColliding = false;

            DistX        = 0;
            DistY        = 0;
            PenetrationX = 0;
            PenetrationY = 0;

            FastCollideDistance  = b1.Radius + b2.Radius;
            FastCollideDistance *= FastCollideDistance;
            LeCollisionType      = CollisionType.None;
            if (Body1.Shape == ColliderType.None || Body2.Shape == ColliderType.None)
            {
            }
            else if (Body1.Shape == ColliderType.Circle)
            {
                if (Body2.Shape == ColliderType.Circle)
                {
                    LeCollisionType = CollisionType.Circle_Circle;
                }
                else if (Body2.Shape == ColliderType.AABox)
                {
                    LeCollisionType = CollisionType.Circle_AABox;
                }
                else if (Body2.Shape == ColliderType.Polygon)
                {
                    LeCollisionType = CollisionType.Circle_Polygon;
                }
            }
            else if (Body1.Shape == ColliderType.AABox)
            {
                if (Body2.Shape == ColliderType.Circle)
                {
                    LeCollisionType = CollisionType.Circle_AABox;
                }
                else if (Body2.Shape == ColliderType.AABox)
                {
                    LeCollisionType = CollisionType.AABox_AABox;
                }
                else if (Body2.Shape == ColliderType.Polygon)
                {
                    LeCollisionType = CollisionType.AABox_Polygon;
                }
            }
            else if (Body1.Shape == ColliderType.Polygon)
            {
                if (Body2.Shape == ColliderType.Circle)
                {
                    LeCollisionType = CollisionType.Circle_Polygon;
                }
                else if (Body2.Shape == ColliderType.AABox)
                {
                    LeCollisionType = CollisionType.AABox_Polygon;
                }
                else if (Body2.Shape == ColliderType.Polygon)
                {
                    LeCollisionType = CollisionType.Polygon_Polygon;
                }
            }

            DoPhysics = ((Body1.IsTrigger || Body2.IsTrigger) == false);
            if (DoPhysics)
            {
            }


            //TODO: Space out checks when culled
            //TODO: The time between collision checks might cause goofy behavior
            //Maybe use a distance or velocity heuristic for culling instead of time since last collision
            //It wouldn't be able to replace partitions because of raycasts and fast-moving objects
            //Let's see if this works well or if something better is needed.
            if (Body1.PreventCulling || Body2.PreventCulling)
            {
                //Never cull
                CullCounter = -1;
            }
            else
            {
                //Immediately check collision
                CullCounter = 0;
                //If collision distance is too large, don't cull based on distance
                PreventDistanceCull = FastCollideDistance > PhysicsManager.CullFastDistanceMax;
                LastCollidedFrame   = LockstepManager.FrameCount;
                FastDistanceOffset  = FixedMath.Sqrt(FastCollideDistance >> FixedMath.SHIFT_AMOUNT) + FixedMath.One * 2;
                FastDistanceOffset *= FastDistanceOffset;
            }
            Active = true;
            _Version++;
        }
        public static void FixedNumberField(string label, ref long Value, long max = 0)
        {
            var value = FixedMath.Create(EditorGUILayout.DoubleField(label, Math.Round(FixedMath.ToDouble(Value), 2, MidpointRounding.AwayFromZero)));

            if (max == 0 || value <= max)
            {
                Value = value;
            }
            else
            {
                Value = max;
            }
        }
Beispiel #10
0
 public void update(float x, float y)
 {
     m_Center.x = FixedMath.Create(x);
     m_Center.y = FixedMath.Create(y);
     init(m_Center, m_Half.x, m_Half.y);
 }
Beispiel #11
0
        internal void DistributeCollision()
        {
            if (!DoPhysics)
            {
                return;
            }
            switch (LeCollisionType)
            {
            case CollisionType.Circle_Circle:
                dist = FixedMath.Sqrt(FastDistance >> FixedMath.SHIFT_AMOUNT);

                depth = (Body1.Radius + Body2.Radius - dist);


                if (depth <= 0)
                {
                    return;
                }

                if (dist == 0)
                {
                    dist = 1;
                }
                //Minimum vector to no longer be colliding
                DistX = (DistX * depth / dist);
                DistY = (DistY * depth / dist);
                //Resolving collision
                //Note: Immovable bodies don't check collision against each other so this case doesn't need to be considered
                if (Body1.Immovable || (Body2.Immovable == false && Body1.Priority > Body2.Priority))
                {
                    //Invert adjustment values when Body2 is being moved
                    DistX *= -1;
                    DistY *= -1;
                    DistributeCircle_CirclePriority(Body1, Body2);
                }
                else
                {
                    if (OnlyAffectBody1)
                    {
                        return;
                    }
                    if (Body2.Immovable || Body2.Priority > Body1.Priority)
                    {
                        DistributeCircle_CirclePriority(Body2, Body1);
                    }
                    else
                    {
                        DistX /= 2;
                        DistY /= 2;
                        DistributeCircle(Body1);
                        DistX *= -1;
                        DistY *= -1;
                        DistributeCircle(Body2);
                    }
                }
                break;

            case CollisionType.Circle_AABox:
                if (Body1.Shape == ColliderType.AABox)
                {
                    DistributeCircle_Box(Body1, Body2);
                }
                else
                {
                    DistributeCircle_Box(Body2, Body1);
                }
                break;

            case CollisionType.Circle_Polygon:
                if (Body1.Shape == ColliderType.Circle)
                {
                    this.DistributeCircle_Poly(Body1, Body2);
                }
                else
                {
                    this.DistributeCircle_Poly(Body2, Body1);
                }
                break;
            }
        }
        public bool Overlaps(FastList <Vector2d> outputIntersectionPoints)
        {
            outputIntersectionPoints.FastClear();
            //Checks if this object overlaps the line formed by p1 and p2
            switch (this.Shape)
            {
            case ColliderType.Circle:
            {
                bool overlaps = false;
                //Check if the circle completely fits between the line
                long projPos = this._position.Dot(cacheAxis.x, cacheAxis.y);
                //Circle withing bounds?
                if (projPos >= axisMin && projPos <= axisMax)
                {
                    long projPerp = this._position.Dot(cacheAxisNormal.x, cacheAxisNormal.y);
                    long perpDif  = (cacheProjPerp - projPerp);
                    long perpDist = perpDif.Abs();
                    if (perpDist <= _radius)
                    {
                        overlaps = true;
                    }
                    if (overlaps)
                    {
                        long sin = (perpDif);
                        long cos = FixedMath.Sqrt(_radius.Mul(_radius) - sin.Mul(sin));
                        if (cos == 0)
                        {
                            outputIntersectionPoints.Add((cacheAxis * projPos) + perpVector);
                        }
                        else
                        {
                            outputIntersectionPoints.Add(cacheAxis * (projPos - cos) + perpVector);
                            outputIntersectionPoints.Add(cacheAxis * (projPos + cos) + perpVector);
                        }
                    }
                }
                else
                {
                    //If not, check distances to points
                    long p1Dist = _position.FastDistance(cacheP1.x, cacheP2.y);
                    if (p1Dist <= this.FastRadius)
                    {
                        outputIntersectionPoints.Add(cacheP1);
                        overlaps = true;
                    }
                    long p2Dist = _position.FastDistance(cacheP2.x, cacheP2.y);
                    if (p2Dist <= this.FastRadius)
                    {
                        outputIntersectionPoints.Add(cacheP2);
                        overlaps = true;
                    }
                }
                return(overlaps);
            }

            //break;
            case ColliderType.AABox:
            {
            }
            break;

            case ColliderType.Polygon:
            {
                bool intersected = false;


                for (int i = 0; i < this.Vertices.Length; i++)
                {
                    int      edgeIndex = i;
                    Vector2d pivot     = this.RealPoints [edgeIndex];
                    Vector2d edge      = this.Edges [edgeIndex];
                    long     proj1     = 0;
                    int      nextIndex = edgeIndex + 1 < this.RealPoints.Length ? edgeIndex + 1 : 0;
                    Vector2d nextPoint = RealPoints [nextIndex];
                    long     proj2     = (nextPoint - pivot).Dot(edge);

                    long min;
                    long max;
                    if (proj1 < proj2)
                    {
                        min = proj1;
                        max = proj2;
                    }
                    else
                    {
                        min = proj2;
                        max = proj1;
                    }

                    long lineProj1 = (cacheP1 - pivot).Dot(edge);
                    long lineProj2 = (cacheP2 - pivot).Dot(edge);

                    long lineMin;
                    long lineMax;
                    if (lineProj1 < lineProj2)
                    {
                        lineMin = lineProj1;
                        lineMax = lineProj2;
                    }
                    else
                    {
                        lineMin = lineProj2;
                        lineMax = lineProj1;
                    }
                    if (CollisionPair.CheckOverlap(min, max, lineMin, lineMax))
                    {
                        Vector2d edgeNorm      = this.EdgeNorms [edgeIndex];
                        long     normProj      = 0;
                        long     normLineProj1 = (cacheP1 - pivot).Dot(edgeNorm);
                        long     normLineProj2 = (cacheP2 - pivot).Dot(edgeNorm);

                        long normLineMin;
                        long normLineMax;

                        if (normLineProj1 < normLineProj2)
                        {
                            normLineMin = normLineProj1;
                            normLineMax = normLineProj2;
                        }
                        else
                        {
                            normLineMin = normLineProj2;
                            normLineMax = normLineProj1;
                        }

                        if (normProj >= normLineMin && normProj <= normLineMax)
                        {
                            long revProj1 = pivot.Dot(LSBody.cacheAxisNormal);
                            long revProj2 = nextPoint.Dot(cacheAxisNormal);

                            long revMin;
                            long revMax;
                            if (revProj1 < revProj2)
                            {
                                revMin = revProj1;
                                revMax = revProj2;
                            }
                            else
                            {
                                revMin = revProj2;
                                revMax = revProj1;
                            }

                            if (LSBody.cacheProjPerp >= revMin && LSBody.cacheProjPerp <= revMax)
                            {
                                intersected = true;
                                if (LSBody.calculateIntersections)
                                {
                                    long fraction         = normLineProj1.Abs().Div(normLineMax - normLineMin);
                                    long intersectionProj = FixedMath.Lerp(lineProj1, lineProj2, fraction);
                                    outputIntersectionPoints.Add(edge * intersectionProj + pivot);

                                    if (outputIntersectionPoints.Count == 2)
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                return(intersected);
            }
                //break;
            }
            return(false);
        }
Beispiel #13
0
        protected override void OnSimulate()
        {
            if (!CanMove)
            {
                return;
            }

            if (IsMoving)
            {
                Agent.SetState(AnimState.Moving);

                if (CanPathfind)
                {
                    if (DoPathfind)
                    {
                        DoPathfind = false;
                        if (viableDestination)
                        {
                            if (Pathfinder.GetStartNode(cachedBody.Position, out currentNode))
                            {
                                if (currentNode.DoesEqual(this.destinationNode))
                                {
                                    if (this.RepathTries >= 1)
                                    {
                                        this.Arrive();
                                    }
                                }
                                else
                                {
                                    if (straightPath)
                                    {
                                        if (Pathfinder.NeedsPath(currentNode, destinationNode, this.GridSize))
                                        {
                                            if (Pathfinder.FindPath(Destination, currentNode, destinationNode, myPath,
                                                                    GridSize, GetNodeHash(destinationNode)))
                                            {
                                                hasPath   = true;
                                                pathIndex = 0;
                                            }
                                            else
                                            {
                                                if (IsFormationMoving)
                                                {
                                                    StartMove(MyMovementGroup.Destination);
                                                    IsFormationMoving = false;
                                                }
                                            }
                                            straightPath = false;
                                        }
                                        else
                                        {
                                        }
                                    }
                                    else
                                    {
                                        if (Pathfinder.NeedsPath(currentNode, destinationNode, this.GridSize))
                                        {
                                            if (Pathfinder.FindPath(Destination, currentNode, destinationNode, myPath,
                                                                    GridSize, GetNodeHash(destinationNode)))
                                            {
                                                hasPath   = true;
                                                pathIndex = 0;
                                            }
                                            else
                                            {
                                                if (IsFormationMoving)
                                                {
                                                    StartMove(MyMovementGroup.Destination);
                                                    IsFormationMoving = false;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            straightPath = true;
                                        }
                                    }
                                }
                            }
                            else
                            {
                            }
                        }
                        else
                        {
                            hasPath = false;
                            if (IsFormationMoving)
                            {
                                StartMove(MyMovementGroup.Destination);
                                IsFormationMoving = false;
                            }
                        }
                    }
                    else
                    {
                    }

                    if (straightPath)
                    {
                        targetPos = Destination;
                    }
                    else if (hasPath)
                    {
                        if (pathIndex >= myPath.Count)
                        {
                            targetPos = this.Destination;
                        }
                        else
                        {
                            targetPos = myPath [pathIndex];
                        }
                    }
                    else
                    {
                        targetPos = Destination;
                    }
                }
                else
                {
                    targetPos = Destination;
                }

                movementDirection = targetPos - cachedBody._position;

                movementDirection.Normalize(out distance);
                if (targetPos.x != lastTargetPos.x || targetPos.y != lastTargetPos.y)
                {
                    lastTargetPos   = targetPos;
                    targetDirection = movementDirection;
                }
                bool movingToWaypoint = (this.hasPath && this.pathIndex < myPath.Count - 1);
                long stuckThreshold   = 1;

                if (distance > closingDistance || movingToWaypoint)
                {
                    desiredVelocity = (movementDirection);

                    if (CanTurn)
                    {
                        CachedTurn.StartTurnDirection(movementDirection);
                    }

                    stuckThreshold = this.timescaledAcceleration / 4;
                }
                else
                {
                    if (distance < FixedMath.Mul(closingDistance, StopMultiplier))
                    {
                        Arrive();
                        return;
                    }
                    if (this.SlowArrival)
                    {
                        long closingSpeed = distance.Div(closingDistance);
                        desiredVelocity = movementDirection * (closingSpeed);
                        stuckThreshold  = closingSpeed / LockstepManager.FrameRate;
                    }
                    else
                    {
                        desiredVelocity = (movementDirection);
                        stuckThreshold  = this.timescaledAcceleration;
                    }
                }
                //If unit has not moved stuckThreshold in a frame, it's stuck
                StuckTime++;
                if (GetCanAutoStop())
                {
                    if (Agent.Body.Position.FastDistance(AveragePosition) <= (stuckThreshold * stuckThreshold))
                    {
                        if (StuckTime > StuckTimeThreshold)
                        {
                            if (movingToWaypoint)
                            {
                                this.pathIndex++;
                            }
                            else
                            {
                                if (RepathTries < StuckRepathTries)
                                {
                                    DoPathfind = true;
                                    RepathTries++;
                                }
                                else
                                {
                                    RepathTries = 0;
                                    this.Arrive();
                                }
                            }
                            StuckTime = 0;
                        }
                    }
                    else
                    {
                        if (StuckTime > 0)
                        {
                            StuckTime -= 1;
                        }

                        RepathTries = 0;
                    }
                }
                if (movingToWaypoint)
                {
                    if (
                        (
                            this.pathIndex >= 0 &&
                            distance < closingDistance &&
                            (movementDirection).Dot(waypointDirection) < 0
                        ) ||
                        distance < FixedMath.Mul(closingDistance, FixedMath.Half))
                    {
                        this.pathIndex++;
                    }
                }

                desiredVelocity      *= Speed;
                cachedBody._velocity += GetAdjustVector(desiredVelocity);

                cachedBody.VelocityChanged = true;
            }
            else
            {
                //Slowin' down
                if (cachedBody.VelocityFastMagnitude > 0)
                {
                    cachedBody.Velocity += GetAdjustVector(Vector2d.zero);
                }
                StoppedTime++;
            }
            AutoStopPauser--;
            CollisionStopPauser--;
            StopPauseLooker--;
            AveragePosition = AveragePosition.Lerped(Agent.Body.Position, FixedMath.One / 2);
        }
Beispiel #14
0
 public Vector3d(int X, int Y, int Z)
 {
     this.x = FixedMath.Create(X);
     this.y = FixedMath.Create(Y);
     this.z = FixedMath.Create(Z);
 }
Beispiel #15
0
        private void DistributeCollision()
        {
            if (Body1.OnContact.IsNotNull())
            {
                Body1.OnContact(Body2);
            }
            if (Body2.OnContact.IsNotNull())
            {
                Body2.OnContact(Body1);
            }

            if (Body1.IsTrigger || Body2.IsTrigger)
            {
                return;
            }

            switch (LeCollisionType)
            {
            case CollisionType.Circle_Circle:
                DistX = Body1._position.x - Body2._position.x;
                DistY = Body1._position.y - Body2._position.y;
                dist  = FixedMath.Sqrt((DistX * DistX + DistY * DistY) >> FixedMath.SHIFT_AMOUNT);

                if (dist == 0)
                {
                    const int randomMax = (int)((long)int.MaxValue % (FixedMath.One / 64));
                    Body1._position.x    += LSUtility.GetRandom(randomMax) - randomMax / 2;
                    Body1._position.y    += LSUtility.GetRandom(randomMax) - randomMax / 2;
                    Body1.PositionChanged = true;
                    Body2._position.x    += LSUtility.GetRandom(randomMax) - randomMax / 2;
                    Body2._position.y    += LSUtility.GetRandom(randomMax) - randomMax / 2;
                    Body2.PositionChanged = true;
                    return;
                }


                depth = (Body1.Radius + Body2.Radius - dist);

                if (depth <= 0)
                {
                    return;
                }
                DistX = (DistX * depth / dist) / 2L;
                DistY = (DistY * depth / dist) / 2L;

                //Switch, used to be const
                bool applyVelocity = false;

                //Resolving collision
                if (Body1.Immovable && Body1.isActiveAndEnabled || (Body2.Immovable == false && Body1.Priority > Body2.Priority))
                {
                    Body2._position.x    -= DistX;
                    Body2._position.y    -= DistY;
                    Body2.PositionChanged = true;
                    if (applyVelocity)
                    {
                        Body2._velocity.x    -= DistX;
                        Body2.VelocityChanged = true;
                    }
                }
                else if (Body2.Immovable || Body2.Priority > Body1.Priority)
                {
                    Body1._position.x    += DistX;
                    Body1._position.y    += DistY;
                    Body1.PositionChanged = true;
                    if (applyVelocity)
                    {
                        Body1._velocity.x    += DistX;
                        Body1._velocity.y    += DistY;
                        Body1.VelocityChanged = true;
                    }
                }
                else
                {
                    DistX /= 2;
                    DistY /= 2;

                    Body1._position.x += DistX;
                    Body1._position.y += DistY;
                    Body2._position.x -= DistX;
                    Body2._position.y -= DistY;

                    Body1.PositionChanged = true;
                    Body2.PositionChanged = true;
                    if (applyVelocity)
                    {
                        DistX                /= 8;
                        DistY                /= 8;
                        Body1._velocity.x    += DistX;
                        Body1._velocity.y    += DistY;
                        Body1.VelocityChanged = true;

                        Body2._velocity.x    -= DistX;
                        Body2._velocity.y    -= DistY;
                        Body2.VelocityChanged = true;
                    }
                }
                break;

            case CollisionType.Circle_AABox:
                if (Body1.Shape == ColliderType.AABox)
                {
                    DistributeCircle_Box(Body1, Body2);
                }
                else
                {
                    DistributeCircle_Box(Body2, Body1);
                }
                break;

            case CollisionType.Circle_Polygon:
                if (Body1.Shape == ColliderType.Circle)
                {
                    this.DistributeCircle_Poly(Body1, Body2);
                }
                else
                {
                    this.DistributeCircle_Poly(Body2, Body1);
                }
                break;
            }
        }
 public static double Round(long value)
 {
     return(Math.Round(FixedMath.ToDouble(value), 2, MidpointRounding.AwayFromZero));
 }
Beispiel #17
0
        protected override void OnSimulate()
        {
            if (!CanMove)
            {
                return;
            }
            if (IsMoving)
            {
                if (CanPathfind)
                {
                    if (repathCount <= 0)
                    {
                        if (viableDestination)
                        {
                            if (Pathfinder.GetPathNode(cachedBody._position.x, cachedBody._position.y, out currentNode))
                            {
                                if (straightPath)
                                {
                                    if (forcePathfind || Pathfinder.NeedsPath(currentNode, destinationNode, this.GridSize))
                                    {
                                        if (Pathfinder.FindPath(Destination, currentNode, destinationNode, myPath,
                                                                GridSize))
                                        {
                                            hasPath   = true;
                                            pathIndex = 0;
                                        }
                                        else
                                        {
                                            if (IsFormationMoving)
                                            {
                                                StartMove(MyMovementGroup.Destination);
                                                IsFormationMoving = false;
                                            }
                                        }
                                        straightPath = false;
                                        repathCount  = RepathRate;
                                    }
                                    else
                                    {
                                        repathCount = StraightRepathRate;
                                    }
                                }
                                else
                                {
                                    if (forcePathfind || Pathfinder.NeedsPath(currentNode, destinationNode, this.GridSize))
                                    {
                                        if (Pathfinder.FindPath(Destination, currentNode, destinationNode, myPath,
                                                                GridSize))
                                        {
                                            hasPath   = true;
                                            pathIndex = 0;
                                        }
                                        else
                                        {
                                            if (IsFormationMoving)
                                            {
                                                StartMove(MyMovementGroup.Destination);
                                                IsFormationMoving = false;
                                            }
                                        }
                                        repathCount = RepathRate;
                                    }
                                    else
                                    {
                                        straightPath = true;
                                        repathCount  = StraightRepathRate;
                                    }
                                }
                            }
                            else
                            {
                            }
                        }
                        else
                        {
                            hasPath = false;
                            if (IsFormationMoving)
                            {
                                StartMove(MyMovementGroup.Destination);
                                IsFormationMoving = false;
                            }
                        }
                    }
                    else
                    {
                        if (hasPath)
                        {
                            //TODO: fix this shit
                            repathCount--;
                        }
                        else
                        {
                            repathCount--;
                        }
                    }

                    if (straightPath)
                    {
                        targetPos = Destination;
                    }
                    else if (hasPath)
                    {
                        if (pathIndex >= myPath.Count)
                        {
                            targetPos = this.Destination;
                        }
                        else
                        {
                            targetPos = myPath [pathIndex];
                        }
                    }
                    else
                    {
                        targetPos = Destination;
                    }
                }
                else
                {
                    targetPos = Destination;
                }

                movementDirection = targetPos - cachedBody._position;

                movementDirection.Normalize(out distance);
                if (targetPos.x != lastTargetPos.x || targetPos.y != lastTargetPos.y)
                {
                    lastTargetPos   = targetPos;
                    targetDirection = movementDirection;
                }
                bool movingToWaypoint = (this.hasPath && this.pathIndex < myPath.Count - 1);
                if (distance > closingDistance || movingToWaypoint)
                {
                    desiredVelocity = (movementDirection);
                    if (movementDirection.Cross(lastMovementDirection.x, lastMovementDirection.y).AbsMoreThan(FixedMath.Half))
                    {
                        lastMovementDirection = movementDirection;
                        if (CanTurn)
                        {
                            CachedTurn.StartTurnDirection(movementDirection);
                        }
                    }
                }
                else
                {
                    if (distance < FixedMath.Mul(closingDistance, CollisionStopMultiplier))
                    {
                        Arrive();
                        return;
                    }
                    desiredVelocity = (movementDirection * (distance) / (closingDistance));
                }


                if (movingToWaypoint)
                {
                    if (distance < FixedMath.Mul(closingDistance, FixedMath.One))
                    {
                        this.pathIndex++;
                    }
                }
                desiredVelocity *= timescaledSpeed;

                cachedBody._velocity += (desiredVelocity - cachedBody._velocity) * timescaledAcceleration;

                cachedBody.VelocityChanged = true;
            }
            else
            {
                if (cachedBody.VelocityFastMagnitude > 0)
                {
                    cachedBody._velocity      -= cachedBody._velocity * timescaledAcceleration;
                    cachedBody.VelocityChanged = true;
                }
                stopTime++;
            }
        }
        public static void DoubleField(Rect position, GUIContent label, ref long value, bool timescaled = false)
        {
            double scale = Scale(timescaled);

            value = FixedMath.Create(EditorGUI.DoubleField(position, label, value.ToDouble() * scale) / scale);
        }
 public Vector2d(float xFloat, float yFloat)
 {
     this.x = FixedMath.Create(xFloat);
     this.y = FixedMath.Create(yFloat);
 }
 public static void FixedNumberField(GUIContent content, int Rounding, ref long Value)
 {
     Value = FixedMath.Create(EditorGUILayout.DoubleField(content, Math.Round(FixedMath.ToDouble(Value), Rounding, MidpointRounding.AwayFromZero)));
 }
 public Vector2d(double xDoub, double yDoub)
 {
     this.x = FixedMath.Create(xDoub);
     this.y = FixedMath.Create(yDoub);
 }
Beispiel #22
0
 public static long GetRandomOne()
 {
     return(FixedMath.Create(GetRandom(), int.MaxValue));
 }
 public long z; //Height
 public Vector3d(Vector3 vec3)
 {
     this.x = FixedMath.Create(vec3.x);
     this.y = FixedMath.Create(vec3.z);
     this.z = FixedMath.Create(vec3.y);
 }
Beispiel #24
0
 /// <summary>
 /// World pos to relative position on grid.
 /// </summary>
 /// <returns>The relative x.</returns>
 /// <param name="xPos">X position.</param>
 public static long GetRelativeX(long xPos)
 {
     return((xPos >> AdditionalShiftSize) - (FixedMath.Create(BoundX)));
 }
Beispiel #25
0
        void BehaveWithTarget()
        {
            if (Target.IsActive == false || Target.SpawnVersion != targetVersion)
            {
                StopEngage();
                BehaveWithNoTarget();
                return;
            }
            Vector2d targetDirection = Target.Body._position - cachedBody._position;
            long     fastMag         = targetDirection.FastMagnitude();

            if (fastMag <= fastRangeToTarget)
            {
                if (!inRange)
                {
                    if (CanMove)
                    {
                        cachedMove.StopMove();
                    }
                }
                Agent.SetState(AnimState.Engaging);
                long mag = FixedMath.Sqrt(fastMag >> FixedMath.SHIFT_AMOUNT);
                //cachedTurn.StartTurn(targetDirection / mag);
                bool withinTurn = TrackAttackAngle == false ||
                                  (fastMag != 0 &&
                                   cachedBody._rotation.Dot(targetDirection.x, targetDirection.y) > 0 &&
                                   cachedBody._rotation.Cross(targetDirection.x, targetDirection.y).Abs() <= AttackAngle);
                bool needTurn = mag != 0 && !withinTurn;
                if (needTurn)
                {
                    if (CanTurn)
                    {
                        targetDirection /= mag;
                        cachedTurn.StartTurn(targetDirection);
                    }
                }
                else
                {
                    if (attackCount <= 0)
                    {
                        attackCount = attackFrameCount;
                        Fire();
                    }
                }

                if (inRange == false)
                {
                    inRange = true;
                }
            }
            else
            {
                if (CanMove)
                {
                    if (cachedMove.IsMoving == false)
                    {
                        cachedMove.StartMove(Target.Body._position);
                        cachedBody.Priority = basePriority;
                    }
                    else
                    {
                        if (Target.Body.PositionChanged || inRange)
                        {
                            cachedMove.Destination = Target.Body._position;
                        }
                    }
                }

                if (isAttackMoving || isFocused == false)
                {
                    searchCount -= 1;
                    if (searchCount <= 0)
                    {
                        searchCount = SearchRate;
                        if (ScanAndEngage())
                        {
                        }
                        else
                        {
                        }
                    }
                }
                if (inRange == true)
                {
                    inRange = false;
                }
            }
        }
Beispiel #26
0
 public static long GetRelativeY(long yPos)
 {
     return((yPos >> AdditionalShiftSize) - (FixedMath.Create(BoundY)));
 }
Beispiel #27
0
 public static double ToFormattedDouble(this long f1)
 {
     return(Math.Round(FixedMath.ToDouble(f1), 2, MidpointRounding.AwayFromZero));
 }
Beispiel #28
0
 public static long GetWorldY(int gridY)
 {
     return((FixedMath.Create(gridY + BoundY)) << AdditionalShiftSize);
 }
Beispiel #29
0
        public void Simulate()
        {
            if (PositionChanged || RotationChanged)
            {
                if (PositionChanged)
                {
                    FuturePosition.x        = Position.x + Velocity.x * PhysicsManager.ColSpreadMul;
                    FuturePosition.y        = Position.y + Velocity.y * PhysicsManager.ColSpreadMul;
                    this.transform.position = Position.ToVector3(this.transform.position.y);
                }
                else
                {
                }

                if (RotationChanged)
                {
                    this.transform.rotation = Quaternion.LookRotation(new Vector3(FixedMath.ToFloat(Rotation.x), 0, FixedMath.ToFloat(Rotation.y)));
                }
                else
                {
                }

                BuildPoints();
                BuildBounds();

                PositionChanged = false;
                RotationChanged = false;
            }
            else
            {
            }
        }
Beispiel #30
0
 /// <summary>
 /// This vector's magnitude.
 /// </summary>
 public long Magnitude()
 {
     return(FixedMath.Sqrt((this.x * this.x + this.y * this.y) >> FixedMath.SHIFT_AMOUNT));
 }