protected FlyingGameObject(Collidable drawObject, Vector2 position, float direction, float speed, float maxSpeed, float acceleration, float maxAcceleration, float maxAngularSpeed) : base(position, direction) { this.speed = speed; this.maxSpeed = maxSpeed; this.maxAngularSpeed = maxAngularSpeed; this.acceleration = acceleration; this.maxAcceleration = maxAcceleration; this.collidable = drawObject; }
public Boolean CollidesWith(Vector2 position, float rotation, Collidable other, Vector2 otherPosition, float otherRotation) { Rectangle tb = this.BoundingRectangle(position, rotation); Rectangle ob = other.BoundingRectangle(otherPosition, otherRotation); Circle thisCirlce = Circle.CreateBounding(tb); Circle otherCirlce = Circle.CreateBounding(ob); if (thisCirlce.Intersects(otherCirlce) && tb.Intersects(ob)) { Color[] thisTextureData = loadedTexture.Data; Color[] otherTextureData = other.loadedTexture.Data; if (MyIntersectPixels(this, other, position, rotation, otherPosition, otherRotation)) { return true; } } return false; }
//This one is better because it only checks the part the bounding rectangeles that intersect instead of the whole texture private static bool MyIntersectPixels(Collidable d1, Collidable d2, Vector2 position1, float rotation1, Vector2 position2, float rotation2) { Rectangle d1Bound = d1.BoundingRectangle(position1, rotation1); Rectangle d2Bound = d2.BoundingRectangle(position2, rotation2); Rectangle intersectArea; Rectangle.Intersect(ref d1Bound, ref d2Bound, out intersectArea); Matrix inversTransform1 = Matrix.Invert(d1.GetWorldTransformation(position1, rotation1)); Matrix inversTransform2 = Matrix.Invert(d2.GetWorldTransformation(position2, rotation2)); Color[] data1 = d1.LoadedTexture.Data; Color[] data2 = d2.LoadedTexture.Data; //randomly selecting a pixels to check instead of iterating through rows would improve performance for (int worldX = intersectArea.X; worldX < intersectArea.X + intersectArea.Width; worldX++) { for (int worldY = intersectArea.Y; worldY < intersectArea.Y + intersectArea.Height; worldY++) { Vector3 pos = new Vector3(worldX, worldY, 0); Vector3 texture1Pos = Vector3.Transform(pos, inversTransform1); Vector3 texture2Pos = Vector3.Transform(pos, inversTransform2); int x1 = (int)Math.Round(texture1Pos.X); int y1 = (int)Math.Round(texture1Pos.Y); int x2 = (int)Math.Round(texture2Pos.X); int y2 = (int)Math.Round(texture2Pos.Y); if (0 <= x1 && x1 < d1.LoadedTexture.Texture.Width && 0 <= y1 && y1 < d1.LoadedTexture.Texture.Height && 0 <= x2 && x2 < d2.LoadedTexture.Texture.Width && 0 <= y2 && y2 < d2.LoadedTexture.Texture.Height) { Color color1 = data1[x1 + y1 * d1.LoadedTexture.Texture.Width]; Color color2 = data2[x2 + y2 * d2.LoadedTexture.Texture.Width]; // If both pixels are not completely transparent, if (color1.A != 0 && color2.A != 0) { // then an intersection has been found return true; } } } } return false; }
public Boolean CollidesWith(Collidable other) { collidable.Position = this.Position; collidable.Rotation = this.Direction; return this.collidable.CollidesWith(other); }
public Mine(Vector2 position) : base(position, 0) { collidable = new Collidable(Textures.Mine, position, Color.Black, 0, new Vector2(25, 25), (float).5); }
public Ship(Vector2 position, Collidable drawable, float maxSpeed) : base(drawable, position, 0, new Vector2(0), maxSpeed, 500, 1.0f) { }
// All ships have a position and a direction (speed). public Ship(Vector2 position, Collidable drawable, float maxSpeed) : base(drawable, position, 0, 00.0f, maxSpeed, 0, 1200, 2.0f) { }
public Boolean CollidesWith(Collidable other) { //return Vector2.Distance(other.position, position) < 200; Rectangle tb = this.BoundingRectangle(); Rectangle ob = other.BoundingRectangle(); Circle thisCirlce = Circle.CreateBounding(tb); Circle otherCirlce = Circle.CreateBounding(ob); if (thisCirlce.Intersects(otherCirlce) && tb.Intersects(ob)) { Color[] thisTextureData = texture.Data; Color[] otherTextureData = other.texture.Data; /*if (IntersectPixels(this.GetWorldTransformation(), this.Texture.Width, this.Texture.Height, thisTextureData, other.GetWorldTransformation(), other.Texture.Width, other.Texture.Height, otherTextureData)) //if(MyIntersectPixels(this, other)) { return true; }*/ if (MyIntersectPixels(this, other)) { return true; } } return false; }