public bool PixelCollition(Ball other) { Color[] dataA = new Color[tex.Width * tex.Height]; tex.GetData(dataA); Color[] dataB = new Color[other.tex.Width * other.tex.Height]; other.tex.GetData(dataB); int top = Math.Max(hitBox.Top, other.hitBox.Top); int bottom = Math.Min(hitBox.Bottom, other.hitBox.Bottom); int left = Math.Max(hitBox.Left, other.hitBox.Left); int right = Math.Min(hitBox.Right, other.hitBox.Right); for (int y = top; y < bottom; y++) { for (int x = left; x < right; x++) { Color colorA = dataA[(x - hitBox.Left) + (y - hitBox.Top) * hitBox.Width]; Color colorB = dataB[(x - other.hitBox.Left) + (y - other.hitBox.Top) * other.hitBox.Width]; if (colorA.A != 0 && colorB.A != 0) { return true; } } } return false; }
public int CheckCollision(Ball b) { if (active && b.Bounds.Intersects(location)) { active = false; b.Deflection(); return 1; } else { return 0; } }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // TODO: Add your initialization logic here mBackground = new Sprite(); mBall = new Ball(); mWallTop = new Wall(); mWallBottom = new Wall(); player1 = new GameSpace(GameSpace.Player.One); player2 = new GameSpace(GameSpace.Player.Two); powerUp = new PowerUp("powerUpTest"); base.Initialize(); }
public void CollideWithBall(Ball ball) { Vector2 previousBallLocation = ball.Position; SpriteBoxCollision.Corner corner= SpriteBoxCollision.GetRectangleCornerInRectangle(ball.BoundingBox, this.BoundingBox); Vector2 bCorner = new Vector2(); if (corner == SpriteBoxCollision.Corner.None) return; else if (corner == SpriteBoxCollision.Corner.TopLeft) { bCorner = ball.Position; } else if (corner == SpriteBoxCollision.Corner.TopRight) { bCorner = new Vector2(ball.Position.X + ball.Source.Width, ball.Position.Y); } else if (corner == SpriteBoxCollision.Corner.BottomLeft) { bCorner = new Vector2(ball.Position.X, ball.Position.Y + ball.Source.Height); } else if (corner == SpriteBoxCollision.Corner.BottomRight) { bCorner = new Vector2(ball.Position.X + ball.Source.Width, ball.Position.Y + ball.Source.Height); } SpriteBoxCollision.SideCollided side = SpriteBoxCollision.GetSidesCollided(bCorner, this.BoundingBox); if (side.HasFlag(SpriteBoxCollision.SideCollided.Left)) { ball.Direction = new Vector2(Math.Abs(ball.Direction.X) * -1, ball.Direction.Y); ball.Position = new Vector2(ball.Position.X - 10, ball.Position.Y); ball.Speed -= (this.mSpeed *.4f) * mDirection; } else if (side.HasFlag(SpriteBoxCollision.SideCollided.Right)) { ball.Direction = new Vector2(Math.Abs(ball.Direction.X), ball.Direction.Y); ball.Position = new Vector2(ball.Position.X + 10, ball.Position.Y); ball.Speed -= (this.mSpeed * .4f) * mDirection; } if (side.HasFlag(SpriteBoxCollision.SideCollided.Top)) { ball.Direction = new Vector2(ball.Direction.X, Math.Abs(ball.Direction.Y) * -1); ball.Position = new Vector2(ball.Position.X, ball.Position.Y - 10); } else if (side.HasFlag(SpriteBoxCollision.SideCollided.Bottom)) { ball.Direction = new Vector2(ball.Direction.X, Math.Abs(ball.Direction.Y)); ball.Position = new Vector2(ball.Position.X, ball.Position.Y + 10); } soundEffect.Play(); }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // TODO: Add your initialization logic here map = new Map(this, MAP_SIZE, MAP_SIZE); wW = new Werewolf(this, map.getWidth(), map.getHeight()); for (int i = 0; i < 50; i++) { Ball ball = new Ball(this,i); } //sheep = new Sheep(this, 50, 50, map); cam = new Camera(this, wW); //temp = new Vision(this, 5, 5); spriteBatch = new SpriteBatch(GraphicsDevice); //hb = new HealthBar(this, 0, 0); base.Initialize(); }
public virtual bool CircleCollision(Ball other) { return Vector2.Distance(pos, other.pos) < (radius + other.radius); }
protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); Texture2D tempTexture = Content.Load<Texture2D>("bar"); player = new Bar(tempTexture, screenRectangle); tempTexture = Content.Load<Texture2D>("ball"); ball = new Ball(tempTexture, screenRectangle); brickImage = Content.Load<Texture2D>("brick"); font = Content.Load<SpriteFont>("myFont"); soundBGM = Content.Load<SoundEffect>("BGM"); soundPing = Content.Load<SoundEffect>("ping"); soundPingInstance = soundPing.CreateInstance(); soundPingInstance.Volume = 0.5f; soundBGMInstance = soundBGM.CreateInstance(); soundBGMInstance.Volume = 0.75f; soundBGMInstance.IsLooped = true; myUtil.readFile(); StartGame(); }