XMVECTOR BoidManager::findAlign(birds &bird) { birds tempBird = bird; XMVECTOR Align = XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f); XMVECTOR tempVectorI, tempVectorTB, vectorBetween; float distanceBetween; int count = 0; for (int i = 0; i < maxBirds; i++) { if (m_birds.at(i)->getID() != tempBird.getID()) { distanceBetween = getDistanceBetween(getVectorBetween(*m_birds.at(i), tempBird)); if (distanceBetween < 10.0f) { tempVectorI = m_birds.at(i)->getVector(); Align = XMVectorAdd(Align, tempVectorI); ++count; } } } if (count > 1) { Align = Align / (count - 1); Align = XMVector4Normalize(Align); Align = XMVectorSubtract(Align, tempBird.getVector()); } return Align; }
XMVECTOR BoidManager::findSeek(birds &bird) { birds tempBird = bird; XMVECTOR seek = XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f); XMVECTOR tempVectorI; XMVECTOR tempVectorTB = XMLoadFloat4(&tempBird.getPosition()); float distanceBetween; bool seeking = false; int count = 0; for (int i = 0; i < maxBirds; i++) { if (m_birds.at(i)->getID() != tempBird.getID()) { distanceBetween = getDistanceBetween(getVectorBetween(*m_birds.at(i), tempBird)); if (distanceBetween < 15.0f) { tempVectorI = m_birds.at(i)->getVector(); seek = XMVectorAdd(seek, tempVectorI); ++count; } } } if (count > 1) { seek = XMVectorSubtract(seek, tempVectorTB); seek = XMVector4Normalize(seek); seek = seek / (count - 1); seek = XMVectorSubtract(seek, tempBird.getVector()) / 100; } return seek; }
void Ship::approach() { target_angle = getAngleBetween(getPosition(), target); turnTo(Angle(target_angle)); if (getDistanceBetween(getPosition(), target) < getGlobalBounds().width * 2.0f) { stop(); setRotation(target_angle); } else { correctSpeed(); } }
void SolarSystem::setObjectsVelocity() { std::list<std::pair<SolarObject*, SolarObject*> > pairs = getSolarObjectsPairs(); for(std::list<std::pair<SolarObject*, SolarObject*> >::iterator it = pairs.begin(); it != pairs.end(); ++it) { std::pair<SolarObject*, SolarObject*> pair = *it; SolarObject* first = pair.first; SolarObject* second = pair.second; double distance = getDistanceBetween(first, second); double force = GravityC * first->mass * second->mass / distance / distance / 6046; double angle = getAngleBetween(first, second); double dv1 = force / first->mass * timeStep; double dv2 = force / second->mass * timeStep; first->changeVelocity(dv1, angle); second->changeVelocity(dv2, angle + PI); } }
XMVECTOR BoidManager::findAvoid(birds &bird) { birds tempBird = bird; XMVECTOR avoid = XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f); XMVECTOR tempVectorI, tempVectorTB, vectorBetween; float distanceBetween; for (int i = 0; i < maxBirds; i++) { if (m_birds.at(i)->getID() != tempBird.getID()) //If bird is not itself { distanceBetween = getDistanceBetween(getVectorBetween(*m_birds.at(i), tempBird)); if (distanceBetween < 5.0f) { avoid = XMVectorSubtract(avoid, getVectorBetween(*m_birds.at(i), tempBird)); avoid = XMVector4Normalize(avoid); } } } return avoid; }
void Ship::correctSpeed() { float stopping_distance = sqrt(pow(getStoppingDistance(velocity.x, frame.getAccel()), 2.0f) + pow(getStoppingDistance(velocity.x, frame.getAccel()), 2.0f)); auto moving_toward = movingToward(target); if (stopping_distance < getDistanceBetween(getPosition(), target) - getGlobalBounds().width / 2.0f && moving_toward.first && moving_toward.second) { accelerate(); } else if (!moving_toward.first) { if (target.x > getPosition().x) { velocity.x += frame.getAccel() / 2.0f; } else { velocity.x -= frame.getAccel() / 2.0f; } } else if (!moving_toward.second) { if (target.y > getPosition().y) { velocity.y += frame.getAccel() / 2.0f; } else { velocity.y -= frame.getAccel() / 2.0f; } } else { decelerate(); } }