c++ - Basic Collision problems -
i have sprite class working on making function collision. want function return true if 2 sprites collide. when call function pass moving objects desired x , y position along it's height , width. invoking sprite object i check. example if wanted check if player 1's move intersect player 2's sprite player2.collide(p1x, p1y, p1h, p1w).
bool sprite::collides(int x, int y, unsigned short w, unsigned short h) const { if ((x == this->getwidth() + this->getleft()) && (y >= this->gettop() && y <= this->gettop() + this->getheight()) || ((x + w) == this->getleft()) && (y >= this->gettop() && y <= this->gettop() + this->getheight())) return true; else if ( (y == this->getheight() + this->gettop()) && ((x >= this->getleft() && x <= this->getleft() + this->getwidth()) || ((y + h) == this->gettop() && (x >= this->getleft() && x <= this->getleft() + this->getwidth())))) return true; return false; } the sprites rectangles. seems forgetting check conditions collision. if rectanges collide works, if shift 1 try butt against other, glide right through. seems each direction approach has similar behavior work sections. can guys me find i'm forgetting check?
i think need check logic of actual conditions again, shouldn't
(x == this->getwidth() + this->getleft()) just part of if statement create collision? right after have &&. in case needs touching on top , right, not right or top???
i think above line should be:
(x <= this->getwidth() + this->getleft()) also, depending on actual movement conditions (i.e. velocity, etc...) above condition
(x <= this->getwidth() + this->getleft() - 1) lets explain minus 1: if have 2 objects, 1 moving directly upward, , moving directly downward, i.e. vertically , parallel 1 another, getwidth + getleft creates "went by" each other condition not collision condition.
example sprites each 4 x 4 moving vertically respect each other:
sprite @ (0, 2) sprite b @ (4, 2) 0 1 2 3 0 1 2 3 ------------------------- ------------------------- | | | | | | | | | | ------------------------- ------------------------- | | | | | | | | | | ------------------------- ------------------------- | | | | | | | | | | ------------------------- ------------------------- | | | | | | | | | | ------------------------- ------------------------- sprite @ (0, 1) sprite b @ (4, 3) 0 1 2 3 ------------------------- | | | | | ------------------------- | | | | | 0 1 2 3 ------------------------- ------------------------- | | | | | | | | | | ------------------------- ------------------------- | | | | | | | | | | ------------------------- ------------------------- | | | | | ------------------------- | | | | | ------------------------- sprite @ (0, 0) sprite b @ (4, 4) 0 1 2 3 ------------------------- | | | | | ------------------------- | | | | | ------------------------- | | | | | ------------------------- | | | | | 0 1 2 3 ------------------------- ------------------------- | | | | | ------------------------- | | | | | ------------------------- | | | | | ------------------------- | | | | | ------------------------- when sprite @ location (0, 0), getwidth + getleft = (0 + 4) right of actual end of sprite. if sprite moving vertically downward x position 4, algorithm flag collision, when in reality moving closely 1 another. - 1 comes in.
also, want make sure careful screen geometry. mean many screens move top-left being pixel (0, 0) bottom right being pixel (positive x, positive y). second condition:
(y >= this->gettop() && y <= this->gettop() + this->getheight()) might need be:
( (y + h - 1) >= this->gettop() || y <= (this->gettop() + this->getheight() - 1) ) the first portion needs change because testing top of input object colliding top of object tested against. want bottom of input tested top of object tested against.
one thing have found extremely useful when trying perform type of coding, draw out simple sketches actual pixel based example numerics can visualize prior coding it.
i hope of helps :-)
Comments
Post a Comment