1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-23 02:42:39 +01:00

Player AI Controller

This commit is contained in:
Daniel Evans 2013-08-12 21:35:23 +00:00
parent a9260e4e40
commit b344971ef9
6 changed files with 105 additions and 6 deletions

View File

@ -17,6 +17,7 @@ SET(RENDERWARE_SOURCES
GTAAINode.cpp
GTAAIController.cpp
GTADefaultAIController.cpp
GTAPlayerAIController.cpp
GTAEngine.cpp
@ -46,6 +47,7 @@ SET(RENDERWARE_HEADERS
include/renderwure/ai/GTAAINode.hpp
include/renderwure/ai/GTAAIController.hpp
include/renderwure/ai/GTADefaultAIController.hpp
include/renderwure/ai/GTAPlayerAIController.hpp
include/renderwure/render/GTARenderer.hpp
include/renderwure/render/DebugDraw.hpp

View File

@ -341,7 +341,7 @@ void GTAEngine::createVehicle(const uint16_t id, const glm::vec3& pos, const glm
}
}
void GTAEngine::createPedestrian(const uint16_t id, const glm::vec3 &pos, const glm::quat& rot)
GTACharacter* GTAEngine::createPedestrian(const uint16_t id, const glm::vec3 &pos, const glm::quat& rot)
{
auto pti = pedestrianTypes.find(id);
if( pti != pedestrianTypes.end() ) {
@ -368,5 +368,7 @@ void GTAEngine::createPedestrian(const uint16_t id, const glm::vec3 &pos, const
new GTADefaultAIController(ped);
dynamicsWorld->addCollisionObject(ped->physObject, btBroadphaseProxy::CharacterFilter, btBroadphaseProxy::StaticFilter|btBroadphaseProxy::DefaultFilter);
dynamicsWorld->addAction(ped->physCharacter);
return ped;
}
return nullptr;
}

View File

@ -0,0 +1,43 @@
#include "renderwure/ai/GTAPlayerAIController.hpp"
#include <renderwure/engine/GTAObjects.hpp>
GTAPlayerAIController::GTAPlayerAIController(GTACharacter* character)
: GTAAIController(character), running(false)
{
}
void GTAPlayerAIController::setRunning(bool run)
{
running = run;
}
void GTAPlayerAIController::updateCameraDirection(const glm::quat& rot)
{
cameraRotation = rot;
}
void GTAPlayerAIController::updateMovementDirection(const glm::vec3& dir)
{
direction = dir;
}
void GTAPlayerAIController::update(float dt)
{
if( glm::length(direction) > 0.001f ) {
character->changeAction(running ? GTACharacter::Run : GTACharacter::Walk);
}
else {
character->changeAction(GTACharacter::Idle);
}
}
glm::vec3 GTAPlayerAIController::getTargetPosition()
{
return glm::vec3();
}
glm::quat GTAPlayerAIController::getTargetRotation()
{
return cameraRotation * glm::quat(glm::vec3(0.f, 0.f, -atan2(direction.x, direction.y)));
}

View File

@ -0,0 +1,32 @@
#pragma once
#ifndef _GTAPLAYERAICONTROLLER_HPP_
#define _GTAPLAYERAICONTROLLER_HPP_
#include "renderwure/ai/GTAAIController.hpp"
class GTAPlayerAIController : public GTAAIController
{
glm::quat cameraRotation;
glm::vec3 direction;
bool running;
public:
GTAPlayerAIController(GTACharacter* character);
void setRunning(bool run);
void updateCameraDirection(const glm::quat& rot);
void updateMovementDirection(const glm::vec3& pos);
virtual void update(float dt);
virtual glm::vec3 getTargetPosition();
virtual glm::quat getTargetRotation();
};
#endif

View File

@ -85,7 +85,7 @@ public:
/**
* Creates a pedestrian.
*/
void createPedestrian(const uint16_t id, const glm::vec3& pos, const glm::quat& rot = glm::quat());
GTACharacter* createPedestrian(const uint16_t id, const glm::vec3& pos, const glm::quat& rot = glm::quat());
/**
* Roughly the middle of everything

View File

@ -5,6 +5,7 @@
#include <renderwure/loaders/LoaderDFF.hpp>
#include <renderwure/render/DebugDraw.hpp>
#include <renderwure/ai/GTAAIController.hpp>
#include <renderwure/ai/GTAPlayerAIController.hpp>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
@ -25,6 +26,9 @@ sf::RenderWindow window;
GTAEngine* gta = nullptr;
GTAPlayerAIController* player = nullptr;
GTACharacter* playerCharacter = nullptr;
DebugDraw* debugDrawer = nullptr;
glm::vec3 plyPos;
@ -149,17 +153,33 @@ void update(float dt)
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
movement.x = 1;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::H)) {
if( player == nullptr ) {
playerCharacter = gta->createPedestrian(1, plyPos);
player = new GTAPlayerAIController(playerCharacter);
}
}
glm::mat4 view;
view = glm::rotate(view, -90.f, glm::vec3(1, 0, 0));
view = glm::rotate(view, plyLook.y, glm::vec3(1, 0, 0));
view = glm::rotate(view, plyLook.x, glm::vec3(0, 0, 1));
if (glm::length(movement) > 0.f) {
plyPos += dt * moveSpeed * (glm::inverse(glm::mat3(view)) * movement);
if( player != nullptr ) {
glm::quat playerCamera(glm::vec3(0.f, 0.f, -plyLook.x * PiOver180));
player->updateCameraDirection(playerCamera);
player->updateMovementDirection(movement);
player->setRunning(moveSpeed > 21.f);
glm::vec3 localView = glm::inverse(glm::mat3(view)) * glm::vec3(0.f, -0.5f, -2.5f);
view = glm::translate(view, -playerCharacter->position + localView);
}
else {
if (glm::length(movement) > 0.f) {
plyPos += dt * moveSpeed * (glm::inverse(glm::mat3(view)) * movement);
}
view = glm::translate(view, -plyPos);
}
view = glm::translate(view, -plyPos);
gta->gameTime += dt;