enemy behavior with boundary to change direction

Posted by BadSniper on Game Development See other posts from Game Development or by BadSniper
Published on 2012-11-18T19:17:10Z Indexed on 2012/11/18 23:34 UTC
Read the original article Hit count: 211

Filed under:
|
|
|
|

I'm doing space shooter kind of game, the logic is to reflect the enemy if it hits the boundary. With my logic, sometimes enemy behaves like flickering instead of changing the velocity. It's like trapped in the boundary and checking for if loops. This is my code for velocity changing:

if(this->enemyPos.x>14)
{
   this->enemyVel.x = -this->enemyVel.x;
}

if(this->enemyPos.x<-14)
{
   this->enemyVel.x = -this->enemyVel.x;
}

How can I get around this? Its going out of boundary and don't know where to go and after sometimes its coming into field. I know whats the problem is, I dont know how to get around this problem.

© Game Development or respective owner

Related posts about c++

Related posts about opengl