public Obstacle(Game game, SpriteBatch spriteBatch,Texture2D texture ,FloatRectangle theRectangle, float theInitialRotation) : base(game) { this.spriteBatch = spriteBatch; this.texture = texture; config = new Configuration(theRectangle, theInitialRotation,"obstacle"); }
public Obstacle(Game game, SpriteBatch spriteBatch, Texture2D texture, float x, float y, float width, float height, float theInitialRotation) : base(game) { this.spriteBatch = spriteBatch; this.texture = texture; config = new Configuration(new FloatRectangle(new Vector2(x,y),new Vector2(width,height)), theInitialRotation, "obstacle"); }
public Agent(Game1 game, SpriteBatch spriteBatch, Texture2D texture, float x, float y, float width, float height, float theInitialRotation,string name) : base(game) { this.game = game; this.spriteBatch = spriteBatch; this.texture = texture; config = new Configuration(new FloatRectangle(new Vector2(x, y), new Vector2(width, height)), theInitialRotation,name); this.color = Color.Chocolate; this.dijkstra = null; }
/// <summary> /// Allows the game component to update itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> public override void Update(GameTime gameTime) { // TODO: Add your update code here mauseState = Mouse.GetState(); if (mauseState.X < 0 || mauseState.Y < 0 || mauseState.X > game.graphics.PreferredBackBufferWidth || mauseState.Y > game.graphics.PreferredBackBufferHeight) return; // Mause pencere aralýðý dýþýnda... if (!startConfigLocated && !goalConfigLocated) { config = new Configuration(mauseState.X, mauseState.Y, config.Width, config.Height, config.Rotation); if (!config.isOnObstacle(game.scenario.obstacles)) { if (Keyboard.GetState().IsKeyDown(Keys.T)) config.Rotation += 0.1F; if (mauseState.LeftButton == ButtonState.Pressed) { startConfigLocated = true; startConfig = new Configuration(mauseState.X, mauseState.Y, config.Width, config.Height, config.Rotation); dijkstra.startConfig = startConfig; } } } else if (startConfigLocated && !goalConfigLocated) { config = new Configuration(mauseState.X, mauseState.Y, config.Width, config.Height, config.Rotation); if (!config.isOnObstacle(game.scenario.obstacles)) { if (Keyboard.GetState().IsKeyDown(Keys.T)) config.Rotation += 0.1F; if (mauseState.RightButton == ButtonState.Pressed) { goalConfigLocated = true; goalConfig = new Configuration(mauseState.X, mauseState.Y, config.Width, config.Height, config.Rotation); dijkstra.goalConfig = goalConfig; dijkstra.addStartAndGoalConfigtoConnectedRoadmapList(); dijkstra.getShortestPath(); config = new Configuration(startConfig.X, startConfig.Y, startConfig.Width, startConfig.Height, startConfig.Rotation); } } } else { if (dijkstra.stations.Count != 0) { Vector2 yon = dijkstra.stations.ElementAt(0).CollisionRectangle.position - config.CollisionRectangle.position; float mesafe = Vector2.Distance(dijkstra.stations.ElementAt(0).CollisionRectangle.position, config.CollisionRectangle.position); float aci = dijkstra.stations.ElementAt(0).Rotation - config.Rotation; float donusNormalize = aci / mesafe; yon.Normalize(); this.config.ChangePosition(yon.X, yon.Y); this.config.Rotation += donusNormalize; if (Vector2.Distance(dijkstra.stations.ElementAt(0).CollisionRectangle.position, config.CollisionRectangle.position) < 1) { //config = dijkstra.stations.ElementAt(0); dijkstra.stations.Dequeue(); } if (dijkstra.stations.Count == 0) { this.config = goalConfig; } } //float mesafe = Vector2.Distance(goalConfig.CollisionRectangle.position, config.CollisionRectangle.position); //float aci = goalConfig.Rotation - config.Rotation; //float donusNormalize = aci / mesafe; //yon.Normalize(); //this.config.ChangePosition(yon.X, yon.Y); //this.config.Rotation += donusNormalize; } base.Update(gameTime); }
public float distance(Configuration to) { float xyDistance = Vector2.Distance(this.CollisionRectangle.position, to.CollisionRectangle.position); float rotationDistance = (float)Math.Pow((double)(to.rotation-this.rotation)%180,2); return (float)Math.Sqrt(Math.Pow(xyDistance,2)+Math.Pow(rotationDistance,2)); }
/// <summary> /// Determines if a collision has occurred on an Axis of one of the /// planes parallel to the Rectangle /// </summary> /// <param name="theRectangle"></param> /// <param name="aAxis"></param> /// <returns></returns> private bool IsAxisCollision(Configuration theRectangle, Vector2 aAxis) { //Project the corners of the Rectangle we are checking on to the Axis and //get a scalar value of that project we can then use for comparison List<float> aRectangleAScalars = new List<float>(); aRectangleAScalars.Add(GenerateScalar(theRectangle.UpperLeftCorner(), aAxis)); aRectangleAScalars.Add(GenerateScalar(theRectangle.UpperRightCorner(), aAxis)); aRectangleAScalars.Add(GenerateScalar(theRectangle.LowerLeftCorner(), aAxis)); aRectangleAScalars.Add(GenerateScalar(theRectangle.LowerRightCorner(), aAxis)); //Project the corners of the current Rectangle on to the Axis and //get a scalar value of that project we can then use for comparison List<float> aRectangleBScalars = new List<float>(); aRectangleBScalars.Add(GenerateScalar(UpperLeftCorner(), aAxis)); aRectangleBScalars.Add(GenerateScalar(UpperRightCorner(), aAxis)); aRectangleBScalars.Add(GenerateScalar(LowerLeftCorner(), aAxis)); aRectangleBScalars.Add(GenerateScalar(LowerRightCorner(), aAxis)); //Get the Maximum and Minium Scalar values for each of the Rectangles float aRectangleAMinimum = aRectangleAScalars.Min(); float aRectangleAMaximum = aRectangleAScalars.Max(); float aRectangleBMinimum = aRectangleBScalars.Min(); float aRectangleBMaximum = aRectangleBScalars.Max(); //If we have overlaps between the Rectangles (i.e. Min of B is less than Max of A) //then we are detecting a collision between the rectangles on this Axis if (aRectangleBMinimum <= aRectangleAMaximum && aRectangleBMaximum >= aRectangleAMaximum) { return true; } else if (aRectangleAMinimum <= aRectangleBMaximum && aRectangleAMaximum >= aRectangleBMaximum) { return true; } return false; }
/// <summary> /// Bu metod kullanılıyor ise start ve goal arasında hiçbir engelin olmadığı doğrulanmalıdır. /// </summary> /// <param name="start"></param> /// <param name="goal"></param> public bool isConnectable(Configuration goalConfig,List<Obstacle> obstacleList) { Configuration virtualConfig = new Configuration(this.X, this.Y, this.Width, this.Height, this.rotation); Vector2 direction; float distance, currentDistance, currentAngle, normalizeAngle; while (true) { direction = goalConfig.CollisionRectangle.position - virtualConfig.CollisionRectangle.position; direction.Normalize(); if (float.IsNaN(direction.X)) // Burada bir bug var. İf ile çözüldü düzelt. return false; currentDistance = Vector2.Distance(goalConfig.CollisionRectangle.position, virtualConfig.CollisionRectangle.position); currentAngle = goalConfig.rotation - virtualConfig.rotation; normalizeAngle = currentAngle / currentDistance; virtualConfig.ChangePosition(direction.X, direction.Y); virtualConfig.rotation += normalizeAngle; foreach (Obstacle obs in obstacleList) { if (virtualConfig.Intersects(obs.config)) return false; } distance = Vector2.Distance(virtualConfig.CollisionRectangle.position, goalConfig.CollisionRectangle.position); if (distance < 1) { break; } } return true; }
/// <summary> /// Check to see if two Rotated Rectangls have collided /// </summary> /// <param name="theRectangle"></param> /// <returns></returns> public bool Intersects(Configuration theRectangle) { //Calculate the Axis we will use to determine if a collision has occurred //Since the objects are rectangles, we only have to generate 4 Axis (2 for //each rectangle) since we know the other 2 on a rectangle are parallel. List<Vector2> aRectangleAxis = new List<Vector2>(); aRectangleAxis.Add(UpperRightCorner() - UpperLeftCorner()); aRectangleAxis.Add(UpperRightCorner() - LowerRightCorner()); aRectangleAxis.Add(theRectangle.UpperLeftCorner() - theRectangle.LowerLeftCorner()); aRectangleAxis.Add(theRectangle.UpperLeftCorner() - theRectangle.UpperRightCorner()); //Cycle through all of the Axis we need to check. If a collision does not occur //on ALL of the Axis, then a collision is NOT occurring. We can then exit out //immediately and notify the calling function that no collision was detected. If //a collision DOES occur on ALL of the Axis, then there is a collision occurring //between the rotated rectangles. We know this to be true by the Seperating Axis Theorem foreach (Vector2 aAxis in aRectangleAxis) { if (!IsAxisCollision(theRectangle, aAxis)) { return false; } } return true; }