public static CollisionType[,] backgroundCollisionCheck(Rectangle objectDestinationRectangle) { CollisionType[,] temp = new CollisionType[10, 10]; Vector2 tempy = new Vector2(objectDestinationRectangle.X + objectDestinationRectangle.Width / 2, objectDestinationRectangle.Y + objectDestinationRectangle.Height / 2); int posX = (int)(tempy.X / 32); if (posX < 5) { posX = 5; } int posY = (int)(tempy.Y / 32); if (posY < 5) { posY = 5; } for (int i = 0; i < temp.GetLength(0); i++) { for (int j = 0; j < temp.GetLength(1); j++) { temp[i, j] = new CollisionType(); } } int x = 0; int y = 0; Rectangle tempRect = Rectangle.Empty; for (int i = posY - 5; i < m_TileData.GetLength(0) && i < posY + 5; i++) { for (int j = posX - 5; j < m_TileData.GetLength(1) && j < posX + 5; j++) { if (Level.isTileCollideable(i, j) == true) { tempRect.Width = m_DestinationRectangle.Width; tempRect.Height = m_DestinationRectangle.Height; tempRect.X = j * m_DestinationRectangle.Width; tempRect.Y = i * m_DestinationRectangle.Height; if (j < (m_TileData.GetLength(1) - 1)) { j++; magicRectangleExtentionFunctionWithExtraRecursionForTheHellOfItButNotReallySinceWithoutThisRecursionThenTheCollisionWouldStillBeBrokenAndABrokenCollisionIsntAnyFunForAnyoneUnlessYoureAGhostSinceGhostsDontNeedCollisionButIfAGhostCouldCollideWithThingsThenSaidGhostWouldAlsoApreciateThisFunctionAndAllOfItsGlory(ref tempRect, i, ref j); } CollisionCheck.collisionCheck(objectDestinationRectangle, tempRect, ref temp[y, x]); } x++; } x = 0; y++; } return temp; }
public CollisionType keyCollision(Rectangle playerRect) { CollisionType collision = new CollisionType(); if (CollisionCheck.collisionCheck(playerRect, Destination, ref collision)) { //TODO::add type of object that player collided with } return collision; }
/// <summary> /// the collision data you enter gets changed through /// pass by reference and will hold additional data about the collision /// </summary> /// <param name="rect1"></param> /// <param name="rect2"></param> /// <param name="collisonData"></param> /// <returns>bool</returns> public static bool collisionCheck(Rectangle rect1, Rectangle rect2, ref CollisionType collisonData) { collisonData.clearData(); //get the overlap area Rectangle.Intersect(ref rect1, ref rect2, out collisonData.m_OverlapArea); //if the width and height are bigger than zero it means they collided if (collisonData.m_OverlapArea.Width > 0 && collisonData.m_OverlapArea.Height > 0) { collisonData.m_WasThereACollision = true; if (collisonData.m_OverlapArea.Width > collisonData.m_OverlapArea.Height) { //either top or bottom collision if (rect1.Bottom > rect2.Top && rect1.Center.Y < rect2.Center.Y) { collisonData.m_FirstObjectSideCollided = SideCollided.BOTTOM; collisonData.m_SecondObjectSideCollided = SideCollided.TOP; } else if (rect1.Top < rect2.Bottom && rect1.Center.Y > rect2.Center.Y) { collisonData.m_FirstObjectSideCollided = SideCollided.TOP; collisonData.m_SecondObjectSideCollided = SideCollided.BOTTOM; } } else { //left or right collision if (rect1.Right > rect2.Left && rect1.Center.X < rect2.Center.X) { collisonData.m_FirstObjectSideCollided = SideCollided.RIGHT; collisonData.m_SecondObjectSideCollided = SideCollided.LEFT; } else if (rect1.Left < rect2.Right && rect1.Center.X > rect2.Center.X) { collisonData.m_FirstObjectSideCollided = SideCollided.LEFT; collisonData.m_SecondObjectSideCollided = SideCollided.RIGHT; } } return true; } return false; }
public override void update(GameTime gameTime) { m_LastFacing = m_Facing; float elapsedTime = (float)gameTime.ElapsedGameTime.Milliseconds / 1000; m_Timer += elapsedTime; m_HealthTimer += elapsedTime; m_StarTimer += elapsedTime; m_RechargeTimer += elapsedTime; //Added November 27, 2013 Vector2 oldPos = new Vector2(m_PlayerPosition.X, m_PlayerPosition.Y); //Added November 26, 2013 KeyboardState keyboardState = Keyboard.GetState(); GamePadState gamePadState = GamePad.GetState(PlayerIndex.One); MouseState mouseState = Mouse.GetState(); CollisionType collisionType = new CollisionType(); if (m_Health == 0) { instantDeath(); } if (m_HealthTimer < HEALTH_DELAY_TIMER) { //TODO: RED SCREEN GET HIT } crossState(gameTime); rechargeLaser(gameTime); m_LaserManager.update(gameTime); getInput(keyboardState, gamePadState, mouseState, gameTime); levelBounds(); runState(gameTime); collectedHeart(); collectedStars(); jumpState(gameTime); m_Position = m_PlayerPosition; //Added November 27, 2013 base.update(gameTime); m_PlayerPosition = m_Position; m_collisionRectangle = Destination; //Added November 27,2013 m_Particles.update(gameTime); m_Particles.SpawnPoint = m_PlayerPosition; m_Particles.DominantDirection = new Vector2(oldPos.X - m_PlayerPosition.X, oldPos.Y - m_PlayerPosition.Y); }