public void onCollision(GameTime gameTime, PolygonCollisionResult collisionResult, Vector2 player1Velocity) { foreach (MenuButton menuButton in menuButtons) { menuButton.onCollision(gameTime, collisionResult, player1Velocity); } }
public void onCollision(GameTime gameTime, PolygonCollisionResult collisionResult, Vector2 player1Velocity) { foreach (Menu gameMenu in gameMenus) { gameMenu.onCollision(gameTime, collisionResult, player1Velocity); } }
public void onCollision(GameTime gameTime, PolygonCollisionResult collisionResult, Vector2 player1Velocity) { Vector2 position = Properties.getProperty <Vector2>("Position"); player1Velocity = player1Velocity / 2; position += player1Velocity * (float)gameTime.ElapsedGameTime.TotalSeconds + collisionResult.MinimumTranslationVector; Properties.updateProperty <Vector2>("Position", position); }
public void onCollision(GameTime gameTime, PolygonCollisionResult collisionResult, Vector2 player1Velocity) { Vector2 position = this.position; player1Velocity = player1Velocity / 2; position += player1Velocity * (float)gameTime.ElapsedGameTime.TotalSeconds + collisionResult.MinimumTranslationVector; this.position = position; }
/* * public Camera getCameraInGame() * { * Vector2 playerPos = this.Properties.getProperty<Vector2>("Position"); * * return new Camera(Matrix.Identity, new Vector2(Renderer.getInstance.getScreenCenter().X - playerPos.X, * Renderer.getInstance.getScreenCenter().Y - playerPos.Y)); * } */ public void onCollision(GameTime gameTime, PolygonCollisionResult collisionResult, Vector2 velocity) { Vector2 position = Properties.getProperty <Vector2>("Position"); bool hasCollided = Properties.getProperty <bool>("HasCollided"); List <AABB> axisAlignedBoxes = Properties.getProperty <List <AABB> >("AABB"); List <Polygon> polygons = Properties.getProperty <List <Polygon> >("Polygon"); List <Animation> animations = Properties.getProperty <List <Animation> >("Animations"); float drawDepth = Properties.getProperty <float>("DrawDepth"); float depthPosition = Properties.getProperty <float>("DepthPosition"); hasCollided = true; velocity = velocity / 2; velocity = velocity * (float)gameTime.ElapsedGameTime.TotalSeconds + collisionResult.MinimumTranslationVector; position += velocity; Vector2 shadowPos = new Vector2(position.X - 11, position.Y - 8); SceneManager.getInstance.getPlayer1Shadow().Properties.updateProperty <Vector2>("Position", shadowPos); foreach (Polygon polygon in polygons) { polygon.offset(velocity); polygon.buildEdges(); } depthPosition += velocity.Y; drawDepth = depthPosition / (SceneManager.getInstance.getAxisAlignedBox().getPosition().Y + SceneManager.getInstance.getAxisAlignedBox().getHeight()); Properties.updateProperty <Vector2>("Position", position); Properties.updateProperty <bool>("HasCollided", hasCollided); Properties.updateProperty <List <AABB> >("AABB", axisAlignedBoxes); Properties.updateProperty <List <Polygon> >("Polygon", polygons); Properties.updateProperty <float>("DrawDepth", drawDepth); Properties.updateProperty <float>("DepthPosition", depthPosition); Properties.updateProperty <List <Animation> >("Animations", animations); }
// Check if polygon A is going to collide with polygon B for the given velocity public static PolygonCollisionResult intersects(Polygon polygonA, Polygon polygonB, Vector2 velocity) { PolygonCollisionResult result = new PolygonCollisionResult(); result.Intersect = true; result.WillIntersect = true; int edgeCountA = polygonA.getEdges().Count; int edgeCountB = polygonB.getEdges().Count; float minIntervalDistance = float.PositiveInfinity; Vector2 translationAxis = new Vector2(); Vector2 edge; // Loop through all the edges of both polygons for (int edgeIndex = 0; edgeIndex < edgeCountA + edgeCountB; edgeIndex++) { if (edgeIndex < edgeCountA) { edge = polygonA.getEdges()[edgeIndex]; } else { edge = polygonB.getEdges()[edgeIndex - edgeCountA]; } // ===== 1. Find if the polygons are currently intersecting ===== // Find the axis perpendicular to the current edge Vector2 axis = new Vector2(-edge.Y, edge.X); axis.Normalize(); // Find the projection of the polygon on the current axis float minA = 0; float minB = 0; float maxA = 0; float maxB = 0; projectPolygon(axis, polygonA, ref minA, ref maxA); projectPolygon(axis, polygonB, ref minB, ref maxB); // Check if the polygon projections are currentlty intersecting if (intervalDistance(minA, maxA, minB, maxB) > 0) { result.Intersect = false; } // ===== 2. Now find if the polygons *will* intersect ===== // Project the velocity on the current axis // float velocityProjection = axis.Dot(velocity); float velocityProjection = Vector2.Dot(axis, velocity); // Get the projection of polygon A during the movement if (velocityProjection < 0) { minA += velocityProjection; } else { maxA += velocityProjection; } // Do the same test as above for the new projection float iDistance = intervalDistance(minA, maxA, minB, maxB); if (iDistance > 0) { result.WillIntersect = false; } // If the polygons are not intersecting and won't intersect, exit the loop if (!result.Intersect && !result.WillIntersect) { break; } // Check if the current interval distance is the minimum one. If so store // the interval distance and the current distance. // This will be used to calculate the minimum translation vector iDistance = Math.Abs(iDistance); if (iDistance < minIntervalDistance) { minIntervalDistance = iDistance; translationAxis = axis; Vector2 d = polygonA.getCenter() - polygonB.getCenter(); if (Vector2.Dot(d, translationAxis) < 0) { translationAxis = -translationAxis; } } } // The minimum translation vector can be used to push the polygons appart. // First moves the polygons by their velocity, then move polygonA by MinimumTranslationVector. if (result.WillIntersect) { result.MinimumTranslationVector = translationAxis * minIntervalDistance; } return(result); }
private void checkCollisions(GameTime gameTime) { List <Polygon> player1Polygons = player1.Properties.getProperty <List <Polygon> >("Polygon"); List <AABB> player1AABB = player1.Properties.getProperty <List <AABB> >("AABB"); Vector2 player1Velocity = player1.Properties.getProperty <Vector2>("Velocity"); //Check collision for all middle objects foreach (MiddleObject middleObject in middleObjects) { CollisionType collisionType = middleObject.Properties.getProperty <CollisionType>("CollisionType"); if (collisionType == CollisionType.POLYGON) { List <Polygon> middleObjectPolygons = middleObject.Properties.getProperty <List <Polygon> >("Polygon"); foreach (Polygon player1Polygon in player1Polygons) { foreach (Polygon middleObjectPolygon in middleObjectPolygons) { PolygonCollisionResult collisionResult = CollisionManager.intersects(player1Polygon, middleObjectPolygon, player1Velocity * (float)gameTime.ElapsedGameTime.TotalSeconds); if (collisionResult.WillIntersect) { player1.onCollision(gameTime, collisionResult, player1Velocity); camera.onCollision(gameTime, collisionResult, player1Velocity); middleObject.onCollision(); menuManager.onCollision(gameTime, collisionResult, player1Velocity); hudManager.onCollision(gameTime, collisionResult, player1Velocity); fontManager.onCollision(gameTime, collisionResult, player1Velocity); collision = true; break; } else { player1.onNotCollision(); middleObject.onNotCollision(); } } if (collision) { break; } } } if (collision) { collision = false; break; } /* * else if (collisionType == CollisionType.AABB) * { * //Console.WriteLine("Type is AABB"); * List<AABB> staticObjectAABB = staticObject.Properties.getProperty<List<AABB>>("AABB"); * * foreach (AABB player1Box in player1AABB) * { * foreach (AABB staticBox in staticObjectAABB) * { * if (CollisionManager.intersects(player1Box, staticBox)) * { * Console.WriteLine("AABB Collision: " + collision); * player1.onCollision(collisionResult, gameTime); * camera.setPosition(camera.getOldPosition()); * doAction(player1, staticObject); * * jumpOver = true; * break; * } * } * if (jumpOver) * { * jumpOver = false; * break; * } * } * } * */ } //Check collision for all background objects foreach (BackgroundObject backgroundObject in backgroundObjects) { CollisionType collisionType = backgroundObject.Properties.getProperty <CollisionType>("CollisionType"); if (collisionType == CollisionType.POLYGON) { List <Polygon> backgroundObjectPolygons = backgroundObject.Properties.getProperty <List <Polygon> >("Polygon"); foreach (Polygon player1Polygon in player1Polygons) { foreach (Polygon backgroundObjectPolygon in backgroundObjectPolygons) { PolygonCollisionResult collisionResult = CollisionManager.intersects(player1Polygon, backgroundObjectPolygon, player1Velocity * (float)gameTime.ElapsedGameTime.TotalSeconds); if (collisionResult.WillIntersect) { backgroundObject.onCollision(); collision = true; break; } else { backgroundObject.onNotCollision(); } } if (collision) { break; } } } if (collision) { collision = false; break; } } /* * foreach (Polygon player1Polygon in player1Polygons) * { * if (CollisionManager.checkWallCollision(player1Polygon)) * { * player1.onCollision(); * camera.setPosition(camera.getOldPosition()); * break; * } * } * */ /* * foreach (AABB player1Box in player1AABB) * { * if (CollisionManager.checkWallCollision(player1Box)) * { * player1.onCollision(); * camera.setPosition(camera.getOldPosition()); * break; * } * } * */ }
public void onCollision(GameTime gameTime, PolygonCollisionResult collisionResult, Vector2 player1Velocity) { inventoryHud.onCollision(gameTime, collisionResult, player1Velocity); questHud.onCollision(gameTime, collisionResult, player1Velocity); }