int C_Rect::Inside(C_Vec2 v) { if( (v.X()>=Start.X())&& (v.X()<=End.X())&& (v.Y()>=Start.Y())&& (v.Y()<=End.Y()) ) return 1; return 0; }
bool Unit::Move(float dt) { bool ret = true; C_Vec2<float> vel = currentVelocity * dt; //Continuous evaluation if (App->entityManager->continuous) { //TODO 3: Split the velocity in parts and check for the target //------------------------------------------------------- //Splitting the velocity into smaller pieces to check if the unit reaches the target float module = vel.GetModule(); int steps = (int)floor(vel.GetModule() / targetRadius); C_Vec2<float> velStep = (vel.GetNormal() * targetRadius); C_Vec2<float> rest = vel - velStep * (float)steps; for (int i = 0; i < steps && ret; i++) { position.x += velStep.x; position.y += velStep.y; if (isTargetReached()) ret = false; } if (ret) { position.x += rest.x; position.y += rest.y; if (isTargetReached()) ret = false; } //------------------------------------------------------- } //Normal movement else { //TODO 1: Move the unit with the velocity obtained previously //------------------------------------------------------ position.x += vel.x; position.y += vel.y; //----------------------------------------------------- if (isTargetReached()) ret = false; } currentVelocity.position = desiredVelocity.position = position; UpdateCollider(); return ret; }
void Unit::DrawDebug() { //Current velocity vector: green float lineX1, lineX2, lineY1, lineY2; C_Vec2<float> line = currentVelocity; line.Normalize(); line *= 3; lineX1 = line.position.x; lineY1 = line.position.y; lineX2 = (line.x * 30 + lineX1); lineY2 = (line.y * 30 + lineY1); App->render->DrawLine((int)lineX1, (int)lineY1, (int)lineX2, (int)lineY2, true, 0, 255, 0); //Desired velocity vector: red C_Vec2<float> line1 = desiredVelocity; line1.Normalize(); line1 *= 3; lineX1 = line1.position.x; lineY1 = line1.position.y; lineX2 = (line1.x * 30 + lineX1); lineY2 = (line1.y * 30 + lineY1); App->render->DrawLine((int)lineX1, (int)lineY1, (int)lineX2, (int)lineY2, true, 255, 0, 0); SDL_Rect rect = collider; App->render->DrawQuad(rect, true, 0, 255, 0, 255, false); //Target position // App->render->DrawCircle(target.x, target.y, 10, true, 255, 255, 255); //Unit position // App->render->DrawCircle((int)round(position.x), (int)round(position.y), 10, true, 255, 255, 255, 255); //Path if (path.Count() > 0) { for (uint i = 0; i < path.Count(); i++) { iPoint position = App->map->MapToWorld(path[i].x, path[i].y); SDL_Rect pos = { position.x, position.y, 8, 8 }; SDL_Rect rect = { 0, 0, 64, 64 }; if (i < (uint)currentNode) rect = { 0, 0, 64, 64 }; App->render->Blit(App->sceneMap->debug_tex, &pos, true, &rect); } } }
//Get the desired velocity: target position - current position void Unit::GetDesiredVelocity() { //TODO 1: Get the velocity we are looking for. //Remember: velocity module must be "maxSpeed" //Set velocity position to entity position for debug C_Vec2<float> velocity = { 0, 0 }; //----------------------------------- velocity.x = (target.x - position.x); velocity.y = (target.y - position.y); velocity.position = position; velocity.Normalize(); velocity *= maxSpeed; //---------------------------------- desiredVelocity = velocity; }
bool Unit::isTargetReached() { //TODO 2: Check if we have reached the target //------------------------------------------------------ C_Vec2<float> vec; vec.x = target.x - position.x; vec.y = target.y - position.y; float distance = vec.GetModule(); if (distance < targetRadius) { position.x = (float)target.x; position.y = (float)target.y; currentVelocity.position = desiredVelocity.position = position; return true; } //----------------------------------------------------- return false; }
void normieren(C_Vec2& G) // Normiert einen Vektor { double r = G.Length(); G /= r; }
C_Rect::C_Rect(const C_Vec2& start, const C_Vec2& end) { double startx, endx, starty, endy; Start = start; End = end; startx = start.X(); starty = start.Y(); endx = end.X(); endy = end.Y(); if (start.X() > end.X()) { startx = end.X(); endx = start.X(); } if (start.Y() > end.Y()) { starty = end.Y(); endy = start.Y(); } Start = C_Vec2(startx, starty); End = C_Vec2(endx, endy); }
double operator < (const C_Vec2& v, const C_Vec2& w) { return double(acos((v*w) / (v.Length() * w.Length()))); }
float Bot::DistanceBetweenUnits(Unit* unit1, Unit* unit2) { C_Vec2<float> distance = { unit1->GetPosition().x - unit2->GetPosition().x, unit1->GetPosition().y - unit2->GetPosition().y }; return distance.GetModule(); }
// Useful Distance functions bool Bot::EnemyOnRange(Unit* unit1, Unit* unit2, float range) { C_Vec2<float> distance = { unit1->GetPosition().x - unit2->GetPosition().x, unit1->GetPosition().y - unit2->GetPosition().y }; return (distance.GetModule() < range + unit1->colRadius + unit2->colRadius); }