private bool pixelsCollide(ICollidable2D i_Source, Rectangle i_IntersectionBounds) {// TODO: optimized bool intersects = false; Color[] sourcePixels = getPixelsData(i_Source); for (int y = i_IntersectionBounds.Top; y < i_IntersectionBounds.Bottom && !intersects; y++) { for (int x = i_IntersectionBounds.Left; x < i_IntersectionBounds.Right && !intersects; x++) { if (!isTransparent(m_Collidable.Pixels, x, y, m_Collidable.Bounds) && !isTransparent(sourcePixels, x, y, i_Source.Bounds)) { intersects = true; } } } return(intersects); }
private bool checkPixleCollision(ICollidable2D i_Source) { bool collided = false; Rectangle rectangle = findIntersectingRectangle(i_Source.Bounds); for (int i = rectangle.Left; (i < rectangle.Right) && (!collided); i++) { for (int j = rectangle.Top; (j < rectangle.Bottom) && (!collided); j++) { Color sourceColor = i_Source.Pixels[(i - i_Source.Bounds.Left) + ((j - i_Source.Bounds.Top) * i_Source.Bounds.Width)]; Color targetColor = Pixels[(i - Bounds.Left) + ((j - Bounds.Top) * Bounds.Width)]; if (sourceColor.A > 0 && targetColor.A > 0) { collided = true; } } } return(collided); }
public virtual bool CheckCollision(ICollidable i_Source) { bool collided = false; ICollidable2D source = i_Source as ICollidable2D; // *** first checking rectangle intersections *** Rectangle intersection = boundsIntersection(source); collided = intersection != Rectangle.Empty; // *** pixel based collision detection if this || i_Source are ICollidableByPixels *** if (collided && this is ICollidableByPixels) { collided = (this as ICollidableByPixels).PixelBasedCollisionComponent.CheckCollision(source, intersection); } else if (collided && (source is ICollidableByPixels)) { collided = false; } return(collided); }
public override bool CheckCollision(ICollidable i_Source) { bool collided = false; ICollidable2D source = i_Source as ICollidable2D; if (IsAlive && source != null) { SpriteAdapter sprite = i_Source as SpriteAdapter; if (sprite != null) { if (sprite.TeamColor != TeamColor && sprite.IsAlive) { if (sprite.Bounds.Intersects(Bounds)) { collided = !CheckPerPixel || PerPixelCollision(sprite); } } } } return(collided); }
// This method actually erases the relevant colliding pixels (Not needed for all collisions) public void ErasePixelCollision(ICollidable i_Collidable) { ICollidable2D other = i_Collidable as ICollidable2D; Rectangle impactPos = GetImpactRectangle(other); for (int y = impactPos.Y; y < impactPos.Y + impactPos.Height; y++) { for (int x = impactPos.X; x < impactPos.X + impactPos.Width; x++) { int thisCurrentPixelLocation = x - this.Bounds.X + ((y - this.Bounds.Y) * this.Texture.Width); int otherCurrentPixelLocation = x - other.Bounds.X + ((y - other.Bounds.Y) * other.Texture.Width); if (this.Pixels[thisCurrentPixelLocation].A != 0 && other.Pixels[otherCurrentPixelLocation].A != 0) { this.Pixels[thisCurrentPixelLocation].A = 0; } } } this.Texture.SetData(Pixels); }
private Rectangle boundsIntersection(ICollidable2D i_Source) { return(Rectangle.Intersect(this.Bounds, i_Source.Bounds)); }
private bool collisionOn2DPixelLevel(ICollidable2D i_CollisionObject1, ICollidable2D i_CollisionObject2) { Color[] pixelData1 = i_CollisionObject1.GetPixelArray(); Color[] pixelData2 = i_CollisionObject2.GetPixelArray(); int Top = Math.Max(i_CollisionObject1.Bounds.Top, i_CollisionObject2.Bounds.Top); int Bottom = Math.Min(i_CollisionObject1.Bounds.Bottom, i_CollisionObject2.Bounds.Bottom); int Left = Math.Max(i_CollisionObject1.Bounds.Left, i_CollisionObject2.Bounds.Left); int Right = Math.Min(i_CollisionObject1.Bounds.Right, i_CollisionObject2.Bounds.Right); for (int y = Top; y < Bottom; y++) { for (int x = Left; x < Right; x++) { Color colorA = pixelData1[((y - i_CollisionObject1.Bounds.Top) * i_CollisionObject1.Bounds.Width) + (x - i_CollisionObject1.Bounds.Left)]; Color colorB = pixelData2[((y - i_CollisionObject2.Bounds.Top) * i_CollisionObject2.Bounds.Width) + (x - i_CollisionObject2.Bounds.Left)]; if (colorA.A != 0 && colorB.A != 0) { return true; } } } return false; }
protected bool LookForCollidingPixels(ICollidable2D i_Source, bool i_StopAfterFirstDetection) { Func <Color, Color> nulledModificationFunc = null; return(LookForCollidingPixels(i_Source, i_StopAfterFirstDetection, nulledModificationFunc)); }
private bool checkPixelCollision(ICollidable2D i_Source) { const bool v_StopAfterFirstDetection = true; return(LookForCollidingPixels(i_Source, v_StopAfterFirstDetection)); }
private List<Point> getCollidingPixels(ICollidable2D i_OtherCollidable) { List<Point> collidingPixels = new List<Point>(); Rectangle intersectingRectangle = Rectangle.Intersect(this.Bounds, i_OtherCollidable.Bounds); for (int y = intersectingRectangle.Y; y < intersectingRectangle.Y + intersectingRectangle.Height; y++) { for (int x = intersectingRectangle.X; x < intersectingRectangle.X + intersectingRectangle.Width; x++) { if (SpritePixelMatrix.GetPixelByLocation(x, y).A != 0) { if (i_OtherCollidable.IsPixelCollidable) { // code to handle collision between two pixelCollisions (not used in this project) if ((i_OtherCollidable as CollidableSprite).SpritePixelMatrix.GetPixelByLocation(x, y).A != 0) { collidingPixels.Add(new Point(y - (int)Position.Y, x - (int)Position.X)); } } else { collidingPixels.Add(new Point(y - (int)Position.Y, x - (int)Position.X)); } } } } return collidingPixels; }