Help, collision check alive objects |
macrofeet
|
Simple 2d Bounding Box collision detection I'm using on the following game
http://gamejolt.com/games/arcade/asteroids-flackroid-hd/52501/ bool GameObject::CollChk(GameObject B, int AReductionX,int AReductionY,int BReductionX,int BReductionY ) { //The sides of the rectangles int LeftA, LeftB; int RightA, RightB; int TopA, TopB; int BottomA, BottomB; ///CheckCol = 1; //switch to show hit points //square Object1 LeftA = XPos+AReductionX; RightA = XPos+LImageLength-AReductionX; TopA= YPos+AReductionY; BottomA = YPos+LImageHeight-AReductionY; //Square Object2 LeftB = B.XPos+BReductionX; RightB = B.XPos+ B.LImageLength-BReductionX; TopB = B.YPos+BReductionY; BottomB = B.YPos+ B.LImageHeight-BReductionY; if( (BottomA <= TopB) || (TopA >= BottomB) || (RightA <= LeftB) || (LeftA >= RightB) ) return false; else return true; } In the following I check collision and move various objects against each other void MoveObjects() { for (int Q = 0; Q <= PtCals; Q++) if (PartS[Q].IsAlive) {PartS[Q].Move(); if (PBullet.EventTimer(PBullet.ALiveDuration)) PBullet.IsAlive=false;} //move particals LevelImage.apply_surfacing(); LevlNumb.apply_surfacing(); if (PBullet.IsAlive) { PBullet.Move(); if (PBullet.EventTimer(PBullet.ALiveDuration)) PBullet.IsAlive=false;}// check if ship is firing update bullet location if (LevelBreak.IsAlive) { if (Mix_Playing(2)==false) Mix_PlayChannel( 2, LevelUp, 0 ); LevelBreak.apply_surfacing(); if (LevelBreak.EventTimer(1000)) { LevelBreak.IsAlive=false; Shipshield.IsAlive=false; } } if (Enterprise.IsAlive) { ShipHealth.SetLocation(Enterprise.XPos-35,Enterprise.YPos+20); if (Enterprise.Movement()=='T') Thurst(); if (Shipshield.IsAlive) Shipshield.SetLocation(Enterprise.XPos,Enterprise.YPos); Enterprise.Move(); Enterprise.LoopObjectOnScreen(); // loop player ship around screen } EndLev=1; //Get ready for end of level if no Large asteroids are alive that is check to see... for (int i = 0; i < lvlnum; i++) if (Ast[i].IsAlive) // check from 1 to amount of created (Large Asteroids Check) if the large Asteroid [i] is alive { Ast[i].LoopObjectOnScreen(); // if off edge of screen loop round Ast[i].loopspritesheet(); // move on spritesheet and if at end reset/loop to start Ast[i].Move(); // set the Asteroid type fire ice rock change to the correct texture EndLev=0; // it is not the end of the level at least 1 asteroid is left alive dont end level if(Ast[i].CollChk(Enterprise,15,20,20,20)) //if a roid hit player or player hit roid blow up { Enterprise.IsAlive=false; // player ship dead Ast[i].IsAlive=false; // asteroid dead Explode[1].SetLocation(Ast[i].XPos,Ast[i].YPos); //set explode location Explode[1].SetAlive(); Mix_PlayChannel( -1, LargeExp , 0 ); Explode[0].SetLocation(Enterprise.XPos,Enterprise.YPos); //set explode location Explode[0].SetAlive(); } if (Shipshield.IsAlive) // if shield active { if(Ast[i].CollChk(Shipshield,0,0,0,0)) //if a roid hit player shield or player hit roid blow up { Shipshield.IsAlive=false; // player ship dead Ast[i].IsAlive=false; // asteroid dead Explode[1].SetLocation(Ast[i].XPos,Ast[i].YPos); //set explode location Explode[1].SetAlive(); Mix_PlayChannel( -1, LargeExp , 0 ); SaveAstNum=i; //record the roid number the one that blew up SpawnObjects(2); SpawnObjects(3); } } if (PBullet.IsAlive) //if bullet is active/fired { if (Ast[i].CollChk(PBullet,10,12,0,0)) // check if bullet hit the large Asteroid if so.. { if (Ast[i].Hit_Points > 0) Ast[i].Hit_Points--; //asteroid has been hit Mix_PlayChannel( -1, SmallExp, 0 ); //play hit sound PBullet.IsAlive=false; // kill bullet if (Ast[i].Hit_Points == 0)// if asteroid has no health left kill { Ast[i].IsAlive = false; // kill roid Explode[1].SetLocation(Ast[i].XPos, Ast[i].YPos); //Set blowup location Explode[1].IsAlive = true; // begin explode SaveAstNum = i; //record the roid number the one that blew up SpawnObjects(2); SpawnObjects(3); Mix_PlayChannel( -1, LargeExp , 0 ); } } } } for (int Q = 0; Q <= AstSmNum; Q++) // Small Asteroids Check { if (AstS[Q].IsAlive) { AstS[Q].LoopObjectOnScreen(); // loop small asteroid on playfield if reach edge AstS[Q].Move(); // move the sprite AstS[Q].apply_surfacing(); if(AstS[Q].CollChk(Enterprise,0,0,30,30)) //if a Small asteroid hit player or player hit asteroid blow up { Enterprise.Hit_Points--; // reduce health counter ShipHealth.loopspritesheet(); // Animate Decrease health counter if (Enterprise.Hit_Points == 0) { Enterprise.IsAlive=false; // player ship dead Mix_PlayChannel( -1, LargeExp , 0 ); } AstS[Q].IsAlive=false; // asteroid dead Explode[0].SetLocation(AstS[Q].XPos,AstS[Q].YPos); //set explode location Explode[0].IsAlive=true; //explode alive Mix_PlayChannel( -1, SmallExp, 0 ); // play small explode sound } if (Shipshield.IsAlive) { if(AstS[Q].CollChk(Enterprise,0,0,0,0)) //if shield active & Small asteroid hit player or player hit asteroid blow up { Shipshield.IsAlive=false; // take shield down AstS[Q].IsAlive=false; // asteroid kill Explode[1].SetLocation(AstS[Q].XPos,AstS[Q].YPos); //set explode location Explode[1].SetAlive(); Mix_PlayChannel( -1, SmallExp, 0 ); //play small explode sound } } if (PBullet.IsAlive) //if bullet is active/fired { PBullet.LoopObjectOnScreen(); //loop bullet if ship is alive and bullet is alive if (AstS[Q].CollChk(PBullet, 0, 0, 0, 0)) // check if bullet hit the small asteroid if so.. { PBullet.IsAlive = false; // kill bullet as it hit Asteroid [Q] AstS[Q].IsAlive = false; // kill Small Asteroid Number [Q] it was hit by bullet Explode[0].SetLocation(AstS[Q].XPos,AstS[Q].YPos); //set explode location Explode[0].IsAlive=true; Mix_PlayChannel( -1, SmallExp, 0 ); //play hit sound } } } } } I'm trying to find a better way to collision check all alive objects ? |
|||||||||||
|
macrofeet
|
Here is what I came up with any help would be great |
|||||||||||||
|