void CKinematicFlee::move(DynamicMovement& currentProperties) { CTransform* transf = static_cast<CTransform*>(m_entity->getComponentByName(TRANSFORM_COMP)); currentProperties.linearSpeed = transf->getPosition() - m_target; currentProperties.linearAccel = currentProperties.linearAccel + currentProperties.linearAccel.crossProduct(Vector3(0.0f, 1.0f, 0.0f)); currentProperties.linearSpeed.normalise(); currentProperties.linearSpeed *= m_maxLinearSpeed; }
void CKinematicSeek::move(DynamicMovement& currentProperties) { CTransform* transf = static_cast<CTransform*>(m_entity->getComponentByName(TRANSFORM_COMP)); currentProperties.linearSpeed = m_target - transf->getPosition(); if (currentProperties.linearSpeed.length() > m_maxLinearSpeed) { currentProperties.linearSpeed.normalise(); currentProperties.linearSpeed *= m_maxLinearSpeed; } }
void CKinematicArrive::move(DynamicMovement ¤tProperties) { CTransform* transf = static_cast<CTransform*>(m_entity->getComponentByName(TRANSFORM_COMP)); currentProperties.linearSpeed = (m_target - transf->getPosition()); currentProperties.linearSpeed /= IMovement::ATTENUATION; if (currentProperties.linearSpeed.squaredLength() > m_maxLinearSpeed2) { currentProperties.linearSpeed.normalise(); currentProperties.linearSpeed *= m_maxLinearSpeed; } }
void CKinematicAlignToTarget::move(DynamicMovement& currentProperties) { CTransform* transf = static_cast<CTransform*>(m_entity->getComponentByName(TRANSFORM_COMP)); Vector3 vec(m_target - transf->getPosition()); currentProperties.angularSpeed = (atan2(-vec.x, -vec.z) - transf->getYaw());// currentProperties.angularSpeed = AI::CAI::correctAngle(currentProperties.angularSpeed); //currentProperties.angularSpeed /= IMovement::ATTENUATION; if (abs(currentProperties.angularSpeed) > m_maxAngularSpeed) { currentProperties.angularSpeed = Ogre::Math::Sign(currentProperties.angularSpeed) * m_maxAngularSpeed; } }
void CSteeringMovement::tick(unsigned int msecs) { IComponent::tick(msecs); using namespace Common::Data; //setting the target if (m_playerTarget) { CEntity* player_target = CLogic::getInstance()->getScene()->getEntityByName("Player"); if(!player_target) return; m_target = static_cast<CTransform*>(player_target->getComponentByName(TRANSFORM_COMP))->getPosition(); m_yaw->setTarget(m_target); } //getting info from the movable entity CTransform* transf = static_cast<CTransform*>(m_entity->getComponentByName(TRANSFORM_COMP)); CMovement* movement = static_cast<CMovement*>(m_entity->getComponentByName(MOVEMENT_COMP)); // Comprobamos si hay que cambiar de movimiento o destino if (m_movType != m_currentMovementType || m_currentTarget != m_target) { if (m_currentMovement != NULL) { delete m_currentMovement; } m_currentMovementType = m_movType; m_currentTarget = m_target; m_currentMovement = AI::Movement::IMovement::getMovement(m_currentMovementType, m_maxLinearSpeed, m_maxAngularSpeed, m_maxLinearAccel, m_maxAngularAccel); // Si hay un nuevo movimiento if (m_currentMovement != NULL) { // Ajustamos el target m_arrived = false; m_currentMovement->setEntity(m_entity); m_currentMovement->setTarget(m_currentTarget); } } bool justArrived = !m_arrived && transf->getPosition().positionEquals(m_target, m_tolerance); if (justArrived) m_arrived = true; // Si nos estamos desplazando calculamos la próxima posición if (!m_arrived && m_currentMovement != NULL) { m_currentMovement->move(m_currentProperties); // Calculamos la evitación if (m_evade != NULL && m_evadeDistance > 0) { AI::Movement::IMovement::DynamicMovement evadeProperties; m_evade->move(evadeProperties); // Distancia al objeto a evadir Vector3 position = transf->getPosition(); Vector3 distance = m_evade->getTarget() - position; float distanceMagnitude = distance.length(); if (distanceMagnitude <= m_evadeDistance){ m_currentProperties.linearAccel += (10.0f * evadeProperties.linearAccel * (msecs * 0.001)); m_evading = true; } else if(m_evading && distanceMagnitude <= m_stopEvadeDistance){ m_currentProperties.linearAccel += (10.0f * evadeProperties.linearAccel * (msecs * 0.001)); }else if(distanceMagnitude > m_stopEvadeDistance) m_evading = false; }else{ m_evading = false; } // Calculamos la rotación if(m_fixedTime >= 0.1f){ m_yaw->move(m_currentProperties); m_fixedTime = 0.0f; }else{ m_fixedTime += msecs * 0.001; } // Mover float speedValue = m_currentProperties.linearSpeed.length(); movement->setSpeed(speedValue); movement->move( Vector3(m_currentProperties.linearSpeed.x, 0.0f,m_currentProperties.linearSpeed.z) ); // Aplicar la rotación float angConst( 0.7/(msecs * 0.01));//5.1 movement->rotate((float)m_currentProperties.angularSpeed * angConst); // Acelerar m_currentProperties.linearSpeed += m_currentProperties.linearAccel; // Clipping if (speedValue > m_maxLinearSpeed) m_currentProperties.linearSpeed *= (m_maxLinearSpeed / speedValue); m_currentProperties.angularSpeed += m_currentProperties.angularAccel; if (m_currentProperties.angularSpeed > m_maxAngularSpeed) m_currentProperties.angularSpeed = Ogre::Math::Sign((float)m_currentProperties.angularSpeed) * m_maxAngularSpeed; } float minTime(1.0f); minTime +=(Application::CGameManager::getInstance()->getTotalObjectives() - Application::CGameManager::getInstance()->getObjectivesAquired()) * 0.5f; if (m_playerTarget) { if ( !m_primary ? m_frequency >= minTime + m_freq(m_generator) : (m_weapons->getPrimaryWeaponType() == Common::Data::Weapons_t::LASER_BEAM ? m_frequency >= minTime + m_freq(m_generator) : m_frequency >= ((minTime + m_freq(m_generator))/2.0f)) ) { if(m_primary){ if(m_weapons->getPrimaryWeaponType() == Common::Data::Weapons_t::LASER_BEAM){ if(((m_weapons->getPrimaryWeapon()->getRange() * m_weapons->getPrimaryWeapon()->getRange()) * 1.1) >= static_cast<CTransform*>(m_entity->getComponentByName(TRANSFORM_COMP))->getPosition().squaredDistance(m_target) ){ m_shootingBeam = !m_shootingBeam; if(m_shootingBeam) m_weapons->shootPrimaryWeapon(msecs); else m_weapons->getPrimaryWeapon()->releaseTrigger(); }else{ m_shootingBeam = false; m_weapons->getPrimaryWeapon()->releaseTrigger(); } }else{ if(((m_weapons->getPrimaryWeapon()->getRange() * m_weapons->getPrimaryWeapon()->getRange()) * 1.1) >= static_cast<CTransform*>(m_entity->getComponentByName(TRANSFORM_COMP))->getPosition().squaredDistance(m_target) ) m_weapons->shootPrimaryWeapon(msecs); } }else{ if(((m_weapons->getSecondaryWeapon()->getRange() * m_weapons->getSecondaryWeapon()->getRange()) * 1.1) >= static_cast<CTransform*>(m_entity->getComponentByName(TRANSFORM_COMP))->getPosition().squaredDistance(m_target) ) m_weapons->shootSecondaryWeapon(); } m_frequency = 0.0f; }else{ m_frequency += (msecs/1000.0f); if(m_primary && m_weapons->getPrimaryWeaponType() == Common::Data::Weapons_t::LASER_BEAM && m_shootingBeam){ if(((m_weapons->getPrimaryWeapon()->getRange() * m_weapons->getPrimaryWeapon()->getRange()) * 1.1) >= static_cast<CTransform*>(m_entity->getComponentByName(TRANSFORM_COMP))->getPosition().squaredDistance(m_target) ){ m_weapons->shootPrimaryWeapon(msecs); }else{ m_weapons->getPrimaryWeapon()->releaseTrigger(); } } } } }
/// Step forward the simulation void Box2DSystem::update(const Time& deltaTime) { mSimulation->Step(deltaTime.seconds(), 8, 8); // Compute stuff with the world ComponentManager* colliderBoxManager = mWorld->getComponentManager<CColliderBox>(); ComponentManager* transformManager = mWorld->getComponentManager<CTransform>(); for (std::size_t i = 0; i < colliderBoxManager->getInstanceCount(); ++i) { CColliderBox* boxCollider = (CColliderBox*)colliderBoxManager->getInstance(i); CTransform* transform = (CTransform*)transformManager->getComponentFromEntity(colliderBoxManager->getInstanceEntity(i)); if (!transform) { mWorld->createComponent(CTransform(), colliderBoxManager->getInstanceEntity(i)); transform = (CTransform*)transformManager->getComponentFromEntity(colliderBoxManager->getInstanceEntity(i)); } if (!boxCollider->userData) { b2BodyDef groundBodyDef2; groundBodyDef2.type = boxCollider->_isDynamic ? b2_dynamicBody : b2_staticBody; groundBodyDef2.position = b2Vec2(transform->getPosition().x, transform->getPosition().y); b2Body* groundBody2 = mSimulation->CreateBody(&groundBodyDef2); b2PolygonShape groundShape2; groundShape2.SetAsBox(10.f, 10.f); groundBody2->CreateFixture(&groundShape2, 1.f); groundBody2->SetUserData(boxCollider); boxCollider->userData = groundBody2; } else { b2Body* boxColliderBody = (b2Body*)boxCollider->userData; transform->position.x = boxColliderBody->GetPosition().x; transform->position.y = boxColliderBody->GetPosition().y; transform->rotation = Quaternion::fromMatrix(mat4::rotatez(boxColliderBody->GetAngle())); } } // Now let's go through the actors to find stuff in them for (std::size_t i = 0; i < mWorld->actors.size(); ++i) { Actor* actor = mWorld->actors[i]; SCColliderBox* box = dynamic_cast<SCColliderBox*>(actor->getRootComponent()); if (box) { if (!box->userData) { b2BodyDef groundBodyDef2; groundBodyDef2.type = box->_isDynamic ? b2_dynamicBody : b2_staticBody; groundBodyDef2.position = b2Vec2(actor->getActorLocation().x, actor->getActorLocation().y); b2Body* groundBody2 = mSimulation->CreateBody(&groundBodyDef2); b2PolygonShape groundShape2; groundShape2.SetAsBox(10.f, 10.f); groundBody2->CreateFixture(&groundShape2, 1.f); groundBody2->SetUserData(box); box->userData = groundBody2; box->rigidBody = new RigidBodyBox2D(groundBody2); } // Apply manual forces b2Body* boxColliderBody = (b2Body*)box->userData; CTransform transform = box->t; transform.position.x = boxColliderBody->GetPosition().x; transform.position.y = boxColliderBody->GetPosition().y; transform.rotation = Quaternion::fromMatrix(mat4::rotatez(boxColliderBody->GetAngle())); // Push the new physics transform back to the actor actor->setTransform(transform); } } }