Hello everybody! I'm trying to solve test task:

Given a list of segments (walls) , given the coordinates of two points on a two-dimensional space . Need to write a class BulletManager, which will include the following functions:

• void Update (float time), where time - global time of update in seconds. Function calculates the trajectory of bullets at a specified time and in contact with the wall, remove it from the list of segments . While hitting the wall, the bullet is reflected.• void Fire (float2 pos, float2 dir, float speed, float time, float life_time), where pos - the starting position of the bullet in meters , dir - direction , speed - speed in meters per second , time - the shot in seconds , life_time - time to self-destruction bullets . This function adds a bullet in manager for further processing by an update .

Sorry for my English, the text above is google-translated.

I've made it, all is good, except of reflextion: bullet reflects, but not always correctly. Here is my function:

void BulletManager::Update(float time) { for (int n = 0; n < bullets.size();) { auto bul = bullets.begin() + n; XMFLOAT2* bulDir = &bul->get()->GetDir(); float x = bul->get()->GetInitPosition().x + (bulDir->x * (time - bul->get()->GetTime()) * bul->get()->GetSpeed()); float y = bul->get()->GetInitPosition().y + (bulDir->y * (time - bul->get()->GetTime()) * bul->get()->GetSpeed()); XMFLOAT2 point = XMFLOAT2(x, y); bul->get()->SetPosition(point); //Check bullet life time if (bul->get()->GetLifeTime() - (time - bul->get()->GetTime()) <= 0.0f) { bullets.erase(bul); n--; continue; } //Check, if bullet collides with sides for (int i=0; i<SidesCoordList.size(); i++) { if ( IsIntersecting(SidesCoordList[i], point, 1.0f) ) { //Calculating normal vector for current side float A = abs(SidesCoordList[i].point1.y - SidesCoordList[i].point2.y); float B = abs(SidesCoordList[i].point2.x - SidesCoordList[i].point1.x); XMFLOAT2 normal = XMFLOAT2(A, B); normal = XMFLOAT2(normal.x / Calculations::VecLength(normal), normal.y / Calculations::VecLength(normal)); //Calculating angles to get reflected directional vector float alpha = atan(A/B); float beta = acos(bulDir->x); float gamma = XM_PIDIV2 - (alpha - beta); float theta = beta + 2*gamma + XM_PI; //Setting new direction (after reflection) bul->get()->GetDir() = Calculations::RotateVector(bul->get()->GetDir(), theta); bul->get()->GetInitPosition() = bul->get()->GetPosition(); bul->get()->GetTime() = time; } } n++; } }

What's wrong with my code?

Thanks.