void Doe::Flee(sf::Vector2f target) { target = Closest(m_position, target); sf::Vector2f diff = m_position - target; if (diff.x*diff.x + diff.y*diff.y > 100000) { m_speed = 3; Move(); } else { m_speed = 8.25f; m_rotation = atan2(diff.y, diff.x); m_direction = sf::Vector2f(cos(m_rotation), sin(m_rotation)); m_position += m_direction * m_speed; float length = sqrt((m_direction.x * m_direction.x) + (m_direction.y * m_direction.y)); if (length > 0) { sf::Vector2f normalised = m_direction / length; m_bodySprite.setPosition(m_position); m_bodySprite.setRotation(atan2(normalised.y, normalised.x) * 180 / (22.0f / 7.0f) + 90.0f); m_selectedSprite.setPosition(m_position); m_selectedSprite.setRotation(atan2(normalised.y, normalised.x) * 180 / (22.0f / 7.0f) + 90.0f); m_headSprite.setPosition(m_position + (normalised * (float)DistanceOfNeck)); m_headSprite.setRotation(atan2(normalised.y, normalised.x) * 180 / (22.0f / 7.0f) + 90.0f); m_tailSprite.setPosition(m_position + (normalised * (float)DistanceOfTail)); m_tailSprite.setRotation(atan2(normalised.y, normalised.x) * 180 / (22.0f / 7.0f) + 90.0f); } } }
Vector3f TrackballManipulator::SphereLineIntersection(const Vector3f& pStart, const Vector3f& pEnd, const Vector3f& pSpherePos, Float pRadius) { // pStart P1 coordinates (point of line) // pEnd P2 coordinates (point of line) // pSpherePos, pRadius P3 coordinates and radius (sphere) Vector3f interVector; Float a, b, c, mu, i ; a = (pEnd - pStart).GetLengthSqr(); b = 2 * ( (pEnd.x - pStart.x)*(pStart.x - pSpherePos.x) + (pEnd.y - pStart.y)*(pStart.y - pSpherePos.y) + (pEnd.z - pStart.z)*(pStart.z - pSpherePos.z) ); c = pSpherePos.GetLengthSqr() + pStart.GetLengthSqr() - 2 * ( pSpherePos | pStart ) - pRadius * pRadius; i = b * b - 4 * a * c ; if( i < 0.0 ) { // no intersection interVector = Closest(pStart, pEnd, pSpherePos); } if( i == 0.0 ) { // one intersection mu = -b/(2*a) ; interVector = pStart + ((pEnd - pStart) * mu); } if ( i > 0.0 ) { // two intersections // first intersection mu = (-b + Maths::Sqrt( b*b - 4*a*c )) / (2*a); Vector3f inter1 = pStart + ((pEnd - pStart) * mu); // second intersection mu = (-b - Maths::Sqrt( b*b - 4*a*c )) / (2*a); Vector3f inter2 = pStart + ((pEnd - pStart) * mu); // Get the camera. Camera* camera = mEditor->GetWorldManager().GetCurrentCamera(); if((inter1 - camera->GetPosition()).GetLength() < (inter2 - camera->GetPosition()).GetLength()) { interVector = inter1; } else { interVector = inter2; } } return interVector; }
void Stag::Chase(sf::Vector2f target) { time_t now; target = Closest(m_position, target); sf::Vector2f diff = m_position - target; if (m_attacking) { m_animatedSprite.update(frameTime); m_animatedSprite.move(m_direction * frameTime.asSeconds()); if (!m_animatedSprite.isPlaying()) { m_attacking = false; } } if (Collision::CircleTest(m_headSprite, Player::GetInstance()->GetSprite()) && std::chrono::duration_cast<milliseconds>(Clock::now() - lastHit).count() > 1000) { m_animatedSprite.play(*m_currentAnimation); lastHit = Clock::now(); m_attacking = true; m_speed = 0; Player::GetInstance()->DecreaseHealth(10); } else if (diff.x*diff.x + diff.y*diff.y >= 18000) { m_speed = 5; m_rotation = atan2(target.y - m_position.y, target.x - m_position.x); m_direction = sf::Vector2f(cos(m_rotation), sin(m_rotation)); m_position += m_direction * m_speed; float length = sqrt((m_direction.x * m_direction.x) + (m_direction.y * m_direction.y)); if (length > 0) { sf::Vector2f normalised = m_direction / length; m_bodySprite.setPosition(m_position); m_bodySprite.setRotation(atan2(normalised.y, normalised.x) * 180 / (22.0f / 7.0f) + 90.0f); m_selectedSprite.setPosition(m_position); m_selectedSprite.setRotation(atan2(normalised.y, normalised.x) * 180 / (22.0f / 7.0f) + 90.0f); m_headSprite.setPosition(m_position + (normalised * (float)DistanceOfNeck)); m_headSprite.setRotation(atan2(normalised.y, normalised.x) * 180 / (22.0f / 7.0f) + 90.0f); m_tailSprite.setPosition(m_position + (normalised * (float)DistanceOfTail)); m_tailSprite.setRotation(atan2(normalised.y, normalised.x) * 180 / (22.0f / 7.0f) + 90.0f); m_animatedSprite.setPosition(m_position + (normalised * (float)DistanceOfAttack)); m_animatedSprite.setRotation(atan2(normalised.y, normalised.x) * 180 / (22.0f / 7.0f) + 90.0f); m_attackArea.setPosition(m_position + (normalised * (float)DistanceOfAttack)); m_attackArea.setRotation(atan2(normalised.y, normalised.x) * 180 / (22.0f / 7.0f) + 90.0f); } } }
void Particle::Update(sf::Vector2f target) { m_sprite.setPosition(m_position); target = Closest(m_position, target); sf::Vector2f diff = m_position - target; if (isAlive) { m_speed = rand() % 5 + 3; m_rotation = atan2(target.y - m_position.y, target.x - m_position.x); m_direction = sf::Vector2f(cos(m_rotation), sin(m_rotation)); m_position += m_direction * m_speed; m_sprite.setColor(m_colour); if (diff.x*diff.x + diff.y*diff.y < 1000) { m_sprite.setColor(sf::Color::Transparent); isAlive = false; m_position = sf::Vector2f(-10000, -100); } } }
void Stag::Flee(sf::Vector2f target) { m_attacking = false; target = Closest(m_position, target); sf::Vector2f diff = m_position - target; if (diff.x*diff.x + diff.y*diff.y > 200000) { Move(); m_speed = 3; } else { m_speed = 4.75; m_rotation = atan2(diff.y, diff.x); m_direction = sf::Vector2f(cos(m_rotation), sin(m_rotation)); m_position += m_direction * m_speed; //m_bodySprite.setRotation(m_rotation * 180 / (22.0f / 7.0f) + 90.0f); //Set position of Head and rotation float length = sqrt((m_direction.x * m_direction.x) + (m_direction.y * m_direction.y)); if (length > 0) { sf::Vector2f normalised = m_direction / length; m_bodySprite.setPosition(m_position); m_bodySprite.setRotation(atan2(normalised.y, normalised.x) * 180 / (22.0f / 7.0f) + 90.0f); m_selectedSprite.setPosition(m_position); m_selectedSprite.setRotation(atan2(normalised.y, normalised.x) * 180 / (22.0f / 7.0f) + 90.0f); m_headSprite.setPosition(m_position + (normalised * (float)DistanceOfNeck)); m_headSprite.setRotation(atan2(normalised.y, normalised.x) * 180 / (22.0f / 7.0f) + 90.0f); m_tailSprite.setPosition(m_position + (normalised * (float)DistanceOfTail)); m_tailSprite.setRotation(atan2(normalised.y, normalised.x) * 180 / (22.0f / 7.0f) + 90.0f); } } }