1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-25 03:42:48 +01:00

Merge pull request #616 from ShFil119/remove_raw_ptrs_from_objects

Remove raw ptrs from game objects
This commit is contained in:
Daniel Evans 2018-09-01 23:37:42 +01:00 committed by GitHub
commit 02b42a8fed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 124 additions and 114 deletions

View File

@ -64,18 +64,18 @@ void CharacterObject::createActor(const glm::vec2& size) {
tf.setIdentity();
tf.setOrigin(btVector3(position.x, position.y, position.z));
physObject = new btPairCachingGhostObject();
physObject = std::make_unique<btPairCachingGhostObject>();
physObject->setUserPointer(this);
physObject->setWorldTransform(tf);
physShape = new btCapsuleShapeZ(size.x, size.y);
physObject->setCollisionShape(physShape);
physShape = std::make_unique<btCapsuleShapeZ>(size.x, size.y);
physObject->setCollisionShape(physShape.get());
physObject->setCollisionFlags(btCollisionObject::CF_KINEMATIC_OBJECT);
#if BT_BULLET_VERSION < 285
physCharacter =
new btKinematicCharacterController(physObject, physShape, 0.30f, 2);
physCharacter = std::make_unique<btKinematicCharacterController>(
physObject.get(), physShape.get(), 0.30f, 2);
#else
physCharacter = new btKinematicCharacterController(
physObject, physShape, 0.30f, btVector3(0.f, 0.f, 1.f));
physCharacter = std::make_unique<btKinematicCharacterController>(
physObject.get(), physShape.get(), 0.30f, btVector3(0.f, 0.f, 1.f));
#endif
physCharacter->setFallSpeed(20.f);
physCharacter->setUseGhostSweepTest(true);
@ -89,21 +89,20 @@ void CharacterObject::createActor(const glm::vec2& size) {
physCharacter->setJumpSpeed(5.f);
engine->dynamicsWorld->addCollisionObject(
physObject, btBroadphaseProxy::KinematicFilter,
physObject.get(), btBroadphaseProxy::KinematicFilter,
btBroadphaseProxy::StaticFilter | btBroadphaseProxy::SensorTrigger);
engine->dynamicsWorld->addAction(physCharacter);
engine->dynamicsWorld->addAction(physCharacter.get());
}
}
void CharacterObject::destroyActor() {
if (physCharacter) {
engine->dynamicsWorld->removeCollisionObject(physObject);
engine->dynamicsWorld->removeAction(physCharacter);
engine->dynamicsWorld->removeCollisionObject(physObject.get());
engine->dynamicsWorld->removeAction(physCharacter.get());
delete physCharacter;
delete physObject;
delete physShape;
physCharacter = nullptr;
physObject = nullptr;
physShape = nullptr;
}
}

View File

@ -81,9 +81,9 @@ private:
public:
static const float DefaultJumpSpeed;
btKinematicCharacterController* physCharacter = nullptr;
btPairCachingGhostObject* physObject = nullptr;
btCapsuleShapeZ* physShape = nullptr;
std::unique_ptr<btKinematicCharacterController> physCharacter;
std::unique_ptr<btPairCachingGhostObject> physObject;
std::unique_ptr<btCapsuleShapeZ> physShape;
CharacterController* controller;

View File

@ -98,11 +98,11 @@ PickupObject::PickupObject(GameWorld* world, const glm::vec3& position,
tf.setIdentity();
tf.setOrigin(btVector3(position.x, position.y, position.z));
m_ghost = new btPairCachingGhostObject;
m_ghost = std::make_unique<btPairCachingGhostObject>();
m_ghost->setUserPointer(this);
m_ghost->setWorldTransform(tf);
m_shape = new btSphereShape(0.5f);
m_ghost->setCollisionShape(m_shape);
m_shape = std::make_unique<btSphereShape>(0.5f);
m_ghost->setCollisionShape(m_shape.get());
m_ghost->setCollisionFlags(btCollisionObject::CF_KINEMATIC_OBJECT |
btCollisionObject::CF_NO_CONTACT_RESPONSE);
@ -162,8 +162,6 @@ PickupObject::~PickupObject() {
if (m_ghost) {
setEnabled(false);
engine->destroyEffect(m_corona);
delete m_ghost;
delete m_shape;
}
}
@ -212,7 +210,7 @@ void PickupObject::tick(float dt) {
const btBroadphasePair& pair = pairArray[i];
auto otherObject = static_cast<const btCollisionObject*>(
pair.m_pProxy0->m_clientObject == m_ghost
pair.m_pProxy0->m_clientObject == m_ghost.get()
? pair.m_pProxy1->m_clientObject
: pair.m_pProxy0->m_clientObject);
if (otherObject->getUserPointer()) {
@ -256,10 +254,10 @@ void PickupObject::tick(float dt) {
void PickupObject::setEnabled(bool enabled) {
if (!m_enabled && enabled) {
engine->dynamicsWorld->addCollisionObject(
m_ghost, btBroadphaseProxy::SensorTrigger);
m_ghost.get(), btBroadphaseProxy::SensorTrigger);
m_corona.size = glm::vec2(1.5f, 1.5f);
} else if (m_enabled && !enabled) {
engine->dynamicsWorld->removeCollisionObject(m_ghost);
engine->dynamicsWorld->removeCollisionObject(m_ghost.get());
m_corona.size = glm::vec2(0.f, 0.f);
}

View File

@ -116,8 +116,8 @@ public:
}
private:
btPairCachingGhostObject* m_ghost = nullptr;
btSphereShape* m_shape = nullptr;
std::unique_ptr<btPairCachingGhostObject> m_ghost;
std::unique_ptr<btSphereShape> m_shape;
bool m_enabled = false;
float m_enableTimer = 0.f;
bool m_collected = false;

View File

@ -95,17 +95,14 @@ void ProjectileObject::explode() {
void ProjectileObject::cleanup() {
if (_body) {
engine->dynamicsWorld->removeRigidBody(_body);
delete _body;
engine->dynamicsWorld->removeRigidBody(_body.get());
_body = nullptr;
}
if (_ghostBody) {
engine->dynamicsWorld->removeCollisionObject(_ghostBody);
delete _ghostBody;
engine->dynamicsWorld->removeCollisionObject(_ghostBody.get());
_ghostBody = nullptr;
}
if (_shape) {
delete _shape;
if(_shape) {
_shape = nullptr;
}
}
@ -114,10 +111,10 @@ ProjectileObject::ProjectileObject(GameWorld* world, const glm::vec3& position,
const ProjectileObject::ProjectileInfo& info)
: GameObject(world, position, glm::quat{1.0f,0.0f,0.0f,0.0f}, nullptr)
, _info(info) {
_shape = new btSphereShape(0.45f);
_shape = std::make_unique<btSphereShape>(0.45f);
btVector3 inertia(0.f, 0.f, 0.f);
_shape->calculateLocalInertia(1.f, inertia);
btRigidBody::btRigidBodyConstructionInfo riginfo(1.f, nullptr, _shape,
btRigidBody::btRigidBodyConstructionInfo riginfo(1.f, nullptr, _shape.get(),
inertia);
btTransform ws;
@ -126,13 +123,13 @@ ProjectileObject::ProjectileObject(GameWorld* world, const glm::vec3& position,
riginfo.m_startWorldTransform = ws;
riginfo.m_mass = 1.f;
_body = new btRigidBody(riginfo);
_body = std::make_unique<btRigidBody>(riginfo);
_body->setUserPointer(this);
_body->setLinearVelocity(
btVector3(_info.direction.x, _info.direction.y, _info.direction.z) *
_info.velocity);
engine->dynamicsWorld->addRigidBody(
_body, btBroadphaseProxy::DefaultFilter,
_body.get(), btBroadphaseProxy::DefaultFilter,
btBroadphaseProxy::StaticFilter | btBroadphaseProxy::DefaultFilter);
if (_info.type == RPG) {
@ -142,15 +139,15 @@ ProjectileObject::ProjectileObject(GameWorld* world, const glm::vec3& position,
if (_info.type != Grenade) {
// Projectiles that aren't grenades explode on contact.
_ghostBody = new btPairCachingGhostObject();
_ghostBody = std::make_unique<btPairCachingGhostObject>();
_ghostBody->setWorldTransform(_body->getWorldTransform());
_ghostBody->setCollisionShape(_shape);
_ghostBody->setCollisionShape(_shape.get());
_ghostBody->setUserPointer(this);
_ghostBody->setCollisionFlags(
btCollisionObject::CF_KINEMATIC_OBJECT |
btCollisionObject::CF_NO_CONTACT_RESPONSE);
engine->dynamicsWorld->addCollisionObject(
_ghostBody, btBroadphaseProxy::SensorTrigger,
_ghostBody.get(), btBroadphaseProxy::SensorTrigger,
btBroadphaseProxy::AllFilter);
}
}

View File

@ -38,12 +38,12 @@ public:
private:
ProjectileInfo _info;
btSphereShape* _shape;
std::unique_ptr<btSphereShape> _shape;
btRigidBody* _body = nullptr;
std::unique_ptr<btRigidBody> _body;
/** Used for RPGs and Molotov collision detection */
btPairCachingGhostObject* _ghostBody = nullptr;
std::unique_ptr<btPairCachingGhostObject> _ghostBody;
bool _exploded = false;

View File

@ -99,7 +99,8 @@ VehicleObject::VehicleObject(GameWorld* engine, const glm::vec3& pos,
collision->createPhysicsBody(this, modelinfo->getCollision(), nullptr,
&info->handling);
collision->getBulletBody()->forceActivationState(DISABLE_DEACTIVATION);
physRaycaster = new VehicleRaycaster(this, engine->dynamicsWorld.get());
physRaycaster =
std::make_unique<VehicleRaycaster>(this, engine->dynamicsWorld.get());
btRaycastVehicle::btVehicleTuning tuning;
float travel = fabs(info->handling.suspensionUpperLimit -
@ -120,10 +121,10 @@ VehicleObject::VehicleObject(GameWorld* engine, const glm::vec3& pos,
tuning.m_frictionSlip = 1.8f + maxVelocity * accelerationFloor / massFloor;
tuning.m_maxSuspensionTravelCm = travel * 100.f;
physVehicle =
new btRaycastVehicle(tuning, collision->getBulletBody(), physRaycaster);
physVehicle = std::make_unique<btRaycastVehicle>(
tuning, collision->getBulletBody(), physRaycaster.get());
physVehicle->setCoordinateSystem(0, 2, 1);
engine->dynamicsWorld->addAction(physVehicle);
engine->dynamicsWorld->addAction(physVehicle.get());
float kC = 0.5f;
float kR = 0.6f;
@ -173,14 +174,11 @@ VehicleObject::VehicleObject(GameWorld* engine, const glm::vec3& pos,
VehicleObject::~VehicleObject() {
ejectAll();
engine->dynamicsWorld->removeAction(physVehicle);
engine->dynamicsWorld->removeAction(physVehicle.get());
for (auto& p : dynamicParts) {
destroyObjectHinge(&p.second);
}
delete physVehicle;
delete physRaycaster;
}
void VehicleObject::setupModel() {
@ -272,7 +270,7 @@ void VehicleObject::setPosition(const glm::vec3& pos) {
auto bodyOrigin = btVector3(position.x, position.y, position.z);
for (auto& part : dynamicParts) {
if (part.second.body == nullptr) continue;
auto body = part.second.body;
auto body = part.second.body.get();
auto rel = body->getWorldTransform().getOrigin() - bodyOrigin;
body->getWorldTransform().setOrigin(
btVector3(pos.x + rel.x(), pos.y + rel.y(), pos.z + rel.z()));
@ -588,7 +586,7 @@ void VehicleObject::tickPhysics(float dt) {
it.second.constraint->getHingeAngle();
if (glm::abs(angledelta) <= 0.01f) {
it.second.constraint->enableAngularMotor(false, 1.f, 1.f);
dynamicParts[it.first].moveToAngle = false;
it.second.moveToAngle = false;
} else {
it.second.constraint->enableAngularMotor(
true, glm::sign(angledelta) * 5.f, 1.f);
@ -603,7 +601,7 @@ void VehicleObject::tickPhysics(float dt) {
auto d = it.second.constraint->getHingeAngle() -
it.second.closedAngle;
if (glm::abs(d) < 0.05f) {
dynamicParts[it.first].moveToAngle = false;
it.second.moveToAngle = false;
setPartLocked(&(it.second), true);
}
}
@ -752,7 +750,7 @@ bool VehicleObject::takeDamage(const GameObject::DamageInfo& dmg) {
dpoint = glm::inverse(getRotation()) * dpoint;
// Set any parts within range to damaged state.
for (auto d : dynamicParts) {
for (auto& d : dynamicParts) {
auto p = &d.second;
if (p->normal == nullptr) continue;
@ -863,10 +861,11 @@ void VehicleObject::registerPart(ModelFrame* mf) {
damage->setFlag(Atomic::ATOMIC_RENDER, false);
}
}
Part part{mf, normal, damage, nullptr, nullptr, nullptr, false,
0.f, 0.f, 0.f};
dynamicParts.insert({mf->getName(),
{mf, normal, damage, nullptr, nullptr, nullptr, false,
0.f, 0.f, 0.f}});
std::move(part)});
}
void VehicleObject::createObjectHinge(Part* part) {
@ -909,7 +908,7 @@ void VehicleObject::createObjectHinge(Part* part) {
return;
}
auto ms = new VehiclePartMotionState(this, part);
auto ms = std::make_unique<VehiclePartMotionState>(this, part);
btTransform tr = btTransform::getIdentity();
const auto& p = part->dummy->getDefaultTranslation();
@ -917,7 +916,7 @@ void VehicleObject::createObjectHinge(Part* part) {
tr.setOrigin(btVector3(p.x, p.y, p.z));
tr.setRotation(btQuaternion(o.x, o.y, o.z, o.w));
btCollisionShape* cs = new btBoxShape(boxSize);
auto cs = std::make_unique<btBoxShape>(boxSize);
btTransform t;
t.setIdentity();
t.setOrigin(boxOffset);
@ -925,41 +924,38 @@ void VehicleObject::createObjectHinge(Part* part) {
btVector3 inertia;
cs->calculateLocalInertia(10.f, inertia);
btRigidBody::btRigidBodyConstructionInfo rginfo(10.f, ms, cs, inertia);
btRigidBody* subObject = new btRigidBody(rginfo);
btRigidBody::btRigidBodyConstructionInfo rginfo(10.f, ms.get(), cs.get(), inertia);
auto subObject = std::make_unique<btRigidBody>(rginfo);
subObject->setUserPointer(this);
auto hinge = new btHingeConstraint(*collision->getBulletBody(), *subObject,
auto hinge = std::make_unique<btHingeConstraint>(*collision->getBulletBody(), *subObject,
tr.getOrigin(), hingePosition, hingeAxis,
hingeAxis);
hinge->setLimit(hingeMin, hingeMax);
hinge->setBreakingImpulseThreshold(250.f);
part->cs = cs;
part->body = subObject;
part->constraint = hinge;
part->cs = std::move(cs);
part->body = std::move(subObject);
part->motionState = std::move(ms);
part->constraint = std::move(hinge);
engine->dynamicsWorld->addRigidBody(part->body);
engine->dynamicsWorld->addConstraint(part->constraint, true);
engine->dynamicsWorld->addRigidBody(part->body.get());
engine->dynamicsWorld->addConstraint(part->constraint.get(), true);
}
void VehicleObject::destroyObjectHinge(Part* part) {
if (part->constraint != nullptr) {
engine->dynamicsWorld->removeConstraint(part->constraint);
delete part->constraint;
engine->dynamicsWorld->removeConstraint(part->constraint.get());
part->constraint = nullptr;
}
if (part->body != nullptr) {
engine->dynamicsWorld->removeCollisionObject(part->body);
delete part->body->getMotionState();
delete part->body;
delete part->cs;
engine->dynamicsWorld->removeCollisionObject(part->body.get());
part->body = nullptr;
part->motionState = nullptr;
part->cs = nullptr;
}
part->cs = nullptr;
part->body = nullptr;
part->constraint = nullptr;
// Reset target.
part->moveToAngle = false;
}

View File

@ -3,9 +3,9 @@
#include <cstddef>
#include <cstdint>
#include <map>
#include <memory>
#include <string>
#include <unordered_map>
#include <LinearMath/btScalar.h>
#include <glm/glm.hpp>
@ -15,18 +15,14 @@
#include <objects/GameObject.hpp>
#include <objects/VehicleInfo.hpp>
#include <btBulletDynamicsCommon.h>
class Atomic;
class CharacterObject;
class CollisionInstance;
class GameWorld;
class ModelFrame;
class btCollisionShape;
struct btVehicleRaycaster;
class btRaycastVehicle;
class btRigidBody;
class btHingeConstraint;
/**
* @class VehicleObject
* Implements Vehicle behaviours.
@ -55,23 +51,47 @@ public:
std::map<size_t, GameObject*> seatOccupants;
std::unique_ptr<CollisionInstance> collision;
btVehicleRaycaster* physRaycaster = nullptr;
btRaycastVehicle* physVehicle = nullptr;
std::unique_ptr<btVehicleRaycaster> physRaycaster;
std::unique_ptr<btRaycastVehicle> physVehicle;
struct Part {
Part(ModelFrame* p_dummy, Atomic* p_normal, Atomic* p_damaged,
std::unique_ptr<btCollisionShape> p_cs,
std::unique_ptr<btRigidBody> p_body,
std::unique_ptr<btHingeConstraint> p_constraint,
bool p_moveToAngle, float p_targetAngle, float p_openAngle,
float p_closedAngle)
: dummy(p_dummy)
, normal(p_normal)
, damaged(p_damaged)
, cs(std::move(p_cs))
, body(std::move(p_body))
, constraint(std::move(p_constraint))
, moveToAngle(p_moveToAngle)
, targetAngle(p_targetAngle)
, openAngle(p_openAngle)
, closedAngle(p_closedAngle) {
}
Part(Part&& part) = default;
Part& operator=(Part&& part) = default;
~Part() = default;
ModelFrame* dummy;
Atomic* normal;
Atomic* damaged;
btCollisionShape* cs;
btRigidBody* body;
btHingeConstraint* constraint;
std::unique_ptr<btCollisionShape> cs;
std::unique_ptr<btRigidBody> body;
std::unique_ptr<btMotionState> motionState;
std::unique_ptr<btHingeConstraint> constraint;
bool moveToAngle;
float targetAngle;
float openAngle;
float closedAngle;
};
std::map<std::string, Part> dynamicParts;
std::unordered_map<std::string, Part> dynamicParts;
VehicleObject(GameWorld* engine, const glm::vec3& pos, const glm::quat& rot,
BaseModelInfo* modelinfo, VehicleInfoHandle info,

View File

@ -425,7 +425,7 @@ const ViewCamera& IngameState::getCamera(float alpha) {
auto target = getCameraTarget();
bool lookleft = held(GameInputState::LookLeft);
bool lookright = held(GameInputState::LookRight);
btCollisionObject* physTarget = player->getCharacter()->physObject;
btCollisionObject* physTarget = player->getCharacter()->physObject.get();
auto targetTransform = target->getTimeAdjustedTransform(alpha);

View File

@ -10,10 +10,10 @@ BOOST_AUTO_TEST_CASE(test_create_vehicle) {
VehicleObject* vehicle =
Global::get().e->createVehicle(90u, glm::vec3(), glm::quat{1.0f,0.0f,0.0f,0.0f});
BOOST_REQUIRE(vehicle != nullptr);
BOOST_REQUIRE(vehicle);
BOOST_REQUIRE(vehicle->info != nullptr);
BOOST_REQUIRE(vehicle->getVehicle() != nullptr);
BOOST_REQUIRE(vehicle->info);
BOOST_REQUIRE(vehicle->getVehicle());
// Hardcoded values for the moment
BOOST_CHECK_EQUAL(vehicle->getVehicle()->vehicletype_,
@ -30,15 +30,15 @@ BOOST_AUTO_TEST_CASE(vehicle_parts) {
VehicleObject* vehicle =
Global::get().e->createVehicle(90u, glm::vec3(), glm::quat{1.0f,0.0f,0.0f,0.0f});
BOOST_REQUIRE(vehicle != nullptr);
BOOST_REQUIRE(vehicle->getModel() != nullptr);
BOOST_REQUIRE(vehicle);
BOOST_REQUIRE(vehicle->getModel());
VehicleObject::Part* part = vehicle->getPart("bonnet_dummy");
BOOST_REQUIRE(part != nullptr);
BOOST_REQUIRE(part);
BOOST_REQUIRE(part->normal != nullptr);
BOOST_REQUIRE(part->damaged != nullptr);
BOOST_REQUIRE(part->normal);
BOOST_REQUIRE(part->damaged);
BOOST_REQUIRE(part->normal->getFrame());
BOOST_REQUIRE(part->damaged->getFrame());
@ -53,8 +53,8 @@ BOOST_AUTO_TEST_CASE(vehicle_part_vis) {
VehicleObject* vehicle =
Global::get().e->createVehicle(90u, glm::vec3(), glm::quat{1.0f,0.0f,0.0f,0.0f});
BOOST_REQUIRE(vehicle != nullptr);
BOOST_REQUIRE(vehicle->getModel() != nullptr);
BOOST_REQUIRE(vehicle);
BOOST_REQUIRE(vehicle->getModel());
VehicleObject::Part* bonnetpart = vehicle->getPart("bonnet_dummy");
@ -75,10 +75,10 @@ BOOST_AUTO_TEST_CASE(test_door_position) {
VehicleObject* vehicle = Global::get().e->createVehicle(
90u, glm::vec3(10.f, 0.f, 0.f), glm::quat{1.0f,0.0f,0.0f,0.0f});
BOOST_REQUIRE(vehicle != nullptr);
BOOST_REQUIRE(vehicle);
BOOST_REQUIRE(vehicle->info != nullptr);
BOOST_REQUIRE(vehicle->getVehicle() != nullptr);
BOOST_REQUIRE(vehicle->info);
BOOST_REQUIRE(vehicle->getVehicle());
BOOST_CHECK(vehicle->getSeatEntryPositionWorld(0).x > 5.f);
@ -93,20 +93,20 @@ BOOST_AUTO_TEST_CASE(test_hinges) {
VehicleObject::Part* part = vehicle->getPart("door_lf_dummy");
BOOST_REQUIRE(part != nullptr);
BOOST_REQUIRE(part);
BOOST_CHECK_EQUAL(part->constraint, nullptr);
BOOST_CHECK_EQUAL(part->body, nullptr);
BOOST_CHECK(!part->constraint);
BOOST_CHECK(!part->body);
vehicle->setPartLocked(part, false);
BOOST_CHECK_NE(part->body, nullptr);
BOOST_CHECK_NE(part->constraint, nullptr);
BOOST_CHECK(part->body);
BOOST_CHECK(part->constraint);
vehicle->setPartLocked(part, true);
BOOST_CHECK_EQUAL(part->constraint, nullptr);
BOOST_CHECK_EQUAL(part->body, nullptr);
BOOST_CHECK(!part->constraint);
BOOST_CHECK(!part->body);
Global::get().e->destroyObject(vehicle);
}
@ -115,13 +115,13 @@ BOOST_AUTO_TEST_CASE(test_open_part) {
VehicleObject* vehicle = Global::get().e->createVehicle(
90u, glm::vec3(10.f, 0.f, 0.f), glm::quat{1.0f,0.0f,0.0f,0.0f});
BOOST_REQUIRE(vehicle != nullptr);
BOOST_REQUIRE(vehicle);
VehicleObject::Part* part = vehicle->getPart("door_lf_dummy");
BOOST_REQUIRE(part != nullptr);
BOOST_REQUIRE(part);
BOOST_CHECK_EQUAL(part->body, nullptr);
BOOST_CHECK(!part->body);
vehicle->setPartLocked(part, true);
vehicle->setPartTarget(part, true, 1.f);