private void ComputeCircleAxes(CollisionCircle element) { _circleAxes.Clear(); for (int i = 0; i < Vertices.Count; i++) { Vector2 position = GetPosition(Vertices[i]); Vector2 edge = element.GetCenter() - position; var normal = new Vector2(edge.Y, -edge.X); _circleAxes.Add(normal); } }
private bool Intersects(CollisionCircle element) { float dx = element.GetCenter().X - GetCenter().X; float dy = element.GetCenter().Y - GetCenter().Y; float radii = Radius + element.Radius; if ((dx * dx) + (dy * dy) < radii * radii) { return(true); } return(false); }
private bool Intersects(CollisionCircle element) { ComputeCircleAxes(element); // loop over the axes of this polygon for (int i = 0; i < _axes.Count; i++) { Vector2 axis = _axes[i]; // project both shapes onto the axis Vector2 p1 = this.Project(axis); Vector2 p2 = element.Project(axis); // do the projections overlap? if (!Overlap(p1, p2)) { // then we can guarantee that the shapes do not overlap return(false); } } // if we get here then we know that every axis had overlap on it // so we can guarantee an intersection return(true); }
private bool Intersects(CollisionCircle circle) { float radiusSquared = circle.Radius * circle.Radius; var vertex = GetWorldPosition(Vertices[Vertices.Count - 1]); var circleCenter = circle.GetCenter(); float nearestDistance = float.MaxValue; bool nearestIsInside = false; int nearestVertex = -1; bool lastIsInside = false; for (int i = 0; i < Vertices.Count; i++) { var nextVertex = GetWorldPosition(Vertices[i]); var axis = circleCenter - vertex; float distance = axis.LengthSquared() - radiusSquared; if (distance <= 0) { return(true); } bool isInside = false; var edge = nextVertex - vertex; float edgeLengthSquared = edge.LengthSquared(); if (!edgeLengthSquared.Equals(0)) { float dot = Vector2.Dot(edge, axis); if (dot >= 0 && dot <= edgeLengthSquared) { var projection = vertex + (dot / edgeLengthSquared) * edge; axis = projection - circleCenter; if (axis.LengthSquared() <= radiusSquared) { return(true); } if (edge.X > 0) { if (axis.Y > 0) { return(false); } } else if (edge.X < 0) { if (axis.Y < 0) { return(false); } } else if (edge.Y > 0) { if (axis.X < 0) { return(false); } } else { if (axis.X > 0) { return(false); } } isInside = true; } } if (distance < nearestDistance) { nearestDistance = distance; nearestIsInside = isInside || lastIsInside; nearestVertex = i; } vertex = nextVertex; lastIsInside = isInside; } if (nearestVertex == 0) { return(nearestIsInside || lastIsInside); } return(nearestIsInside); }
private bool Intersects(CollisionCircle element) { ComputeCircleAxes(element); // loop over the axes of this polygon for (int i = 0; i < _axes.Count; i++) { Vector2 axis = _axes[i]; // project both shapes onto the axis Vector2 p1 = this.Project(axis); Vector2 p2 = element.Project(axis); // do the projections overlap? if (!Overlap(p1, p2)) { // then we can guarantee that the shapes do not overlap return false; } } // if we get here then we know that every axis had overlap on it // so we can guarantee an intersection return true; }
private bool Intersects(CollisionCircle circle) { float radiusSquared = circle.Radius * circle.Radius; var vertex = GetWorldPosition(Vertices[Vertices.Count - 1]); var circleCenter = circle.GetCenter(); float nearestDistance = float.MaxValue; bool nearestIsInside = false; int nearestVertex = -1; bool lastIsInside = false; for (int i = 0; i < Vertices.Count; i++) { var nextVertex = GetWorldPosition(Vertices[i]); var axis = circleCenter - vertex; float distance = axis.LengthSquared() - radiusSquared; if (distance <= 0) return true; bool isInside = false; var edge = nextVertex - vertex; float edgeLengthSquared = edge.LengthSquared(); if (!edgeLengthSquared.Equals(0)) { float dot = Vector2.Dot(edge, axis); if (dot >= 0 && dot <= edgeLengthSquared) { var projection = vertex + (dot / edgeLengthSquared) * edge; axis = projection - circleCenter; if (axis.LengthSquared() <= radiusSquared) return true; if (edge.X > 0) { if (axis.Y > 0) return false; } else if (edge.X < 0) { if (axis.Y < 0) return false; } else if (edge.Y > 0) { if (axis.X < 0) return false; } else { if (axis.X > 0) return false; } isInside = true; } } if (distance < nearestDistance) { nearestDistance = distance; nearestIsInside = isInside || lastIsInside; nearestVertex = i; } vertex = nextVertex; lastIsInside = isInside; } if (nearestVertex == 0) return nearestIsInside || lastIsInside; return nearestIsInside; }
protected override void LoadContent() { Sprite = Game.Content.Load<Texture2D>(@"Graphics/Sprites/balls"); Center = new Vector2(Sprite.Height / 2f, Sprite.Height / 2f); CollisionBox = new CollisionCircle(this, Vector2.Zero, Sprite.Height / 2f); int index = (int)(Sprite.Height * _rand.Next((Sprite.Width / Sprite.Height))); _spriteRectangle = new Rectangle(index, 0, Sprite.Height, Sprite.Height); base.LoadContent(); }
private bool Intersects(CollisionCircle element) { float dx = element.GetCenter().X - GetCenter().X; float dy = element.GetCenter().Y - GetCenter().Y; float radii = Radius + element.Radius; if ((dx * dx) + (dy * dy) < radii * radii) { return true; } return false; }
protected override void LoadContent() { base.LoadContent(); Sprite = GameRef.Content.Load<Texture2D>("Graphics/Entities/player"); _bulletSprite = GameRef.Content.Load<Texture2D>("Graphics/Entities/player_bullet"); _hitboxSprite = GameRef.Content.Load<Texture2D>("Graphics/Pictures/player_hitbox"); CollisionBoxes.Add(new CollisionCircle(this, new Vector2(Sprite.Height / 6f, Sprite.Height / 6f), _hitboxRadius/2f)); _shieldSprite = GameRef.Content.Load<Texture2D>("Graphics/Entities/shield"); _shieldOrigin = new Vector2(_shieldSprite.Width / 2f, _shieldSprite.Height / 2f); _shieldCollisionCircle = new CollisionCircle(this, Vector2.Zero, _shieldSprite.Width / 2f); _lifeIcon = GameRef.Content.Load<Texture2D>("Graphics/Pictures/life_icon"); _bulletTimeBarLeft = GameRef.Content.Load<Texture2D>("Graphics/Pictures/gauge_left"); _bulletTimeBarContent = GameRef.Content.Load<Texture2D>("Graphics/Pictures/gauge_middle"); _bulletTimeBarRight = GameRef.Content.Load<Texture2D>("Graphics/Pictures/gauge_right"); if (_shootSound == null) _shootSound = GameRef.Content.Load<SoundEffect>(@"Audio/SE/hit"); if (_deadSound == null) _deadSound = GameRef.Content.Load<SoundEffect>(@"Audio/SE/dead"); }
protected override void LoadContent() { base.LoadContent(); Sprite = Game.Content.Load<Texture2D>("Graphics/Entities/ship"); _bulletSprite = this.Game.Content.Load<Texture2D>("Graphics/Entities/ship_bullet"); _hitboxSprite = this.Game.Content.Load<Texture2D>("Graphics/Pictures/hitbox"); Center = new Vector2(Sprite.Width / 2f, Sprite.Height / 2f); CollisionBox = new CollisionCircle(this, new Vector2(Sprite.Height / 6f, Sprite.Height / 6f), _hitboxRadius/2f); _lifeIcon = Game.Content.Load<Texture2D>("Graphics/Pictures/life"); _bulletTimeBarLeft = Game.Content.Load<Texture2D>("Graphics/Pictures/bulletTimeBarLeft"); _bulletTimeBarContent = Game.Content.Load<Texture2D>("Graphics/Pictures/bulletTimeBarContent"); _bulletTimeBarRight = Game.Content.Load<Texture2D>("Graphics/Pictures/bulletTimeBarRight"); if (_shootSound == null) _shootSound = Game.Content.Load<SoundEffect>(@"Audio/SE/hit"); if (_deadSound == null) _deadSound = Game.Content.Load<SoundEffect>(@"Audio/SE/dead"); float xLag = Config.Resolution.X / 2; if (Config.PlayersNumber > 1) xLag /= 2; _camera = new Camera2D(_viewport, Config.GameArea.X, Config.GameArea.Y, 1f); }