private bool Intersects(CollisionConvexPolygon element) { // Loop over the axes of this polygon for (var i = 0; i < _axes.Count; i++) { var axis = _axes[i]; // Project both shapes onto the axis var p1 = Project(axis); var p2 = element.Project(axis); // Do the projections overlap? if (!Overlap(p1, p2)) { // Then we can guarantee that the shapes do not overlap return(false); } } // Loop over element polygon's axes List <Vector2> axes = element.GetAxes(); for (int i = 0; i < axes.Count; i++) { Vector2 axis = axes[i]; // Project both shapes onto the axis Vector2 p1 = 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); }
public Bullet(DnK game, Texture2D sprite, Vector2 position, Vector2 direction, float velocity) : base(game, sprite, position, direction, velocity) { Rotation = 0; Center = new Vector2(Sprite.Width / 2f, Sprite.Height / 2f); _distance = 0; WaveMode = false; List<Point> vertices = new List<Point>() { new Point(0, 0), new Point(sprite.Width, 0), new Point(sprite.Width, sprite.Height), new Point(0, sprite.Height), }; CollisionBox = new CollisionConvexPolygon(this, Vector2.Zero, vertices); Power = Improvements.ShootPowerData[PlayerData.ShootPowerIndex].Key; }
private bool Intersects(CollisionConvexPolygon element) { ComputeAxes(); // 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; } } // loop over element polygon's axes List<Vector2> axes = element.GetAxes(); 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(CollisionConvexPolygon element) { return(element.Intersects(this)); }
private void Split(CollisionConvexPolygon box) { var newPolygonShape = _structure.Split(box); var center = box.GetCenterInWorldSpace(); // TODO: Part is dead? // A boss part is dead when its center is dead? // or when the number of sub-parts is less than a number? if (center.X > (Size.X / 2f - 2 * _step) + Position.X - Origin.X && center.X < (Size.X / 2f + 2 * _step) + Position.X - Origin.X) { TakeDamage(99999); } else { var boxLocalPosition = box.GetLocalPosition(); // Left (1) or right (-1) part? var factor = (boxLocalPosition.X > Origin.X) ? 1 : -1; // If the break out part is not large enough => we don't create another part if (newPolygonShape.Vertices != null && newPolygonShape.GetArea() > Config.MinBossPartArea) { var bossPart = new BossPart( GameRef, _bossRef, _players, _moverManager, null, _color, _health, 0, 25f, null, newPolygonShape ); bossPart.Initialize(); var bossPartSize = newPolygonShape.GetSize(); var boxWorldPosition = box.GetWorldPosition(); // Compute new boss part's world position var worldPosition = Vector2.Zero; worldPosition.Y = boxWorldPosition.Y - boxLocalPosition.Y; // Left part if (factor == 1) worldPosition.X = boxWorldPosition.X - bossPartSize.X; // Right part else if (factor == -1) worldPosition.X = boxWorldPosition.X + _step; // Update world position according to parent rotation bossPart.Scale = Scale; bossPart.Rotation = Rotation; var newLocalPosition = Vector2.Zero; newLocalPosition.X = (Position.X - Origin.X) + (boxLocalPosition.X); newLocalPosition.Y = (Position.Y - Origin.Y); if (factor == -1) newLocalPosition.X -= bossPartSize.X; else newLocalPosition.X += _step; var newOrigin = newPolygonShape.GetCenter(); newLocalPosition += newOrigin; var rotationOrigin = Position; newLocalPosition = Vector2.Transform(newLocalPosition - rotationOrigin, Matrix.CreateRotationZ(Rotation)) + rotationOrigin; bossPart.Origin = newOrigin; bossPart.Position = newLocalPosition; _bossRef.Parts.Add(bossPart); // Give to this new BossPart an impulsion to (pseudo) random direction due to explosion var random = (float)(GameRef.Rand.NextDouble() * (1f - 0.75f)) + 0.75f; bossPart.ApplyImpulse(new Vector2(factor, random * factor), new Vector2(random / 5f)); ApplyImpulse(new Vector2(-factor, random * -factor), new Vector2(random / 5f)); } // Remove destroyed bounding boxes if (factor == -1) CollisionBoxes.RemoveAll(bb => bb.GetCenter().X < boxLocalPosition.X); else CollisionBoxes.RemoveAll(bb => bb.GetCenter().X > boxLocalPosition.X); // This is the bounding that the player has destroyed, so we remove it from the list CollisionBoxes.Remove(box); } }
// Create multiple collision boxes from BossStructure private void ComputeCollisionBoxes() { CollisionBoxes.Clear(); _collisionBoxesHp.Clear(); // TODO: Handle the case where there is 2 vertices with Y = 0 var vertices = _structure.GetVertices(); var bottomVertices = new List<Vector2>(); // the lowest vertex for each step var topVertices = new List<Vector2>(); // the highest vertex for each step var currentStep = 0f; for (var i = 0; i < vertices.Length; i++) { // Bottom part if (vertices[i].Y >= 0) { if (vertices[i].X > currentStep) { bottomVertices.Add(vertices[i - 1]); bottomVertices.Add(vertices[i]); } } // Top part else { if (vertices[i].X < currentStep) { topVertices.Add(vertices[i - 1]); topVertices.Add(vertices[i]); } } currentStep = vertices[i].X; } if (bottomVertices.Count != topVertices.Count) { // Left part if (bottomVertices.First().Y.Equals(0f)) { topVertices.Add(vertices[vertices.Length - 1]); topVertices.Add(bottomVertices.First()); topVertices.Reverse(); } // Right part else if (bottomVertices.Last().Y.Equals(0f)) { topVertices.Reverse(); topVertices.Add(bottomVertices.Last()); } } else topVertices.Reverse(); if (bottomVertices.Count != topVertices.Count) return; for (var i = 1; i < bottomVertices.Count; i += 2) { var boxVertices = new List<Vector2> { bottomVertices[i], bottomVertices[i - 1], topVertices[i - 1], topVertices[i] }; var collisionBox = new CollisionConvexPolygon(this, Vector2.Zero, boxVertices); CollisionBoxes.Add(collisionBox); _collisionBoxesHp.Add(collisionBox, 100f); } }
public PolygonShape Split(CollisionConvexPolygon collisionBox) { var collisionBoxCenter = collisionBox.GetCenter(); var destroyAll = false; /* // TODO: This structure is dead only if its area is less than a specific number, not because its center is destroyed // Structure is dead if (collisionBoxCenter.X > (_size.X / 2f - 2 * _step) + _parent.Position.X - _parent.Origin.X && collisionBoxCenter.X < (_size.X / 2f + 2 * _step) + _parent.Position.X - _parent.Origin.X) { destroyAll = true; } */ // When we retrieve vertices, we need to transform them from world to local var newPolygonShapeVertices = new List<Vector2>(); var newVertices = _polygonShape.Vertices.ToList(); if (!destroyAll) { // The part to remove is at left if (collisionBoxCenter.X < _size.X / 2f) { var leftVertices = newVertices.FindAll(vertex => vertex.X <= collisionBoxCenter.X); newPolygonShapeVertices.AddRange(leftVertices); foreach (var vertex in leftVertices) { newVertices.Remove(vertex); } /* foreach (var box in CollisionBoxes) { var center = box.GetCenter(); if (center.X <= minX + _parent.Position.X - _parent.Origin.X) { ParticleExplosion(center); toDelete.Add(box); } } foreach (var collisionElement in toDelete) { var box = (CollisionConvexPolygon) collisionElement; CollisionBoxes.Remove(box); _leftCollisionBoxes.Remove(box); } */ } // The part to remove is at right else if (collisionBoxCenter.X > _size.X / 2f) { var rightVertices = newVertices.FindAll(vertex => vertex.X >= collisionBoxCenter.X); newPolygonShapeVertices.AddRange(rightVertices); foreach (var vertex in rightVertices) newVertices.Remove(vertex); /* foreach (CollisionElement box in CollisionBoxes) { var center = box.GetCenter(); if (center.X >= maxX + _parent.Position.X - _parent.Origin.X) { ParticleExplosion(center); toDelete.Add(box); } } foreach (var collisionElement in toDelete) { var box = (CollisionConvexPolygon) collisionElement; CollisionBoxes.Remove(box); _rightCollisionBoxes.Remove(box); } */ } } /* else { foreach (var center in CollisionBoxes.Select(box => box.GetCenter())) { ParticleExplosion(center); } _leftCollisionBoxes.Clear(); _rightCollisionBoxes.Clear(); CollisionBoxes.Clear(); } */ /* var vertices = new List<Vector2>(); vertices.AddRange(_bottomLeftVertices); vertices.AddRange(_bottomRightVertices); vertices.AddRange(_topRightVertices); vertices.AddRange(_topLeftVertices); */ _polygonShape.UpdateVertices(newVertices.ToArray()); /* if (_leftCollisionBoxes.Contains(collisionBox)) _leftCollisionBoxes.Remove(collisionBox); else if (_rightCollisionBoxes.Contains(collisionBox)) _rightCollisionBoxes.Remove(collisionBox); CollisionBoxes.Remove(collisionBox); */ var newPolygonShapeVerticesArray = newPolygonShapeVertices.ToArray(); if (newPolygonShapeVertices.Count > 0) { var newVerticesOriginPosition = newPolygonShapeVertices[0]; for (int i = 0; i < newPolygonShapeVerticesArray.Length; i++) { newPolygonShapeVerticesArray[i].X -= newVerticesOriginPosition.X; } } else newPolygonShapeVerticesArray = null; return new PolygonShape(_gameRef, newPolygonShapeVerticesArray); }
protected override void LoadContent() { Sprite = Game.Content.Load<Texture2D>(@"Graphics/Entities/enemy"); _healthBar = Game.Content.Load<Texture2D>(@"Graphics/Pictures/pixel"); Center = new Vector2(Sprite.Width / 2f, Sprite.Height / 2f); // Audio if (_deadSound == null) _deadSound = Game.Content.Load<SoundEffect>(@"Audio/SE/boss_dead"); List<Point> vertices = new List<Point>() { new Point(0, (int)(Sprite.Height / 2.74f)), new Point(Sprite.Width / 2, 0), new Point(Sprite.Width, (int)(Sprite.Height / 2.74f)), new Point(Sprite.Width / 2, Sprite.Height) }; CollisionBox = new CollisionConvexPolygon(this, Vector2.Zero, vertices); Position = new Vector2( Config.GameArea.X / 2f, Config.GameArea.Y / 2f); //Get all the xml files foreach (var source in Directory.GetFiles(@"Content\XML", "*.xml", SearchOption.AllDirectories)) { //store the name _patternNames.Add(source); //load the pattern BulletPattern pattern = new BulletPattern(); pattern.ParseXML(source); _myPatterns.Add(pattern); } _currentPattern = GameplayScreen.Rand.Next(0, _patternNames.Count); AddBullet(true); base.LoadContent(); }
private bool Intersects(CollisionConvexPolygon element) { return element.Intersects(this); }