Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void Update()
- {
- IsCollidingOnTop = false;
- IsCollidingOnBottom = false;
- IsCollidingOnLeft = false;
- IsCollidingOnRight = false;
- List<object[]> invokations = new List<object[]>();
- if (IsAffectedByGravity) Velocity += World.Gravity;
- SSSPVector2 nextPosition = Position + Velocity;
- List<SSSPBody> bodiesToCheck =
- World.QuadTree.Query(new Rect(Position.X, Position.Y, Width + (Width * 3), Height + (Height * 3)));
- for (int i = 0; i < bodiesToCheck.Count; i++)
- {
- SSSPBody body = bodiesToCheck[i];
- if (body == this || IgnoreCollisionDetectBodies.Contains(body) ||
- body.CollisionGroups.Any(IgnoreCollisionDetectGroups.Contains)) continue;
- int nextH = 0, nextV = 0;
- bool overlap = false;
- if (true)
- {
- if (SSSPUtils.IsOverlapping(this, body, nextPosition.X - Position.X))
- {
- overlap = true;
- int left = (body.CornerMin.X - (nextPosition.X + Width / 2));
- int right = (body.CornerMax.X - (nextPosition.X - Width / 2));
- nextH = Math.Abs(left) < right ? left : right;
- if (!(body.CollisionGroups.Any(IgnoreCollisionResolveGroups.Contains)) &&
- InvokeOnResolve(body, new SSSPVector2(nextH, 0)))
- {
- nextPosition.X += nextH;
- if (Math.Abs(left) < right)
- {
- if (Velocity.X > 0) Velocity = new SSSPVector2(0, Velocity.Y);
- IsCollidingOnLeft = true;
- }
- else
- {
- if (Velocity.X < 0) Velocity = new SSSPVector2(0, Velocity.Y);
- IsCollidingOnRight = true;
- }
- }
- }
- if (SSSPUtils.IsOverlapping(this, body, 0, nextPosition.Y - Position.Y))
- {
- overlap = true;
- int top = (body.CornerMin.Y - (nextPosition.Y + Height / 2));
- int bottom = (body.CornerMax.Y - (nextPosition.Y - Height / 2));
- nextV = Math.Abs(top) < bottom ? top : bottom;
- if (!(body.CollisionGroups.Any(IgnoreCollisionResolveGroups.Contains)) &&
- InvokeOnResolve(body, new SSSPVector2(0, nextV)))
- {
- nextPosition.Y += nextV;
- if (Math.Abs(top) < bottom)
- {
- if (Velocity.Y > 0) Velocity = new SSSPVector2(Velocity.X, 0);
- IsCollidingOnTop = true;
- }
- else
- {
- if (Velocity.Y < 0) Velocity = new SSSPVector2(Velocity.X, 0);
- IsCollidingOnBottom = true;
- }
- }
- }
- }
- if (overlap) invokations.Add(new object[] { body, nextH, nextV });
- }
- foreach (object[] objects in invokations)
- {
- InvokeOnOverlap((SSSPBody)objects[0], (int)objects[1], (int)objects[2]);
- SSSPBody b = (SSSPBody)objects[0];
- b.InvokeOnOverlap(this, (int)objects[1] * -1, (int)objects[2] * -1);
- }
- Position = nextPosition;
- InvokeBoundsChanged(new EventArgs());
- }
- ---
- public static bool IsOverlapping(SSSPBody mBody1, SSSPBody mBody2, int mOffsetX = 0, int mOffsetY = 0)
- {
- return mBody1.CornerMax.X + mOffsetX > mBody2.CornerMin.X &&
- mBody1.CornerMin.X + mOffsetX < mBody2.CornerMax.X &&
- (mBody1.CornerMax.Y + mOffsetY > mBody2.CornerMin.Y &&
- mBody1.CornerMin.Y + mOffsetY < mBody2.CornerMax.Y);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement