void AI::collisionHandler(std::vector<GameObject *> &m_GOList) { if(collisionChecking(pos + Vector3(collisionMesh.ColBox.x, ModelPos.y, 0), m_GOList)) { if(vel.x > 0) { vel.x = 0; } } if(collisionChecking(pos + Vector3(-collisionMesh.ColBox.x, ModelPos.y, 0), m_GOList)) { if(vel.x < 0) { vel.x = 0; } } if(collisionChecking(pos + Vector3(0, ModelPos.y, collisionMesh.ColBox.z), m_GOList)) { if(vel.z > 0) { vel.z = 0; } } if(collisionChecking(pos + Vector3(0, ModelPos.y, -collisionMesh.ColBox.z), m_GOList)) { if(vel.z < 0) { vel.z = 0; } } }
void Physics3DWorld::stepSimulate(float dt) { if (_btPhyiscsWorld) { setGhostPairCallback(); //should sync kinematic node before simulation for (auto it : _physicsComponents) { it->preSimulate(); } _btPhyiscsWorld->stepSimulation(dt, 3); //sync dynamic node after simulation for (auto it : _physicsComponents) { it->postSimulate(); } if (needCollisionChecking()) collisionChecking(); } }
void AI::aiStateHandling(double &dt, Vector3 &playerPos, std::vector<GameObject*> &m_GOList) { Mtx44 rotation; rotation.SetToRotation(CalAnglefromPosition(Lookat, pos, true), 0.f, 1.f, 0.f); Vector3 L, R, C; C = rotation * Vector3(0.f, ModelPos.y, 50.f); L = rotation * Vector3(-15.f, ModelPos.y, 15.f); R = rotation * Vector3(15.f, ModelPos.y, 15.f); switch (e_State) { case WALKING: { //Have the AI partol a certain area //Need Pathfinding i think destination = playerPos; if (b_isDestinationWithinFOV && b_isDestinationVisible) { //If player is infront and near player, then ai will switch to attack state if ((playerPos - pos).LengthSquared() < d_detectionRange) { currentLookat.x = playerPos.x; currentLookat.z = playerPos.z; destination = playerPos; prevPosition = pos; e_State = ATTACK; break; } //if ai saw player but is too far way, the ai will investigate else if ((playerPos - pos).LengthSquared() >= d_detectionRange && (playerPos - pos).LengthSquared() <= d_detectionRangeMax) { destination.x = playerPos.x; destination.z = playerPos.z; destination = playerPos; prevPosition = pos; e_State = ALERT; break; } } SensorUpdate(dt, collisionChecking(pos + L, m_GOList), collisionChecking(pos + C, m_GOList), collisionChecking(pos + R, m_GOList)); } break; case ALERT: { //If destination is not visible if (!b_isDestinationVisible) { //use sensor update 2 to get to the destination SensorUpdate2(dt, collisionChecking(pos + L, m_GOList), collisionChecking(pos + C, m_GOList), collisionChecking(pos + R, m_GOList)); } //If player is infront and near player, then ai will switch to attack state if (isVisible(pos, Lookat, getDetectionAngle(), playerPos) && (playerPos - pos).LengthSquared() < d_detectionRange) { currentLookat.x = playerPos.x; currentLookat.z = playerPos.z; destination = playerPos; } if (b_isDestinationVisible && b_isDestinationWithinFOV && destination == playerPos) { e_State = ATTACK; } if(b_isDestinationVisible && b_isDestinationWithinFOV) { moveToDestination(dt); } if((pos - destination).LengthSquared() < 300) { e_State = WALKING; } } break; case ATTACK: { destination = playerPos; //if enemy is holding a weapon if (holding != NULL) { if (holding->isWeapon) { WeaponsObject *WO = dynamic_cast<WeaponsObject*>(holding); //Enemy is holding a gun if (WO->isGun) { //Make enemy stay at the same pos if (b_isDestinationVisible && b_isDestinationWithinFOV && (playerPos - pos).LengthSquared() < d_detectionRange) { currentLookat.x = playerPos.x; currentLookat.z = playerPos.z; b_SHOOTLA = true; } else { b_SHOOTLA = false; if (!b_isDestinationVisible) { SensorUpdate2(dt, collisionChecking(pos + L, m_GOList), collisionChecking(pos + C, m_GOList), collisionChecking(pos + R, m_GOList)); } else { moveToDestination(dt); } } } else { if (!b_isDestinationVisible) { SensorUpdate2(dt, collisionChecking(pos + L, m_GOList), collisionChecking(pos + C, m_GOList), collisionChecking(pos + R, m_GOList)); } else { moveToDestination(dt); } } } else { if (!b_isDestinationVisible) { SensorUpdate2(dt, collisionChecking(pos + L, m_GOList), collisionChecking(pos + C, m_GOList), collisionChecking(pos + R, m_GOList)); } else { moveToDestination(dt); } } } //Enemy is not holding a weapon else { if (!b_isDestinationVisible) { SensorUpdate2(dt, collisionChecking(pos + L, m_GOList), collisionChecking(pos + C, m_GOList), collisionChecking(pos + R, m_GOList)); } else { moveToDestination(dt); } } break; } default: break; } }