public bool PPCollisionWith(GameObject g, Texture2D t) { bool rCollision = CollisionWith(g); // Bei false ist keine Kollision if (!rCollision) return false; // Falls einer der Renderer NoRender ist, keine PixelCollision machen if (Renderer.GetType() == typeof(NoRenderer) || g.Renderer.GetType() == typeof(NoRenderer) || Renderer.GetType() == typeof(SimpleRenderer) || g.Renderer.GetType() == typeof(SimpleRenderer)) return true; // Rectangles Rectangle rectA = Rect; Rectangle rectB = g.Rect; // Texture Data Texture2D textA, textB; if (Renderer.GetType() == typeof(AnimationRenderer)) textA = ((AnimationRenderer)Renderer).Textures[((AnimationRenderer)Renderer).Frame]; else textA = ((StaticRenderer)Renderer).Texture; if (g.Renderer.GetType() == typeof(AnimationRenderer)) textB = ((AnimationRenderer)g.Renderer).Textures[((AnimationRenderer)g.Renderer).Frame]; else textB = ((StaticRenderer)g.Renderer).Texture; if (t != null) textA = t; Color[] dataA = new Color[textA.Width * textA.Height]; Color[] dataB = new Color[textB.Width * textB.Height]; textA.GetData(dataA); textB.GetData(dataB); Vector2 originA = new Vector2(textA.Width / 2, textA.Height / 2); Vector2 originB = new Vector2(textB.Width / 2, textB.Height / 2); // Update each block bool personHit = false; // Build the block's transform Matrix transformA = Matrix.CreateTranslation(new Vector3(-originA, 0.0f)) * // Matrix.CreateScale(block.Scale) * would go here Matrix.CreateRotationZ(LocationBehavior.Rotation) * Matrix.CreateTranslation(new Vector3(LocationBehavior.Position, 0.0f)); // Build the block's transform Matrix transformB = Matrix.CreateTranslation(new Vector3(-originB, 0.0f)) * // Matrix.CreateScale(block.Scale) * would go here Matrix.CreateRotationZ(g.LocationBehavior.Rotation) * Matrix.CreateTranslation(new Vector3(g.LocationBehavior.Position, 0.0f)); // Calculate the bounding rectangle of this block in world space Rectangle blockRectangleA = CalculateBoundingRectangle( new Rectangle(0, 0, textA.Width, textA.Height), transformA); // Calculate the bounding rectangle of this block in world space Rectangle blockRectangleB = CalculateBoundingRectangle( new Rectangle(0, 0, textB.Width, textB.Height), transformB); // The per-pixel check is expensive, so check the bounding rectangles // first to prevent testing pixels when collisions are impossible. if (rectA.Intersects(blockRectangleB)) { // Check collision with person if (IntersectPixels(transformA, textA.Width, textA.Height, dataA, transformB, textB.Width, textB.Height, dataB)) { personHit = true; return true; } } return false; }
// *************************************************************************** // Pixelgenaue Collisionsprüfung public bool PPCollisionWith(GameObject g) { return PPCollisionWith(g, null); }
// *************************************************************************** // Prüft kollision mit anderem GameObject public bool CollisionWith(GameObject g) { return g.Rect.Intersects(Rect); }
// *************************************************************************** // Prüft ob ein objekt näher drann ist wie wert public bool DistanceLessThan(GameObject g, float distance) { if (Vector2.Distance(new Vector2(Rect.X, Rect.Y), new Vector2(g.Rect.X, g.Rect.Y)) < distance) return true; else return false; }
// *************************************************************************** // Prüft ob das objekt das andere objekt sehen kann public bool CanSee(GameObject g) { return GameManager.PointSeePoint(LocationBehavior.Position, g.LocationBehavior.Position); }