mirror of
https://github.com/rwengine/openrw.git
synced 2024-11-22 18:32:44 +01:00
Fixed Instance rotation
This commit is contained in:
parent
6445bc50e9
commit
f7ae75876e
@ -4,7 +4,7 @@
|
||||
#include <renderwure/render/Model.hpp>
|
||||
|
||||
Animator::Animator()
|
||||
: animation(nullptr), model(nullptr), time(0.f), serverTime(0.f), lastServerTime(0.f)
|
||||
: animation(nullptr), model(nullptr), time(0.f), serverTime(0.f), lastServerTime(0.f), repeat(true)
|
||||
{
|
||||
|
||||
}
|
||||
@ -46,13 +46,14 @@ void Animator::reset()
|
||||
}
|
||||
}
|
||||
|
||||
void Animator::setAnimation(Animation *animation)
|
||||
void Animator::setAnimation(Animation *animation, bool repeat)
|
||||
{
|
||||
if(animation == this->animation) {
|
||||
return;
|
||||
}
|
||||
|
||||
this->animation = animation;
|
||||
this->repeat = repeat;
|
||||
|
||||
updateFrameMapping();
|
||||
reset();
|
||||
@ -101,7 +102,13 @@ void Animator::tick(float dt)
|
||||
fmat[3] = glm::vec4(model->frames[fi].defaultTranslation, 1.f);
|
||||
|
||||
if( animation && fi < model->frameNames.size() ) {
|
||||
serverTime = fmod(serverTime, animation->duration);
|
||||
if(repeat) {
|
||||
serverTime = fmod(serverTime, animation->duration);
|
||||
}
|
||||
else {
|
||||
serverTime = std::min(serverTime, animation->duration);
|
||||
}
|
||||
|
||||
if( lastServerTime > serverTime ) {
|
||||
lastRootPosition = glm::vec3(0.f);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
GTACharacter::GTACharacter(GTAEngine* engine, const glm::vec3& pos, const glm::quat& rot, Model* model, std::shared_ptr<LoaderIDE::PEDS_t> ped)
|
||||
: GTAObject(engine, pos, rot, model),
|
||||
currentVehicle(nullptr),
|
||||
currentVehicle(nullptr), physCharacter(nullptr),
|
||||
ped(ped), currentActivity(None), controller(nullptr)
|
||||
{
|
||||
if(model) {
|
||||
@ -23,8 +23,12 @@ GTACharacter::~GTACharacter()
|
||||
destroyActor();
|
||||
}
|
||||
|
||||
void GTACharacter::createActor()
|
||||
void GTACharacter::createActor(const glm::vec3& size)
|
||||
{
|
||||
if(physCharacter) {
|
||||
destroyActor();
|
||||
}
|
||||
|
||||
// Don't create anything without a valid model.
|
||||
if(model) {
|
||||
btTransform tf;
|
||||
@ -33,7 +37,7 @@ void GTACharacter::createActor()
|
||||
|
||||
physObject = new btPairCachingGhostObject;
|
||||
physObject->setWorldTransform(tf);
|
||||
physShape = new btBoxShape(btVector3(0.25f, 0.25f, 1.f));
|
||||
physShape = new btBoxShape(btVector3(size.x, size.y, size.z));
|
||||
physObject->setCollisionShape(physShape);
|
||||
physObject->setCollisionFlags(btCollisionObject::CF_CHARACTER_OBJECT);
|
||||
physCharacter = new btKinematicCharacterController(physObject, physShape, 0.65f, 2);
|
||||
@ -64,14 +68,22 @@ void GTACharacter::changeAction(Activity newAction)
|
||||
switch( currentActivity ) {
|
||||
default:
|
||||
case Idle:
|
||||
createActor();
|
||||
animator->setAnimation(engine->gameData.animations.at("idle_stance"));
|
||||
break;
|
||||
case Walk:
|
||||
createActor();
|
||||
animator->setAnimation(engine->gameData.animations.at("walk_civi"));
|
||||
break;
|
||||
case Run:
|
||||
createActor();
|
||||
animator->setAnimation(engine->gameData.animations.at("run_civi"));
|
||||
break;
|
||||
case KnockedDown:
|
||||
// Change body shape.
|
||||
createActor(glm::vec3(0.5f, 0.5f, 0.1f));
|
||||
animator->setAnimation(engine->gameData.animations.at("kd_front"), false);
|
||||
break;
|
||||
case VehicleDrive:
|
||||
animator->setAnimation(engine->gameData.animations.at("car_sit"));
|
||||
break;
|
||||
@ -95,6 +107,56 @@ void GTACharacter::tick(float dt)
|
||||
void GTACharacter::updateCharacter()
|
||||
{
|
||||
if(physCharacter) {
|
||||
|
||||
// Check to see if the character should be knocked down.
|
||||
btManifoldArray manifoldArray;
|
||||
btBroadphasePairArray& pairArray = physObject->getOverlappingPairCache()->getOverlappingPairArray();
|
||||
int numPairs = pairArray.size();
|
||||
|
||||
for (int i=0;i<numPairs;i++)
|
||||
{
|
||||
manifoldArray.clear();
|
||||
|
||||
const btBroadphasePair& pair = pairArray[i];
|
||||
|
||||
//unless we manually perform collision detection on this pair, the contacts are in the dynamics world paircache:
|
||||
btBroadphasePair* collisionPair = engine->dynamicsWorld->getPairCache()->findPair(pair.m_pProxy0,pair.m_pProxy1);
|
||||
if (!collisionPair)
|
||||
continue;
|
||||
|
||||
if (collisionPair->m_algorithm)
|
||||
collisionPair->m_algorithm->getAllContactManifolds(manifoldArray);
|
||||
|
||||
for (int j=0;j<manifoldArray.size();j++)
|
||||
{
|
||||
btPersistentManifold* manifold = manifoldArray[j];
|
||||
btScalar directionSign = manifold->getBody0() == physObject ? btScalar(-1.0) : btScalar(1.0);
|
||||
for (int p=0;p<manifold->getNumContacts();p++)
|
||||
{
|
||||
const btManifoldPoint&pt = manifold->getContactPoint(p);
|
||||
if (pt.getDistance() < 0.f)
|
||||
{
|
||||
const btVector3& ptA = pt.getPositionWorldOnA();
|
||||
const btVector3& ptB = pt.getPositionWorldOnB();
|
||||
const btVector3& normalOnB = pt.m_normalWorldOnB;
|
||||
|
||||
auto otherObject = static_cast<btCollisionObject*>(
|
||||
manifold->getBody0() == physObject ? manifold->getBody1() : manifold->getBody0());
|
||||
if(otherObject->getUserPointer()) {
|
||||
GTAObject* object = static_cast<GTAObject*>(otherObject->getUserPointer());
|
||||
if(object->type() == Vehicle) {
|
||||
GTAVehicle* vehicle = static_cast<GTAVehicle*>(object);
|
||||
if(vehicle->physBody->getLinearVelocity().length() > 0.1f) {
|
||||
changeAction(KnockedDown);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
glm::vec3 direction = rotation * animator->getRootTranslation();
|
||||
physCharacter->setWalkDirection(btVector3(direction.x, direction.y, direction.z));
|
||||
|
||||
|
@ -19,7 +19,7 @@ GTAInstance::GTAInstance(
|
||||
btDefaultMotionState* msta = new btDefaultMotionState;
|
||||
msta->setWorldTransform(btTransform(
|
||||
btQuaternion(
|
||||
rot.w, rot.x, rot.y, rot.z
|
||||
rot.x, rot.y, rot.z, -rot.w
|
||||
).inverse(),
|
||||
btVector3(
|
||||
pos.x, pos.y, pos.z
|
||||
|
@ -63,6 +63,7 @@ GTAVehicle::GTAVehicle(GTAEngine* engine, const glm::vec3& pos, const glm::quat&
|
||||
btRigidBody::btRigidBodyConstructionInfo rginfo(info.handling.mass, msta, cmpShape, inertia);
|
||||
|
||||
physBody = new btRigidBody(rginfo);
|
||||
physBody->setUserPointer(this);
|
||||
engine->dynamicsWorld->addRigidBody(physBody);
|
||||
|
||||
physRaycaster = new btDefaultVehicleRaycaster(engine->dynamicsWorld);
|
||||
|
@ -30,6 +30,8 @@ class Animator
|
||||
float time;
|
||||
float serverTime;
|
||||
float lastServerTime;
|
||||
|
||||
bool repeat;
|
||||
|
||||
void updateFrameMapping();
|
||||
void reset();
|
||||
@ -41,9 +43,10 @@ public:
|
||||
/**
|
||||
* @brief setAnimation Sets the currently active animation.
|
||||
* @param animation
|
||||
* @param repeat If true animation will restart after ending.
|
||||
* @todo Interpolate between the new and old frames.
|
||||
*/
|
||||
void setAnimation(Animation* animation);
|
||||
void setAnimation(Animation* animation, bool repeat = true);
|
||||
|
||||
void setModel(Model* model);
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
#ifndef _GTAOBJECTS_HPP_
|
||||
#define _GTAOBJECTS_HPP_
|
||||
#ifndef _GTAOBJECT_HPP_
|
||||
#define _GTAOBJECT_HPP_
|
||||
|
||||
#include <renderwure/engine/GTATypes.hpp>
|
||||
#include <renderwure/loaders/LoaderIDE.hpp>
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include <bullet/BulletDynamics/Character/btKinematicCharacterController.h>
|
||||
#include <bullet/btBulletCollisionCommon.h>
|
||||
#include <BulletCollision/CollisionDispatch/btGhostObject.h>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
class GTAVehicle;
|
||||
|
||||
@ -17,7 +18,7 @@ struct GTACharacter : public GTAObject
|
||||
private:
|
||||
GTAVehicle* currentVehicle;
|
||||
|
||||
void createActor();
|
||||
void createActor(const glm::vec3& size = glm::vec3(0.25f, 0.25f, 1.f));
|
||||
void destroyActor();
|
||||
|
||||
public:
|
||||
@ -29,7 +30,8 @@ public:
|
||||
Run,
|
||||
Crouch,
|
||||
VehicleDrive,
|
||||
VehicleSit
|
||||
VehicleSit,
|
||||
KnockedDown
|
||||
};
|
||||
|
||||
std::shared_ptr<LoaderIDE::PEDS_t> ped;
|
||||
|
Loading…
Reference in New Issue
Block a user