collision - C++ DirectX11 2d Game: Stopping enemy sprites moving over each other? -
i using intersectswith(this->boundingbox))
method detect collisions between sprites , player. want somehow able use method in detecting enemy sprites collide each other, , when make sure don't move on 1 another.
all of enemy sprites follow player.
maingame.cpp
loops on each enemy in vector , update loop:
for (auto &enemymobsobj : this->enemymobs) { enemymobsobj->update(ticktotal, tickdelta, timetotal, timedelta, windowbounds, this->ship, this->firstboss, this->enemymobs, this->bullets, this->missiles, null, "null", "null"); }
here tried before stop each sprite moving on each other:
enemymobone::update:
int nextenemy; (int = 0; < enemymobone.size(); i++) { nextenemy = + 1; if (nextenemy < enemymobone.size()) { //deal mobs collision if (enemymobone[i].boundingbox.intersectswith(enemymobone[nextenemy].boundingbox)) { enemymobone[i].position.x = enemymobone[nextenemy].position.x - enemymobone[i].boundingbox.width; } } }
however makes each enemy sprite stick each other, doesn't right, makes them teleport.
anyone know correct code stop them moving on each other? thanks.
when detect intersection between 2 collision objects, need make decision how you're going counteract overlap (as i'm sure figured out). however, how 1 bit trickier "pushing" them 1 side (as you've done in code). want have counter-force applied force, speak. basically, want calculate minimum translation, or direction least amount of movement required, @ least 1 of boxes move out of other one.
this bit more complicated "put me on right (or left, depending on how set coordinates, suppose) side of other guy," more or less code does, now.
for simple solution, check if 1 of colliders closer left, right, top, or bottom of other. this, can take collision intersection position , check relative distance between point , minimum , maximum x , y coordinates relative 1 of colliders, move 1 or both of sprites, accordingly.
ex: [edit] after reviewing previous answer this, realized need calculate overlap of boxes, make easier accomplish so:
float minx = min(sprite0.boundingbox.maxx, sprite1.boundingbox.maxx);// minimum of boxes' right-side points (top right , bottom right) x coordinates float miny = min(sprite0.boundingbox.maxy, sprite1.boundingbox.maxy);// minimum of boxes' top-side points (top left , top right) y coordinates float maxx = max(sprite0.boundingbox.minx, sprite1.boundingbox.minx);// maximum of boxes' left-side points (top left , bottom left) x coordinates float maxy = max(sprite0.boundingbox.miny, sprite1.boundingbox.miny);// maximum of boxes' bottom-side points (bottom left , bottom right) y coordinates float disthoriz = minx - maxx;// horizontal intersection distance float distvert = miny - maxy;// vertical instersection distance // if boxes overlapping less on horizontal axis vertical axis, // move 1 of sprites (in case, sprite0) in opposite direction of // x-axis overlap if(abs(disthoriz) < abs(distvert)) { sprite0.x -= disthoriz; } // else, move 1 of sprites (again, decided use sprite0 here, // arbitrarily) in opposite direction of y-axis overlap else { sprite0.y -= distvert; }
to further clarify (beyond comments), we're doing here checking distance between overlapping lines. example:
box 0 x-axis xmin0|------------------|xmax0 box 1 x-axis xmin1|----------------------|xmax1 |----|<-- overlap (xmax0 - xmin1)
notice minimum 2 bounding boxes used overlap maximum among 2 minima (xmin0 , xmin1), , maximum used overlap minimum among 2 maxima (xmax0 , xmax1).
the y-axis calculation works same way. once have both axes, check see 1 has lower absolute value (which distance shorter) , move along distance counteract intersection.
Comments
Post a Comment