Thanks for the replies.
The enemies simply navigate around the map , and they attack player or trying to avoid any incoming attacks.
Sante , i looked at your 1st link , but looks too complicated for what im trying to do.
I need something really simple :).
Anyway , i've coded a function (similar to rect collision detection) , but didn't work as expected.
I've also tried various methods to get the desired effects , but at the end the movement looks choppy :).
Here , im posting the source for those who're interested.
inline void dontTouchMe(CBox& a,CBox& b) { float leftA,leftB,rightA,rightB, topA,topB,bottomA,bottomB; //'a' box leftA = a.x; rightA = (a.x + a.w); topA = a.y; bottomA = a.y + a.h; //'b' box leftB = b.x; rightB = (b.x + b.w); topB = b.y; bottomB = (b.y + b.h); if(bottomA <= topB) { a.yvel += b.h/2; b.yvel -= b.h/2; } if(topA >= bottomB) { a.yvel -= b.h/2; b.yvel += a.h/2; } if(rightA <= leftB) { a.xvel += b.w/2; b.xvel -= a.w/2; } if(leftA >= rightB) { a.xvel -= b.w/2; b.xvel += a.w/2; } }
And here's my second attempt ( check if 'a' touching 'b' , and adjust their velocity)
void CEnemyList::update(const int ai_op,const CBox& player_pos,const unsigned time_now) { if(enemies.empty())return; std::vector<CEnemyAttributes>::iterator next = enemies.begin(); for(std::vector<CEnemyAttributes>::iterator it=enemies.begin(); it < enemies.end();it++) { next = it++; switch(it->getState()) {//========================follow state case AI_ENEMY_STATE_FOLLOW: { if(it->box.angle > player_pos.angle) { it->box.angle -=2 ; } if(it->box.angle < player_pos.angle) { it->box.angle +=2 ; } if(next < enemies.end()) { if( collidesWith(it->box,next->box) ) { next->box.xvel += it->box.w/6; next->box.yvel += it->box.h/6; it->box.xvel -= next->box.w/6; it->box.yvel -= next->box.h/6; } } if(it->box.xvel > 1.0f) it->box.xvel = 1.0f; if(it->box.yvel > 1.0f) it->box.yvel = 1.0f; if(it->box.x > player_pos.x - player_pos.w/2) { it->box.x -= it->box.xvel;// + (float)(rand()%(int)it->box.w/6)+1; } if(it->box.x < player_pos.x+ player_pos.w/2) { it->box.x += it->box.xvel ;// + (float)(rand()%(int)it->box.w/6)+1; } if(it->box.y > player_pos.y - player_pos.h/2) { it->box.y -= it->box.xvel ;// + (float)(rand()%(int)it->box.w/6)+1; } if(it->box.y < player_pos.y + player_pos.h/2) { it->box.y += it->box.yvel ;// + (float)(rand()%(int)it->box.w/6)+1; } } break; }//switch applyBorderCollision(it->box); if(it->getType() == ENEMY_TYPE_A) { enemy_type_a.draw(it->box); } }//for }