Add back ability to programmatically add track objects, use it in 3 strikes

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@12610 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
auria 2013-04-05 01:30:34 +00:00
parent f296461d21
commit 61720b2661
10 changed files with 220 additions and 88 deletions

View File

@ -32,6 +32,7 @@
#include "physics/physical_object.hpp"
#include "physics/triangle_mesh.hpp"
#include "tracks/bezier_curve.hpp"
#include "tracks/track_object.hpp"
#include "utils/constants.hpp"
#include <ISceneManager.h>
#include <IMeshSceneNode.h>

View File

@ -301,7 +301,7 @@ void ThreeStrikesBattle::update(float dt)
std::string tire;
float scale = 0.5f;
float radius = 0.5f;
PhysicalObject::bodyTypes tire_model;
PhysicalObject::bodyTypes body_shape;
// insert blown away tire(s) now if was requested
while (m_insert_tire > 0)
@ -312,12 +312,12 @@ void ThreeStrikesBattle::update(float dt)
tire = file_manager->getModelFile("tire.b3d");
scale = 0.5f;
radius = 0.5f;
tire_model = PhysicalObject::MP_CYLINDER_Y;
body_shape = PhysicalObject::MP_CYLINDER_Y;
}
else
{
scale = 1.0f;
tire_model = PhysicalObject::MP_CYLINDER_X;
body_shape = PhysicalObject::MP_CYLINDER_X;
radius = m_tire_radius[m_insert_tire-2];
tire_offset = m_tire_offsets[m_insert_tire-2];
if (m_insert_tire == 2)
@ -330,28 +330,36 @@ void ThreeStrikesBattle::update(float dt)
tire = m_tire_dir+"/wheel-rear-right.b3d";
}
// TODO: add back tires
#if 0
TrackObjectManager* tom = m_track->getTrackObjectManager();
PhysicalObject* obj =
tom->insertObject(tire,
tire_model,
15 /* mass */,
radius /* radius */,
core::vector3df(800.0f,0,m_tire_rotation
/ M_PI * 180 + 180) ,
m_tire_position + tire_offset,
core::vector3df(scale,scale,scale) /* scale */);
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);
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;
TrackObjectPresentationMesh* tire_presentation =
new TrackObjectPresentationMesh(tire, tire_xyz, tire_hpr, tire_scale);
TrackObject* tire = new TrackObject(tire_xyz, tire_hpr, tire_scale,
"movable", tire_presentation, true /* kinetic */,
&physicsSettings);
getTrack()->getTrackObjectManager()->insertObject(tire);
// FIXME: orient the force relative to kart orientation
obj->getBody()->applyCentralForce(btVector3(60.0f, 0.0f, 0.0f));
tire->getPhysics()->getBody()->applyCentralForce(btVector3(60.0f, 0.0f, 0.0f));
m_insert_tire--;
if(m_insert_tire == 1)
m_insert_tire = 0;
m_tires.push_back(obj);
#endif
m_tires.push_back(tire);
}
} // update

View File

@ -32,6 +32,7 @@ using namespace irr;
#include "physics/physics.hpp"
#include "physics/triangle_mesh.hpp"
#include "tracks/track.hpp"
#include "tracks/track_object.hpp"
#include "utils/constants.hpp"
#include "utils/string_utils.hpp"
@ -40,7 +41,49 @@ using namespace irr;
// ----------------------------------------------------------------------------
PhysicalObject::PhysicalObject(bool kinetic, const XMLNode &xml_node,
PhysicalObject* PhysicalObject::fromXML(bool kinetic, 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;
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 );
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());
return new PhysicalObject(kinetic, settings, object);
} // PhysicalObject
// ----------------------------------------------------------------------------
PhysicalObject::PhysicalObject(bool kinetic, const PhysicalObject::Settings& settings,
TrackObject* object)
{
m_shape = NULL;
@ -59,30 +102,13 @@ PhysicalObject::PhysicalObject(bool kinetic, const XMLNode &xml_node,
m_init_hpr = object->getRotation();
m_init_scale = object->getScale();
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_explode_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 fprintf(stderr, "Unknown shape type : %s\n", shape.c_str());
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_reset_when_too_low = settings.reset_when_too_low;
m_reset_height = settings.reset_height;
m_init_pos.setIdentity();
Vec3 radHpr(m_init_hpr);

View File

@ -24,11 +24,11 @@
#include "btBulletDynamicsCommon.h"
#include "physics/user_pointer.hpp"
#include "tracks/track_object.hpp"
#include "utils/vec3.hpp"
#include "utils/leak_check.hpp"
class XMLNode;
class TrackObject;
/**
* \ingroup physics
@ -41,7 +41,18 @@ public:
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
{
float mass;
float radius;
PhysicalObject::bodyTypes body_type;
bool crash_reset;
bool knock_kart;
bool reset_when_too_low;
float reset_height;
};
private:
/** The initial XYZ position of the object. */
@ -110,8 +121,11 @@ private:
TriangleMesh *m_triangle_mesh;
public:
PhysicalObject(bool kinetic, const XMLNode &node,
TrackObject* object);
PhysicalObject(bool kinetic, const Settings& settings,
TrackObject* object);
static PhysicalObject* fromXML(bool kinetic, const XMLNode &node,
TrackObject* object);
/*
PhysicalObject(const std::string& model,

View File

@ -48,6 +48,35 @@ TrackObject::TrackObject(const XMLNode &xml_node, LODNode* lod_node)
init(xml_node, lod_node);
}
// ----------------------------------------------------------------------------
TrackObject::TrackObject(const core::vector3df& xyz, const core::vector3df& hpr,
const core::vector3df& scale, const char* interaction,
TrackObjectPresentation* presentation,
bool kinetic, const PhysicalObject::Settings* physicsSettings)
{
m_init_xyz = xyz;
m_init_hpr = hpr;
m_init_scale = scale;
m_enabled = true;
m_presentation = NULL;
m_animator = NULL;
m_rigid_body = NULL;
m_interaction = interaction;
m_presentation = presentation;
if (m_interaction != "ghost" && m_interaction != "none" && physicsSettings != NULL)
{
m_rigid_body = new PhysicalObject(kinetic,
*physicsSettings,
this);
}
reset();
}
// ----------------------------------------------------------------------------
void TrackObject::init(const XMLNode &xml_node, LODNode* lod_node)
@ -117,9 +146,9 @@ void TrackObject::init(const XMLNode &xml_node, LODNode* lod_node)
if (m_interaction != "ghost" && m_interaction != "none")
{
m_rigid_body = new PhysicalObject(type == "movable",
xml_node,
this);
m_rigid_body = PhysicalObject::fromXML(type == "movable",
xml_node,
this);
}
}

View File

@ -22,6 +22,7 @@
#include <vector3d.h>
#include "items/item.hpp"
#include "physics/physical_object.hpp"
#include "tracks/track_object_presentation.hpp"
#include "utils/cpp2011.h"
#include "utils/no_copy.hpp"
@ -29,7 +30,6 @@
#include <string>
class XMLNode;
class PhysicalObject;
class ThreeDAnimation;
@ -85,10 +85,14 @@ public:
TrackObject(const XMLNode &xml_node, LODNode* lodNode);
TrackObject();
/*
TrackObject(const core::vector3df& pos, const core::vector3df& hpr,
const core::vector3df& scale, const std::string& model);
*/
/**
* @param kinetic Only if interaction == 'movable'
* @param physicsSettings If interaction != 'ghost'
*/
TrackObject(const core::vector3df& xyz, const core::vector3df& hpr,
const core::vector3df& scale, const char* interaction,
TrackObjectPresentation* presentation,
bool kinetic, const PhysicalObject::Settings* physicsSettings);
~TrackObject();
virtual void update(float dt);
virtual void reset();

View File

@ -255,23 +255,9 @@ void TrackObjectManager::enableFog(bool enable)
// ----------------------------------------------------------------------------
TrackObject* TrackObjectManager::insertObject(const std::string& model,
PhysicalObject::bodyTypes shape,
float mass, float radius,
const core::vector3df& hpr,
const core::vector3df& pos,
const core::vector3df& scale)
void TrackObjectManager::insertObject(TrackObject* object)
{
/*
PhysicalObject* object = new PhysicalObject(model, shape, mass, radius,
hpr, pos, scale);
object->init();
m_all_objects.push_back(object);
return object;
*/
assert(false);
// TODO
return NULL;
}
// ----------------------------------------------------------------------------

View File

@ -64,12 +64,7 @@ public:
/** Enable or disable fog on objects */
void enableFog(bool enable);
TrackObject* insertObject(const std::string& model,
PhysicalObject::bodyTypes shape,
float mass, float radius,
const core::vector3df& hpr,
const core::vector3df& pos,
const core::vector3df& scale);
void insertObject(TrackObject* object);
void removeObject(TrackObject* who);

View File

@ -55,7 +55,6 @@ TrackObjectPresentation::TrackObjectPresentation(const XMLNode& xml_node)
// ----------------------------------------------------------------------------
const core::vector3df& TrackObjectPresentationSceneNode::getPosition() const
{
return m_node->getPosition();
@ -139,11 +138,12 @@ TrackObjectPresentationMesh::TrackObjectPresentationMesh(const XMLNode& xml_node
xml_node.get("model", &model_name );
std::string full_path =
World::getWorld()->getTrack()->getTrackFile(model_name);
bool animated = (UserConfigParams::m_graphical_effects ||
World::getWorld()->getIdent() == IDENT_CUSTSCENE);
World::getWorld()->getTrack()->getTrackFile(model_name);
bool animated = (UserConfigParams::m_graphical_effects ||
World::getWorld()->getIdent() == IDENT_CUSTSCENE);
if (file_manager->fileExists(full_path))
{
if (animated)
@ -156,7 +156,7 @@ TrackObjectPresentationMesh::TrackObjectPresentationMesh(const XMLNode& xml_node
}
}
if(!m_mesh)
if (!m_mesh)
{
// If the model isn't found in the track directory, look
// in STK's model directory.
@ -168,7 +168,47 @@ TrackObjectPresentationMesh::TrackObjectPresentationMesh(const XMLNode& xml_node
throw std::runtime_error("Model '" + model_name + "' cannot be found");
}
}
init(&xml_node, enabled);
}
TrackObjectPresentationMesh::TrackObjectPresentationMesh(
const std::string& model_file, const core::vector3df& xyz,
const core::vector3df& hpr, const core::vector3df& scale) :
TrackObjectPresentationSceneNode(xyz, hpr, scale)
{
m_is_looped = false;
m_mesh = NULL;
m_node = NULL;
bool animated = (UserConfigParams::m_graphical_effects ||
World::getWorld()->getIdent() == IDENT_CUSTSCENE);
if (file_manager->fileExists(model_file))
{
if (animated)
{
m_mesh = irr_driver->getAnimatedMesh(model_file);
}
else
{
m_mesh = irr_driver->getMesh(model_file);
}
}
if (!m_mesh)
{
throw std::runtime_error("Model '" + model_file + "' cannot be found");
}
init(NULL, true);
}
void TrackObjectPresentationMesh::init(const XMLNode* xml_node, bool enabled)
{
bool animated = (UserConfigParams::m_graphical_effects ||
World::getWorld()->getIdent() == IDENT_CUSTSCENE);
m_mesh->grab();
irr_driver->grabAllTextures(m_mesh);
@ -179,10 +219,12 @@ TrackObjectPresentationMesh::TrackObjectPresentationMesh(const XMLNode& xml_node
m_node = node;
m_frame_start = node->getStartFrame();
xml_node.get("frame-start", &m_frame_start);
if (xml_node != NULL)
xml_node->get("frame-start", &m_frame_start);
m_frame_end = node->getEndFrame();
xml_node.get("frame-end", &m_frame_end);
if (xml_node != NULL)
xml_node->get("frame-end", &m_frame_end);
}
else
{
@ -190,10 +232,10 @@ TrackObjectPresentationMesh::TrackObjectPresentationMesh(const XMLNode& xml_node
m_frame_start = 0;
m_frame_end = 0;
}
#ifdef DEBUG
std::string debug_name = model_name+" (track-object)";
m_node->setName(debug_name.c_str());
#endif
//#ifdef DEBUG
// std::string debug_name = model_name+" (track-object)";
// m_node->setName(debug_name.c_str());
//#endif
if(!enabled)
m_node->setVisible(false);

View File

@ -42,6 +42,17 @@ protected:
public:
TrackObjectPresentation(const XMLNode& xml_node);
TrackObjectPresentation(
const core::vector3df& xyz,
const core::vector3df& hpr,
const core::vector3df& scale)
{
m_init_xyz = xyz;
m_init_hpr = hpr;
m_init_scale = scale;
}
virtual ~TrackObjectPresentation() {}
virtual void reset() {}
@ -74,6 +85,15 @@ public:
m_node = NULL;
}
TrackObjectPresentationSceneNode(
const core::vector3df& xyz,
const core::vector3df& hpr,
const core::vector3df& scale) :
TrackObjectPresentation(xyz, hpr, scale)
{
m_node = NULL;
}
virtual const core::vector3df& getPosition() const OVERRIDE;
virtual const core::vector3df& getRotation() const OVERRIDE;
virtual const core::vector3df& getScale() const OVERRIDE;
@ -132,8 +152,15 @@ private:
/** End frame of the animation to be played. */
unsigned int m_frame_end;
void init(const XMLNode* xml_node, bool enabled);
public:
TrackObjectPresentationMesh(const XMLNode& xml_node, bool enabled);
TrackObjectPresentationMesh(
const std::string& model_file, const core::vector3df& xyz,
const core::vector3df& hpr, const core::vector3df& scale);
virtual ~TrackObjectPresentationMesh();
virtual void reset() OVERRIDE;