1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-09-18 16:32:32 +02:00

Added primitive lane following

This commit is contained in:
Daniel Evans 2013-09-19 03:33:13 +01:00
parent fbe0738fb4
commit b15331766a
7 changed files with 51 additions and 14 deletions

View File

@ -39,7 +39,7 @@ void GTADefaultAIController::update(float dt)
}
}
}
else if( glm::length(targetNode->position - character->getPosition()) < nodeMargin ) {
else if( glm::length(getTargetPosition() - character->getPosition()) < nodeMargin ) {
if(targetNode->connections.size() > 0) {
std::uniform_int_distribution<int> uniform(0, targetNode->connections.size()-1);
GTAAINode* nextNode = nullptr; size_t i = 0;
@ -59,6 +59,13 @@ void GTADefaultAIController::update(float dt)
if( action == Wander && targetNode != nullptr ) {
auto d = targetNode->position - character->getPosition();
if(lastNode && character->getCurrentVehicle()) {
auto nDir = glm::normalize(targetNode->position - lastNode->position);
auto right = glm::cross(nDir, glm::vec3(0.f, 0.f, 1.f));
d += right * 2.2f;
}
if(character->getCurrentVehicle()) {
auto vd = glm::inverse(character->getCurrentVehicle()->getRotation()) * d;
float va = -atan2( vd.x, vd.y );
@ -83,7 +90,7 @@ void GTADefaultAIController::update(float dt)
perc = std::min(1.f, std::max(0.f, perc));
// Determine if the vehicle should reverse instead.
auto td = targetNode->position - character->getPosition();
auto td = getTargetPosition() - character->getPosition();
auto vd = character->getCurrentVehicle()->getRotation() * glm::vec3(0.f, 1.f, 0.f);
if(glm::dot(td, vd) < -0.25f) {
perc *= -1.f;
@ -97,6 +104,11 @@ void GTADefaultAIController::update(float dt)
glm::vec3 GTADefaultAIController::getTargetPosition()
{
if(targetNode) {
if(lastNode && character->getCurrentVehicle()) {
auto nDir = glm::normalize(targetNode->position - lastNode->position);
auto right = glm::cross(nDir, glm::vec3(0.f, 0.f, 1.f));
return targetNode->position + right * 2.2f;
}
return targetNode->position;
}
return glm::vec3();

View File

@ -80,6 +80,9 @@ GTAInstance::GTAInstance(
ainode->type = (path.type == LoaderIDE::PATH_PED ? GTAAINode::Pedestrian : GTAAINode::Vehicle);
ainode->nextIndex = node.next >= 0 ? startIndex + node.next : -1;
ainode->flags = GTAAINode::None;
ainode->size = node.size;
ainode->other_thing = node.other_thing;
ainode->other_thing2 = node.other_thing2;
ainode->position = position + (rotation * node.position);
ainode->external = node.type == LoaderIDE::EXTERNAL;

View File

@ -573,15 +573,10 @@ void GTARenderer::renderPaths()
}
}
else {
carlines.push_back(start->position);
if( start->external ) {
carlines.push_back(start->position+glm::vec3(0.f, 0.f, 2.f));
}
else {
carlines.push_back(start->position+glm::vec3(0.f, 0.f, 1.f));
}
carlines.push_back(start->position-glm::vec3(start->size / 2.f, 0.f, 0.f));
carlines.push_back(start->position+glm::vec3(start->size / 2.f, 0.f, 0.f));
}
for( size_t c = 0; c < start->connections.size(); ++c ) {
auto end = start->connections[c];

View File

@ -219,6 +219,15 @@ bool LoaderIDE::load(const std::string &filename)
getline(buffstream, buff, ',');
node.position.z = atof(buff.c_str()) * 1/16.f;
getline(buffstream, buff, ',');
node.size = atof(buff.c_str()) * 1/16.f;
getline(buffstream, buff, ',');
node.other_thing = atoi(buff.c_str());
getline(buffstream, buff, ',');
node.other_thing2 = atoi(buff.c_str());
path.nodes.push_back(node);
}

View File

@ -19,6 +19,9 @@ struct GTAAINode
NodeType type;
glm::vec3 position;
float size;
int other_thing;
int other_thing2;
bool external;
uint8_t flags;

View File

@ -122,6 +122,9 @@ public:
NodeType type;
int32_t next;
glm::vec3 position;
float size;
int other_thing;
int other_thing2;
};
struct PATH_t

View File

@ -170,15 +170,27 @@ void update(float dt)
if( player == nullptr ) {
playerCharacter = gta->createPedestrian(1, plyPos+glm::vec3(0.f,10.f,0.f));
player = new GTAPlayerAIController(playerCharacter);
auto vid = (gta->vehicleTypes.begin())->first;
auto vehicle = gta->createVehicle(vid, plyPos, glm::quat(glm::vec3(0.f, 0.f, -plyLook.x * PiOver180)));
// Pick random vehicle.
auto it = gta->vehicleTypes.begin();
std::uniform_int_distribution<int> uniform(0, 9);
for(size_t i = 0, n = uniform(gta->randomEngine); i != n; i++) {
it++;
}
auto vehicle = gta->createVehicle(it->first, plyPos, glm::quat(glm::vec3(0.f, 0.f, -plyLook.x * PiOver180)));
playerCharacter->setCurrentVehicle(vehicle);
}
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::U)) {
auto ped = gta->createPedestrian(2, plyPos+glm::vec3(0.f,10.f,0.f));
auto vid = (gta->vehicleTypes.begin())->first;
auto vehicle = gta->createVehicle(vid, plyPos, glm::quat(glm::vec3(0.f, 0.f, -plyLook.x * PiOver180)));
// Pick random vehicle.
auto it = gta->vehicleTypes.begin();
std::uniform_int_distribution<int> uniform(0, 9);
for(size_t i = 0, n = uniform(gta->randomEngine); i != n; i++) {
it++;
}
auto vehicle = gta->createVehicle(it->first, plyPos, glm::quat(glm::vec3(0.f, 0.f, -plyLook.x * PiOver180)));
ped->setCurrentVehicle(vehicle);
}