android - Box2d Collision Detection with Arrays -
i'm developing in andengine have completed game, unfortunately, suffers low fps due fact checking .collideswith lot in update loop. understand problem, , have been trying change using box2d bodies , such in theory, need, can't grasp around it!
basically, have 4 arrays; 1 cars, 3 enemies. cars drive left right, , if make contact of these enemies, it's speed changes depending. have allocate body each 1 of cars , enemies when loading arrays? , how check? run loop, , 'isbodycontacted(carbody, icebergbody);' in update look? it's bit cofusing!
for reference, loading car:
private void loadcar() { (int = 0; < rmanager.getinstance().cararray.length; i++) { rmanager.getinstance().cararray[i] = new car(new sprite( rmanager.getinstance().spawnpoint[i].getspawnpos().x, rmanager.getinstance().spawnpoint[i].getspawnpos().y, rmanager.getinstance().car_region, engine.getvertexbufferobjectmanager()) { @override public boolean onareatouched(final touchevent pscenetouchevent, final float ptoucharealocalx, final float ptoucharealocaly) { if (pscenetouchevent.isactionmove() && gamemanager.getinstance().getdebugmode() == true) { this.setposition(pscenetouchevent.getx(), pscenetouchevent.gety()); } return true; } }); } (int = 0; < rmanager.getinstance().cararray.length; i++) { rmanager.getinstance().cararray[i].getcarsprite().setscale(0.5f); rmanager.getinstance().cararray[i].getcarsprite().setanchorcenter( 0, 0); colourx = (randnumber(2, 9)); coloury = (randnumber(2, 9)); colourz = (randnumber(2, 9)); colourx /= 10; coloury /= 10; colourz /= 10; rmanager.getinstance().cararray[i].getcarsprite().setcolor(colourz, coloury, colourz); attachchild(rmanager.getinstance().cararray[i].getcarsprite()); this.registertoucharea(rmanager.getinstance().cararray[i] .getcarsprite()); this.settouchareabindingonactiondownenabled(true); } }
loading enemy..
void loadiceberg() { (int = 0; < rmanager.getinstance().icebergarray.length; i++) { rmanager.getinstance().icebergarray[i] = new icebergenemy( new sprite(randnumber(200, 700), randnumber(0, 480), rmanager.getinstance().iceberg_region, engine.getvertexbufferobjectmanager()) { @override public boolean onareatouched(final touchevent pscenetouchevent, final float ptoucharealocalx, final float ptoucharealocaly) { this.setposition(pscenetouchevent.getx(), pscenetouchevent.gety()); if (this.gety() > rmanager.getinstance().camera.getheight() - (this.getheight() / 2) || this.gety() < 0 + (this.getheight() / 2)) { this.setposition(200 - (pdifficultylevel * 20), -100); showscore(50, " ice berg!"); this.setvisible(false); } if(pscenetouchevent.isactionup()) { updatescore(50); gamemanager.getinstance().updatetotalicebergenemieskilled(); } return true; } }); rmanager.getinstance().icebergarray[i].geticebergsprite() .setposition( rmanager.getinstance().icebergarray[i] .geticebergsprite().getx(), rmanager.getinstance().icebergarray[i] .geticebergsprite().gety()); rmanager.getinstance().icebergarray[i].geticebergsprite().setscale( 0.8f); this.registertoucharea(rmanager.getinstance().icebergarray[i] .geticebergsprite()); this.settouchareabindingonactiondownenabled(true); attachchild(rmanager.getinstance().icebergarray[i] .geticebergsprite()); } }
some collision!
else if (rmanager.getinstance().icebergarray[2] .geticebergsprite().collideswith( rmanager.getinstance().cararray[r] .getcarsprite())) { rmanager.getinstance().cararray[r].setcarspeed(0f); } else if (rmanager.getinstance().icebergarray[3] .geticebergsprite().collideswith( rmanager.getinstance().cararray[r] .getcarsprite())) { rmanager.getinstance().cararray[r].setcarspeed(0f); } else if (rmanager.getinstance().icebergarray[4] .geticebergsprite().collideswith( rmanager.getinstance().cararray[r] .getcarsprite())) { rmanager.getinstance().cararray[r].setcarspeed(0f); } else if (rmanager.getinstance().icebergarray[5] .geticebergsprite().collideswith( rmanager.getinstance().cararray[r] .getcarsprite())) { rmanager.getinstance().cararray[r].setcarspeed(0f); }
i have found many tutorials, none seem reference array of objects, , need. if me or provide sort of quick tutorial, appreciated. thanks!!
you should not use loops check each possible combination of collision. instead, should register listener whole world , set user data on bodies (e.g. type = "car", "enemy"). in listener, check 2 objects collided. check out example:
http://www.andengine.org/forums/gles2/collision-events-t7140.html#p31300
Comments
Post a Comment