c# - creating rectangles for each side of an object? -


i'm building breakout type game , i'm having little issue collision detection. how should create rectangle(s), do 1 rectangle per block how detect side has been hit, or do 4 rectangles each side of block , base if statement around them.

i tried create 4 rectangles per block, 1 top, bottom etc etc couldn't correct. heres code see if can work out best way handle it.

brick class:

class bricks {     texture2d redbrickimg;     texture2d bluebrickimg;     texture2d greenbrickimg;     texture2d pinkbrickimg;     texture2d aquabrickimg;      /*rectangle aquabrickrectangle;     rectangle redbrickrectangle;     rectangle bluebrickrectangle;     rectangle greenbrickrectangle;     rectangle pinkbrickrectangle;      */     rectangle[,] tophit = new rectangle[12,12];     rectangle[,] bottomhit = new rectangle[12,12];     rectangle[,] righthit = new rectangle[12,12];     rectangle[,] lefthit = new rectangle[12,12];      int[,] redbrickxpos = new int [12,12];     int[,] redbrickypos = new int [12,12];     int[,] colourbrick  = new int[12, 12];      public bricks(         texture2d redbricks,         texture2d bluebricks,         texture2d greenbricks,         texture2d pinkbricks,         texture2d aquabricks         )     {         redbrickimg  = redbricks;         bluebrickimg = bluebricks;         greenbrickimg = greenbricks;         pinkbrickimg = pinkbricks;         aquabrickimg = aquabricks;     }      public void initialize()     {         (int j = 0; j < 12; j++)         {              (int = 0; < 12; i++)             {                 redbrickxpos[j,i] = 1 + * redbrickimg.width;                 redbrickypos[j,i] = 1 + j * redbrickimg.height;                  colourbrick[j,i] = j/2;             }                         }     }      public void update()     {     }      public void draw(spritebatch spritebatch)     {         (int j = 0; j < 12; j++)         {             (int = 0; < 12; i++)             {                 if (colourbrick[j, i] == 0)                 {                     spritebatch.draw(redbrickimg, new vector2(redbrickxpos[j, i], redbrickypos[j, i]),color.white);                 }                 else if (colourbrick[j, i] == 1)                 {                     spritebatch.draw(bluebrickimg, new vector2(redbrickxpos[j, i], redbrickypos[j, i]),color.white);                 }                 else if (colourbrick[j, i] == 2)                 {                     spritebatch.draw(greenbrickimg, new vector2(redbrickxpos[j, i], redbrickypos[j, i]), color.white);                 }                 else if (colourbrick[j, i] == 3)                 {                     spritebatch.draw(pinkbrickimg, new vector2(redbrickxpos[j, i], redbrickypos[j, i]),color.white);                 }                 else if (colourbrick[j, i] == 4)                 {                     spritebatch.draw(aquabrickimg, new vector2(redbrickxpos[j, i], redbrickypos[j, i]), color.white);                 }             }         }     } } 

my main class:

public class breakout : microsoft.xna.framework.game {     graphicsdevicemanager graphics;     spritebatch spritebatch;     texture2d backgroundimg;     bricks bricks;     paddle paddle;     gameball gameball;     bool iskeyleft = false;     bool iskeyright = false;     bool flag;     int moveby;     float ballx;     float bally;      public breakout()     {         graphics = new graphicsdevicemanager(this);          graphics.preferredbackbufferwidth = 960;         graphics.preferredbackbufferheight = 768;         graphics.applychanges();         content.rootdirectory = "content";     }      protected override void initialize()     {         base.initialize();         bricks.initialize();         paddle.initialize();         gameball.initialize();     }      /// <summary>     /// loadcontent called once per game , place load     /// of content.     /// </summary>     protected override void loadcontent()     {         // create new spritebatch, can used draw textures.         spritebatch = new spritebatch(graphicsdevice);         backgroundimg = content.load<texture2d>("starfield");         bricks = new bricks(             content.load<texture2d>("red brick"),             content.load<texture2d>("brickblue"),             content.load<texture2d>("greenbrick"),             content.load<texture2d>("pinkbrick"),             content.load<texture2d>("aquabrick")             );          paddle = new paddle(content.load<texture2d>("paddle"),              new rectangle(0, 0, 110, 30),iskeyleft,iskeyright);          gameball = new gameball(content.load <texture2d>("ball"),              new rectangle(0, 0, 60, 60));          ismousevisible = true;          // todo: use this.content load game content here     }      protected override void unloadcontent()     {         // todo: unload non contentmanager content here     }      /// <summary>     /// allows game run logic such updating world,     /// checking collisions, gathering input, , playing audio.     /// </summary>     /// <param name="gametime">provides snapshot of timing values.</param>     protected override void update(gametime gametime)     {         // allows game exit         if (gamepad.getstate(playerindex.one).buttons.back == buttonstate.pressed)             this.exit();          // call update on paddle          moveby =  paddle.update();         flag = gameball.update();        if (flag == false)        {            gameball.moveball(moveby);        }          // todo: add update logic here         // mainmenu = new mainmenu(mainmenuison, content.load<texture2d>("option_menu"),         content.load<texture2d>("start_button"), content.load<texture2d>("exit_button"));          base.update(gametime);     }      protected override void draw(gametime gametime)     {         graphicsdevice.clear(color.cornflowerblue);          base.draw(gametime);         spritebatch.begin();         spritebatch.draw(backgroundimg, vector2.zero, color.white);         bricks.draw(spritebatch);         paddle.draw(spritebatch);         gameball.draw(spritebatch);          spritebatch.end();     } } 

you left out interesting part of code you're attempting collision test.

i go 1 rect brick (and thereby 1 collision test per brick) , if passes need figure out side hit. can rule out 2 sides direction of gameball (you cannot hit "backside"). remaining 2 sides check if "front" corner on left or right of current line of movement.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -