Whats a good way to do Collision with 2D Rectangles? can someone give me a tip?
Posted
by
Javier
on Game Development
See other posts from Game Development
or by Javier
Published on 2012-03-15T19:58:27Z
Indexed on
2012/03/19
18:15 UTC
Read the original article
Hit count: 259
XNA
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
namespace BreakOut
{
class Field
{
public static Field generateField()
{
List<Block> blocks = new List<Block>();
for (int j = 0; j < BlockType.BLOCK_TYPES.Length; j++)
for (int i = 0; i < (Game1.WIDTH / Block.WIDTH); i++)
{
Block b = new Block(BlockType.BLOCK_TYPES[j], new Vector2(i * Block.WIDTH, (Block.HEIGHT + 2) * j + 5));
blocks.Add(b);
}
return new Field(blocks);
}
List<Block> blocks;
public Field(List<Block> blocks)
{
this.blocks = blocks;
}
public void Update(GameTime gameTime, Ball b)
{
List<Block> removals = new List<Block>();
foreach (Block o in blocks)
{
if (o.BoundingBox.Intersects(new Rectangle((int)b.pos.X, (int)b.pos.Y, Ball.WIDTH, Ball.HEIGHT))) //collision with blocks
{
removals.Add(o);
}
}
foreach(Block o in removals)
blocks.Remove(o); //removes the blocks, but i need help hitting one at a time
}
public void Draw(GameTime gameTime)
{
foreach (Block b in blocks)
b.Draw(gameTime);
}
}
}
My problem is that My collision in this sucks. I'm trying to add collision with a ball and hitting against a block and then one of the blocks dissapear.
The problem i'm having is: When the ball hits the block, it removes it all in one instance.
Please people don't be mean and say mean answers to me, im just in highschool, still a nooby and trying to learn more c#/XNA..
© Game Development or respective owner