From c136bc2bd0c294cec2dba8e8775391832e8ea4f4 Mon Sep 17 00:00:00 2001 From: hikerstk Date: Mon, 25 Nov 2013 06:01:16 +0000 Subject: [PATCH] Fixed #1109 by adding a proper constructor to the Settings class (which will make sure all fields are initialised). git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@14576 178a84e3-b1eb-0310-8ba1-8eac791a3b58 --- src/modes/three_strikes_battle.cpp | 15 ++-- src/physics/physical_object.cpp | 112 +++++++++++++++++------------ src/physics/physical_object.hpp | 40 +++++++---- 3 files changed, 100 insertions(+), 67 deletions(-) diff --git a/src/modes/three_strikes_battle.cpp b/src/modes/three_strikes_battle.cpp index bc91087fd..cea9604bd 100644 --- a/src/modes/three_strikes_battle.cpp +++ b/src/modes/three_strikes_battle.cpp @@ -300,7 +300,7 @@ void ThreeStrikesBattle::update(float dt) std::string tire; float scale = 0.5f; float radius = 0.5f; - PhysicalObject::bodyTypes body_shape; + PhysicalObject::BodyTypes body_shape; // insert blown away tire(s) now if was requested while (m_insert_tire > 0) @@ -332,16 +332,11 @@ void ThreeStrikesBattle::update(float dt) core::vector3df tire_xyz = m_tire_position + tire_offset; core::vector3df tire_hpr = core::vector3df(800.0f,0, - m_tire_rotation / M_PI * 180 + 180); + m_tire_rotation *RAD_TO_DEGREE + 180); core::vector3df tire_scale(scale,scale,scale); - PhysicalObject::Settings physicsSettings; - physicsSettings.body_type = PhysicalObject::MP_CYLINDER_Y; - physicsSettings.crash_reset = false; - physicsSettings.knock_kart = false; - physicsSettings.mass = 15.0f; - physicsSettings.radius = radius; - physicsSettings.reset_when_too_low = false; + PhysicalObject::Settings physics_settings(PhysicalObject::MP_CYLINDER_Y, + radius, /*mass*/15.0f); TrackObjectPresentationMesh* tire_presentation = new TrackObjectPresentationMesh(tire, tire_xyz, tire_hpr, tire_scale); @@ -349,7 +344,7 @@ void ThreeStrikesBattle::update(float dt) TrackObject* tire = new TrackObject(tire_xyz, tire_hpr, tire_scale, "movable", tire_presentation, true /* is_dynamic */, - &physicsSettings); + &physics_settings); getTrack()->getTrackObjectManager()->insertObject(tire); // FIXME: orient the force relative to kart orientation diff --git a/src/physics/physical_object.cpp b/src/physics/physical_object.cpp index 6c90b014b..b72a54965 100644 --- a/src/physics/physical_object.cpp +++ b/src/physics/physical_object.cpp @@ -38,48 +38,70 @@ using namespace irr; #include #include -// ---------------------------------------------------------------------------- +/** Creates a physical Settings object with the given type, radius and mass. + */ +PhysicalObject::Settings::Settings(BodyTypes type, float radius, float mass) +{ + init(); + m_mass = mass; + m_radius = radius; +} // Settings +// ---------------------------------------------------------------------------- +/** Reads the physical settings values from a given XML node. + */ +PhysicalObject::Settings::Settings(const XMLNode &xml_node) +{ + init(); + std::string shape; + xml_node.get("mass", &m_mass ); + xml_node.get("radius", &m_radius ); + xml_node.get("shape", &shape ); + xml_node.get("reset", &m_crash_reset ); + xml_node.get("explode", &m_knock_kart ); + xml_node.get("flatten", &m_flatten_kart); + + m_reset_when_too_low = + xml_node.get("reset-when-below", &m_reset_height) == 1; + + m_body_type = MP_NONE; + if (shape=="cone" || + shape=="coneY" ) m_body_type = MP_CONE_Y; + else if(shape=="coneX" ) m_body_type = MP_CONE_X; + else if(shape=="coneZ" ) m_body_type = MP_CONE_Z; + else if(shape=="cylinder"|| + shape=="cylinderY") m_body_type = MP_CYLINDER_Y; + else if(shape=="cylinderX") m_body_type = MP_CYLINDER_X; + else if(shape=="cylinderZ") m_body_type = MP_CYLINDER_Z; + else if(shape=="box" ) m_body_type = MP_BOX; + else if(shape=="sphere" ) m_body_type = MP_SPHERE; + else if(shape=="exact" ) m_body_type = MP_EXACT; + + else + Log::error("PhysicalObject", "Unknown shape type : %s.", + shape.c_str()); +} // Settings(XMLNode) + +// ---------------------------------------------------------------------------- +/** Initialises a Settings object. + */ +void PhysicalObject::Settings::init() +{ + m_body_type = PhysicalObject::MP_NONE; + m_crash_reset = false; + m_knock_kart = false; + m_mass = -1.0f; + m_radius = -1.0f; + m_reset_when_too_low = false; + m_flatten_kart = false; +} // Settings + +// ============================================================================ PhysicalObject* PhysicalObject::fromXML(bool is_dynamic, const XMLNode &xml_node, TrackObject* object) { - PhysicalObject::Settings settings; - - settings.reset_height = 0; - settings.mass = 1; - settings.radius = -1; - settings.crash_reset = false; - settings.knock_kart = false; - settings.flatten_kart = false; - - std::string shape; - xml_node.get("mass", &settings.mass ); - xml_node.get("radius", &settings.radius ); - xml_node.get("shape", &shape ); - xml_node.get("reset", &settings.crash_reset ); - xml_node.get("explode", &settings.knock_kart ); - xml_node.get("flatten", &settings.flatten_kart); - - settings.reset_when_too_low = - xml_node.get("reset-when-below", &settings.reset_height) == 1; - - settings.body_type = MP_NONE; - if (shape=="cone" || - shape=="coneY" ) settings.body_type = MP_CONE_Y; - else if(shape=="coneX" ) settings.body_type = MP_CONE_X; - else if(shape=="coneZ" ) settings.body_type = MP_CONE_Z; - else if(shape=="cylinder"|| - shape=="cylinderY") settings.body_type = MP_CYLINDER_Y; - else if(shape=="cylinderX") settings.body_type = MP_CYLINDER_X; - else if(shape=="cylinderZ") settings.body_type = MP_CYLINDER_Z; - - else if(shape=="box" ) settings.body_type = MP_BOX; - else if(shape=="sphere" ) settings.body_type = MP_SPHERE; - else if(shape=="exact") settings.body_type = MP_EXACT; - - else fprintf(stderr, "Unknown shape type : %s\n", shape.c_str()); - + PhysicalObject::Settings settings(xml_node); return new PhysicalObject(is_dynamic, settings, object); } // fromXML @@ -107,14 +129,14 @@ PhysicalObject::PhysicalObject(bool is_dynamic, m_init_hpr = object->getRotation(); m_init_scale = object->getScale(); - m_mass = settings.mass; - m_radius = settings.radius; - m_body_type = settings.body_type; - m_crash_reset = settings.crash_reset; - m_explode_kart = settings.knock_kart; - m_flatten_kart = settings.flatten_kart; - m_reset_when_too_low = settings.reset_when_too_low; - m_reset_height = settings.reset_height; + m_mass = settings.m_mass; + m_radius = settings.m_radius; + m_body_type = settings.m_body_type; + m_crash_reset = settings.m_crash_reset; + m_explode_kart = settings.m_knock_kart; + m_flatten_kart = settings.m_flatten_kart; + m_reset_when_too_low = settings.m_reset_when_too_low; + m_reset_height = settings.m_reset_height; m_init_pos.setIdentity(); Vec3 radHpr(m_init_hpr); diff --git a/src/physics/physical_object.hpp b/src/physics/physical_object.hpp index 947817542..0cb52df59 100644 --- a/src/physics/physical_object.hpp +++ b/src/physics/physical_object.hpp @@ -39,22 +39,38 @@ class PhysicalObject { public: /** The supported collision shapes. */ - enum bodyTypes {MP_NONE, + enum BodyTypes {MP_NONE, MP_CONE_Y, MP_CONE_X, MP_CONE_Z, MP_CYLINDER_Y, MP_CYLINDER_X, MP_CYLINDER_Z, MP_BOX, MP_SPHERE, MP_EXACT}; - struct Settings + class Settings { - float mass; - float radius; - PhysicalObject::bodyTypes body_type; - bool crash_reset; - bool knock_kart; - bool flatten_kart; - bool reset_when_too_low; - float reset_height; - }; + public: + /** Mass of the object. */ + float m_mass; + /** Radius of the object. */ + float m_radius; + /** Shape of the object. */ + PhysicalObject::BodyTypes m_body_type; + /** Trigger a reset in karts touching it? */ + bool m_crash_reset; + /** Knock the kart around. */ + bool m_knock_kart; + /** Flatten the kart when this object is touched. */ + bool m_flatten_kart; + /** Reset the object when it falls under the track (useful + * e.g. for a boulder rolling down a hill). */ + bool m_reset_when_too_low; + /** If the item is below that height, it is reset (when + * m_reset_when_too_low is true). */ + float m_reset_height; + private: + void init(); + public: + Settings(BodyTypes type, float radius, float mass); + Settings(const XMLNode &xml_node); + }; // Settings private: @@ -70,7 +86,7 @@ private: TrackObject *m_object; /** The shape of this object. */ - bodyTypes m_body_type; + BodyTypes m_body_type; /** The bullet collision shape. */ btCollisionShape *m_shape;