public void AddCollidableInstance(CollideableGameObject gameObject, Type type) { if (!CollisionList.ContainsKey(type)) { CollisionList.Add(type, new List <CollideableGameObject>()); } CollisionList.TryGetValue(type, out cList); cList.Add(gameObject); }
bool CheckEdges(CollideableGameObject t, CollideableGameObject o) { //This whole operation of checking edges is expensive and therefore we make sure //that the box is actually within the possible viscinity of collision //BoundingPoints This Vector2 TBottomRight = t.BoxPosition + new Vector2(t.BoxWidth, t.BoxHeight); Vector2 TBottomLeft = t.BoxPosition + new Vector2(0, t.BoxHeight); Vector2 TTopLeft = t.BoxPosition + new Vector2(0, 0); Vector2 TTopRight = t.BoxPosition + new Vector2(t.BoxWidth, 0); //BoundingPoints Other Vector2 OBottomRight = o.BoxPosition + new Vector2(o.BoxWidth, o.BoxHeight); Vector2 OBottomLeft = o.BoxPosition + new Vector2(0, o.BoxHeight); Vector2 OTopLeft = o.BoxPosition + new Vector2(0, 0); Vector2 OTopRight = o.BoxPosition + new Vector2(o.BoxWidth, 0); //RotatePoints This TBottomRight = Rotate(t.BoxX + t.BoxWidth / 2, t.BoxY + t.BoxHeight / 2, t.Rotation, TBottomRight + new Vector2(-0.5f, -0.5f)); TBottomLeft = Rotate(t.BoxX + t.BoxWidth / 2, t.BoxY + t.BoxHeight / 2, t.Rotation, TBottomLeft + new Vector2(0.5f, -0.5f)); TTopLeft = Rotate(t.BoxX + t.BoxWidth / 2, t.BoxY + t.BoxHeight / 2, t.Rotation, TTopLeft + new Vector2(0.5f, 0.5f)); TTopRight = Rotate(t.BoxX + t.BoxWidth / 2, t.BoxY + t.BoxHeight / 2, t.Rotation, TTopRight + new Vector2(-0.5f, 0.5f)); //RotatePoints Other OBottomRight = Rotate(o.BoxX + o.BoxWidth / 2, o.BoxY + o.BoxHeight / 2, o.Rotation, OBottomRight + new Vector2(-0.5f, -0.5f)); OBottomLeft = Rotate(o.BoxX + o.BoxWidth / 2, o.BoxY + o.BoxHeight / 2, o.Rotation, OBottomLeft + new Vector2(0.5f, -0.5f)); OTopLeft = Rotate(o.BoxX + o.BoxWidth / 2, o.BoxY + o.BoxHeight / 2, o.Rotation, OTopLeft + new Vector2(0.5f, 0.5f)); OTopRight = Rotate(o.BoxX + o.BoxWidth / 2, o.BoxY + o.BoxHeight / 2, o.Rotation, OTopRight + new Vector2(-0.5f, 0.5f)); Vector2[] TRectangleEdges = new Vector2[4]; Vector2[] ORectangleEdges = new Vector2[4]; Vector2[] TRectangleDiagonals = new Vector2[4]; Vector2[] ORectangleDiagonals = new Vector2[4]; TRectangleEdges[0] = TBottomRight; TRectangleEdges[1] = TBottomLeft; TRectangleEdges[2] = TTopLeft; TRectangleEdges[3] = TTopRight; ORectangleEdges[0] = OBottomRight; ORectangleEdges[1] = OBottomLeft; ORectangleEdges[2] = OTopLeft; ORectangleEdges[3] = OTopRight; TRectangleDiagonals[0] = TTopLeft; TRectangleDiagonals[1] = TBottomRight; TRectangleDiagonals[2] = TBottomLeft; TRectangleDiagonals[3] = TTopRight; ORectangleDiagonals[0] = OTopLeft; ORectangleDiagonals[1] = OBottomRight; ORectangleDiagonals[2] = OBottomLeft; ORectangleDiagonals[3] = OTopRight; //Check Intersection for rectangle edges for (int i = 0; i < TRectangleEdges.Length; i++) { for (int j = 0; j < ORectangleEdges.Length; j++) { if (LineIntersection(TRectangleEdges[i], TRectangleEdges[(i + 1) % TRectangleEdges.Length], ORectangleEdges[j], ORectangleEdges[(j + 1) % ORectangleEdges.Length])) { return(true); } } } //Check Intersection for internal diagonal lines for (int i = 0; i < TRectangleDiagonals.Length; i++) { for (int j = 0; j < ORectangleDiagonals.Length; j++) { if (LineIntersection(TRectangleDiagonals[i], TRectangleDiagonals[(i + 1) % TRectangleDiagonals.Length], ORectangleDiagonals[j], ORectangleDiagonals[(j + 1) % ORectangleDiagonals.Length])) { return(true); } } } return(false); }
public override void Update() { Depth = Y; //check keys W = Keyboard.IsKeyDown(Keys.W); S = Keyboard.IsKeyDown(Keys.S); A = Keyboard.IsKeyDown(Keys.A); D = Keyboard.IsKeyDown(Keys.D); //axes axis_x_dir = Convert.ToInt32(D) - Convert.ToInt32(A); axis_y_dir = Convert.ToInt32(S) - Convert.ToInt32(W); //acceleration axis_x_add = axis_x_dir * axis_x_acceleration; axis_y_add = axis_y_dir * axis_y_acceleration; //restitution axis_x_sub = MathHelper.Min(axis_x_restitution, Math.Abs(axis_x)) * Math.Sign(axis_x) * Convert.ToInt32(axis_x_dir == 0);//cause if statements are slow? axis_y_sub = MathHelper.Min(axis_y_restitution, Math.Abs(axis_y)) * Math.Sign(axis_y) * Convert.ToInt32(axis_y_dir == 0); axis_x = MathHelper.Clamp(axis_x + axis_x_add - axis_x_sub, -axis_x_max, axis_x_max); axis_y = MathHelper.Clamp(axis_y + axis_y_add - axis_y_sub, -axis_y_max, axis_y_max); //normalize axis_x and axis_y if (axis_x != 0 && axis_y != 0) { var dist = Math.Sqrt((axis_x * axis_x) + (axis_y * axis_y)); var mdist = MathHelper.Min(movement_speed + 1, (float)dist); axis_x = (axis_x / (float)dist) * (mdist); axis_y = (axis_y / (float)dist) * (mdist); } CollideBlock = BoxCollisionList((int)axis_x, 0, typeof(Solid)); if (CollideBlock != null && CollideBlock.Collision) { if (X > CollideBlock.BoxX && axis_x < 0) { axis_x = 0; //prevents clipping into the solid object's collisonbox when moving diagonally X = CollideBlock.BoxX + CollideBlock.BoxWidth + Math.Abs(X - BoxX); } if (X < CollideBlock.BoxX && axis_x > 0) { axis_x = 0; //prevents clipping into the solid object's collisonbox when moving diagonally X = CollideBlock.BoxX + Math.Abs(X - BoxX) - BoxWidth; } } CollideBlock = BoxCollisionList(0, (int)axis_y, typeof(Solid)); if (CollideBlock != null && CollideBlock.Collision) { if (Y > CollideBlock.BoxY && axis_y < 0) { axis_y = 0; Y = CollideBlock.BoxY + CollideBlock.BoxHeight + Math.Abs(Y - BoxY); } if (Y < CollideBlock.BoxY && axis_y > 0) { axis_y = 0; Y = CollideBlock.BoxY + Math.Abs(Y - BoxY) - BoxHeight; } } //move coordinates X += (int)axis_x; Y += (int)axis_y; if (axis_x != 0 || axis_y != 0) { AnimationSpeed = 0.12f; } else { AnimationIndex = (int)MathHelper.Lerp(AnimationIndex, 0, 0.2f); AnimationSpeed = 0; } look_angle = MathHelper.ToDegrees(G.PointDirection(X, Y, Camera.MouseX, Camera.MouseY)); SpriteDirection(look_angle); /*if (Keyboard.IsKeyDown(Keys.Down)) * { * Camera.Zoom -= 0.01f * Camera.Zoom; * } * if (Keyboard.IsKeyDown(Keys.Up)) * { * Camera.Zoom += 0.01f * Camera.Zoom; * }*/ if (GetMousePressed(Mouse.LeftButton)) { new PlayerAttack(X, Y); } if (GetKeyPressed(Keys.X)) { new Player(X + 20, Y + 20); nrOfInstances++; } if (GetKeyPressed(Keys.Space)) { Camera.ScreenShake += 8; } if (GetKeyPressed(Keys.R)) { RestartGame(); } if (AnimationIndex == numberOfFrames - 1 && animationCounter < 0.1f) { new Dust(X, (int)(Y + ScaledWidth / 2), G.PointDirection(0, 0, -axis_x, -axis_y) - MathHelper.ToRadians(90)); } base.Update(); }