Vector3 CFSM::ComputeSeperation(vector<CFSM*> ListOfCharacters) { Vector2 steer; steer.SetZero(); int count = 0; for (int i = 0; i < ListOfCharacters.size(); ++i) { if (ListOfCharacters[i] != this && ListOfCharacters[i]->GetAlive()) { float distance = (m_CurrentPosition - ListOfCharacters[i]->GetPosition()).Length(); if (distance < m_seperationZone) { Vector3 DirectionCopy = m_CurrentPosition - ListOfCharacters[i]->GetPosition(); Vector2 diff = Vector2(DirectionCopy.x, DirectionCopy.z); if (!diff.IsZero()) { diff = diff.Normalized(); diff.x /= distance; diff.y /= distance; } steer += diff; count++; } } } if (count > 0) { steer.x /= (float)count; steer.y /= (float)count; } if (steer.Length() > 0) { steer = steer.Normalized(); steer *= m_MovementSpeed; Vector2 DirectionCopy = Vector2(m_CurrentDirection.x, m_CurrentDirection.z); steer -= DirectionCopy; if (steer.Length() >= m_maxforce) { steer *= (m_maxforce / steer.Length()); } } return Vector3(steer.x, 0, steer.y); }
void PlayerArrow::Update() { // アングル Vector3 playerFront = Vector3(player->GetParameter().mat.GetFront() * Vector3(1, 0, 1)).Normalized(); if (playerFront.Length() != 0) { angle_ = Vector3::Inner(playerFront, -Vector3::Forward); if (Vector3::Dot(playerFront, Vector3::Left) > 0.0f) angle_ *= -1; } /* プレイヤーデータ */ // ポジション Vector3 playerPos = player->GetParameter().mat.GetPosition(); Vector2 pos = Vector2(playerPos.x, -playerPos.z); if (pos.Length() != 0.0f) drawPos_ = MAP_DRAW_POSITION + pos.Normalized() * pos.Length() * RE_SIZE_SCALE; else drawPos_ = MAP_DRAW_POSITION; /* 気流発生 */ isDash = player->ReturnTackleParameter().dashFlag; if (isDash && isDash != prevDash) world.UIAdd(UI_ID::FLOWROOT_UI, std::make_shared<FlowRoot>(world, player, &drawPos_, resPiece)); prevDash = isDash; }
void WanderBehaviour::Update(Agent* a_agent) { Vector2 randomTargetOnCircle; if (/*a_agent->WanderTargetCalculated == false*/ true) { float randomDeg = rand() % 360; float randomRadian = randomDeg * PI / 180.0f; randomTargetOnCircle = Vector2(cos(randomRadian), sin(randomRadian)); randomTargetOnCircle = randomTargetOnCircle * wanderRadius; } else { //randomTargetOnCircle = a_agent->prevWanderTarget; } //Jitter Vector float randomDeg = rand() % 360; float randomRadian = randomDeg * PI / 180.0f; Vector2 jitterVector(cos(randomRadian), sin(randomRadian)); jitterVector = jitterVector * jitterAmount; // Add jitter vector to target vector randomTargetOnCircle += jitterVector; // Bring target back to circle randomTargetOnCircle = randomTargetOnCircle.Normalized() * wanderDistance; //Add agent heading * wanderdist randomTargetOnCircle; //a_agent->prevWanderTarget = randomTargetOnCircle; randomTargetOnCircle += a_agent->GetPosition(); //calculate seek force Vector2 vecBetween = randomTargetOnCircle - a_agent->GetPosition(); Vector2 force = vecBetween.Normalized() * a_agent->m_maxVelocity; //Add force to agent a_agent->AddForce(force - a_agent->m_velocity); }
void SeekAction::Execute(Agent * a_agent) { //calculate force vector Vector2 vecBetweenAgentTarget = a_agent->GetTarget() - a_agent->GetPosition(); Vector2 force = vecBetweenAgentTarget.Normalized() * a_agent->m_maxVelocity; //Add the force a_agent->AddForce(force - a_agent->m_velocity); }
Vector2 Wave::GetPath(Vector2 start, Vector2 end, float speed) { if (start == end) return Vector2(0, 0); Vector2 up(0, 1); return Vector2(speed, sin(up.AngleBetween((start.Normalized()))) * speed); }
void ClickTargetSystem::ObjectClicked(Pocket::TouchData d) { if (d.Index != 1) return; std::vector<GameObject*> objectsToMove; for(auto o : selectables->Objects()) { if (!o->GetComponent<Selectable>()->Selected()) continue; objectsToMove.push_back(o); // o->GetComponent<Movable>()->Target = {d.WorldPosition.x, d.WorldPosition.z }; } if (objectsToMove.empty()) return; Vector2 target = { d.WorldPosition.x, d.WorldPosition.z }; Vector2 centerPoint = 0; for(GameObject* go : objectsToMove) { centerPoint += go->GetComponent<Particle>()->position; } centerPoint *= (1.0f / objectsToMove.size()); std::vector<Vector2> targets; for(GameObject* go : objectsToMove) { Vector2 fromCenterPoint = go->GetComponent<Particle>()->position - centerPoint; targets.push_back(target + fromCenterPoint.Normalized()); } for (int iterations=0; iterations<10; iterations++) { for (int i=0; i<objectsToMove.size(); i++) { //Particle* a = objectsToMove[i]->GetComponent<Particle>(); Vector2& targetA = targets[i]; for (int j=i+1; j<objectsToMove.size(); j++) { //Particle* b = objectsToMove[i]->GetComponent<Particle>(); Vector2& targetB = targets[j]; const float radius = 2.0f; Vector2 vector = targetB - targetA; float length = vector.Length(); if (length<radius) { vector *= (1.0f / length); float penetration = radius - length; targetA -= vector * penetration * 0.5f; targetB += vector * penetration * 0.5f; } } } /* for (int i=0; i<objectsToMove.size(); i++) { targets[i] = objectsToMove[i]->GetComponent<Mappable>()->Map->FindNearestValidPosition(targets[i]); } */ } for (int i=0; i<objectsToMove.size(); i++) { objectsToMove[i]->GetComponent<Movable>()->Target = targets[i]; } }
Vector2 Vector2::MoveToPoint(Vector2 pos, Vector2 destination, float movement) { if (pos == destination) { return pos; } Vector2 relative = (destination - pos); float distance = relative.Length(); if (distance < movement) { // Reach destination return destination; } else { return pos + relative.Normalized() * movement; } }
Vector3 CFSM::ComputeAlignment(vector<CFSM*> ListOfCharacters) { Vector2 sum; int count = 0; for (int i = 0; i < ListOfCharacters.size(); ++i) { if (ListOfCharacters[i] != this && ListOfCharacters[i]->GetAlive()) //if (ListOfCharacters[i] != this && ListOfCharacters[i]->TYPE != "BOSS") { float distance = (m_CurrentPosition - ListOfCharacters[i]->GetPosition()).Length(); if (distance < m_flockZone) { Vector2 VelCopy = Vector2(ListOfCharacters[i]->GetCurrentDirection().x, ListOfCharacters[i]->GetCurrentDirection().z); sum += VelCopy; count++; } } } if (count > 0) { sum.x /= (float)count; sum.y /= (float)count; if (!sum.IsZero()) { sum = sum.Normalized(); sum *= m_MovementSpeed; } Vector2 DirectionCopy = Vector2(m_CurrentDirection.x, m_CurrentDirection.z); Vector2 steer = sum - DirectionCopy; if (steer.Length() >= m_maxforce) { steer *= (m_maxforce / steer.Length()); } return Vector3(steer.x, 0, steer.y); } else { return Vector3(0, 0, 0); } }
Vector2 CFSM::seek(Vector2 target) { Vector2 PositionCopy = Vector2(m_CurrentPosition.x, m_CurrentPosition.z); Vector2 direction = target - PositionCopy; if (!direction.IsZero()) { direction = direction.Normalized(); direction *= m_MovementSpeed; } Vector2 DirectionCopy = Vector2(m_CurrentDirection.x, m_CurrentDirection.z); Vector2 steer = direction - DirectionCopy; if (steer.Length() >= m_maxforce) { steer *= (m_maxforce / steer.Length()); } return steer; }
void Fluid::AddForces(const Vector2& position, const Vector2& direction, float sigma, float amplitude, View2f* velocities) const { if (direction.LengthSquared() < 1e-4f) { return AddForces(position, sigma, amplitude, velocities); } const int x = std::lroundf(position.x * Width()); const int y = std::lroundf((1.0f - position.y) * Height()); const int radius = std::lround(3.3f * sigma); const Vector2 dn = direction.Normalized(); const float sigmaSq = sigma * sigma; for (int j = y - radius; j <= y + radius; ++j) { for (int i = x - radius; i <= x + radius; ++i) { const int s = i - x; const int t = y - j; // Flips at y-axis. const float e = amplitude * std::exp(-(0.5f / sigmaSq) * (s * s + t * t)); AddValue(i, j, e * dn, velocities); } } }
/************************************************************************************************************* AFTER-COLLISION EFFECT : Change Variables after Collision *************************************************************************************************************/ bool CollisionHandler::FindCollideNormal(Box* b1, Vector2 origin) { Vector2 up(0, 1); Vector2 down(0, -1); Vector2 right(1, 0); Vector2 left(-1, 0); Vector2 max = b1->GetOrigin() + Vector2(b1->GetWidth() / 2, b1->GetHeight() / 2); Vector2 dir = (origin - b1->GetOrigin()); if ((origin - b1->GetOrigin()).IsZero()) { b1->SetCollideNormal(Vector2(0, 1)); return true; } else dir = dir.Normalized(); if ((max - b1->GetOrigin()).IsZero()) { b1->SetCollideNormal((dir.unary())); return true; } float horizontalDotLimit = right.Dot((max - b1->GetOrigin()).Normalized()); float verticalDotLimit = up.Dot((max - b1->GetOrigin()).Normalized()); if (up.Dot(dir) > verticalDotLimit && up.Dot(dir) < 1)//Up b1->SetCollideNormal(up); else if (down.Dot(dir) > verticalDotLimit && down.Dot(dir) < 1)//Down b1->SetCollideNormal(down); else if (right.Dot(dir) > horizontalDotLimit && right.Dot(dir) < 1)//Right b1->SetCollideNormal(right); else if (left.Dot(dir) > horizontalDotLimit && left.Dot(dir) < 1)//Left b1->SetCollideNormal(left); else //Corner b1->SetCollideNormal((dir.unary())); return true; }
void MainWindow::BruteForceVoronoi() { vector<Edge> halfPlanes; for (int i = 0; i < points.size(); i++) { halfPlanes.clear(); for (int j = 0; j < points.size(); j++) { if (i == j) { continue; } Vector2 center = (points[i] + points[j]) * 0.5f; Vector2 perp = (points[i] - points[j]).GetPerpendicular(); if (perp.Length() == 0.0f) { continue; } Vector2 n = perp.Normalized(); halfPlanes.push_back(Edge(center - n * 50.0f, center + n * 50.0f)); } vector<Vector2> isectPoints; for (int k = 0; k < halfPlanes.size(); k++) { for (int l = k + 1; l < halfPlanes.size(); l++) { Vector2 r(0.0f, 0.0f); if (IntersectLines(halfPlanes[k].p0, halfPlanes[k].p1 , halfPlanes[l].p0, halfPlanes[l].p1 , r)) { bool inside = true; for (int j = 0; j < halfPlanes.size(); j++) { if (j == k || j == l) { continue; } if (HalfPlaneSign(halfPlanes[j].p0, halfPlanes[j].p1, points[i]) * HalfPlaneSign(halfPlanes[j].p0, halfPlanes[j].p1, r) <= 0.0f) { inside = false; } } if (inside) { isectPoints.push_back(r); } } } } CalcConvexHull(isectPoints); for (int j = 0; j < isectPoints.size(); j++) { // gIsectPoints.push_back(isectPoints[j]); edges.push_back(Edge(isectPoints[j], isectPoints[(j + 1) % isectPoints.size()])); } } }