2009-12-29 19:08:20 -05:00
|
|
|
//
|
|
|
|
// SuperTuxKart - a fun racing game with go-kart
|
2015-03-29 20:31:42 -04:00
|
|
|
// Copyright (C) 2009-2015 Joerg Henrichs
|
2009-12-29 19:08:20 -05:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License
|
|
|
|
// as published by the Free Software Foundation; either version 3
|
|
|
|
// of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
#include "tracks/track_object_manager.hpp"
|
|
|
|
|
2012-04-18 09:14:18 -04:00
|
|
|
#include "animations/ipo.hpp"
|
2009-12-29 19:08:20 -05:00
|
|
|
#include "animations/three_d_animation.hpp"
|
2011-05-17 21:00:26 -04:00
|
|
|
#include "graphics/lod_node.hpp"
|
2011-06-08 21:29:25 -04:00
|
|
|
#include "graphics/material_manager.hpp"
|
2009-12-29 19:08:20 -05:00
|
|
|
#include "io/xml_node.hpp"
|
|
|
|
#include "physics/physical_object.hpp"
|
|
|
|
#include "tracks/track_object.hpp"
|
2014-04-11 14:14:20 -04:00
|
|
|
#include "utils/log.hpp"
|
2009-12-29 19:08:20 -05:00
|
|
|
|
2011-06-08 21:29:25 -04:00
|
|
|
#include <IMeshSceneNode.h>
|
2012-07-25 20:33:25 -04:00
|
|
|
#include <ISceneManager.h>
|
2011-06-08 21:29:25 -04:00
|
|
|
|
2009-12-29 19:08:20 -05:00
|
|
|
TrackObjectManager::TrackObjectManager()
|
|
|
|
{
|
|
|
|
} // TrackObjectManager
|
|
|
|
|
2010-01-06 20:14:46 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
TrackObjectManager::~TrackObjectManager()
|
|
|
|
{
|
|
|
|
} // ~TrackObjectManager
|
|
|
|
|
2009-12-29 19:08:20 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Adds an object to the track object manager. The type to add is specified
|
|
|
|
* in the xml_node.
|
|
|
|
*/
|
2014-09-11 07:48:03 -04:00
|
|
|
void TrackObjectManager::add(const XMLNode &xml_node, scene::ISceneNode* parent,
|
2015-06-03 20:34:58 -04:00
|
|
|
ModelDefinitionLoader& model_def_loader,
|
2016-08-16 23:57:18 -04:00
|
|
|
TrackObject* parent_library)
|
2009-12-29 19:08:20 -05:00
|
|
|
{
|
2012-02-13 18:40:27 -05:00
|
|
|
try
|
2009-12-29 19:08:20 -05:00
|
|
|
{
|
2016-08-16 23:57:18 -04:00
|
|
|
TrackObject *obj = new TrackObject(xml_node, parent, model_def_loader, parent_library);
|
2014-09-11 07:48:03 -04:00
|
|
|
m_all_objects.push_back(obj);
|
|
|
|
if(obj->isDriveable())
|
|
|
|
m_driveable_objects.push_back(obj);
|
2010-12-05 15:55:09 -05:00
|
|
|
}
|
2012-02-13 18:40:27 -05:00
|
|
|
catch (std::exception& e)
|
2009-12-29 19:08:20 -05:00
|
|
|
{
|
2014-01-01 16:18:06 -05:00
|
|
|
Log::warn("TrackObjectManager", "Could not load track object. Reason : %s",
|
|
|
|
e.what());
|
2009-12-29 19:08:20 -05:00
|
|
|
}
|
|
|
|
} // add
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Initialises all track objects.
|
|
|
|
*/
|
|
|
|
void TrackObjectManager::init()
|
|
|
|
{
|
2015-08-09 21:38:01 -04:00
|
|
|
for_var_in(TrackObject*, curr, m_all_objects)
|
2009-12-29 19:08:20 -05:00
|
|
|
{
|
2015-08-09 21:38:01 -04:00
|
|
|
curr->onWorldReady();
|
2009-12-29 19:08:20 -05:00
|
|
|
}
|
|
|
|
} // reset
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Initialises all track objects.
|
|
|
|
*/
|
|
|
|
void TrackObjectManager::reset()
|
|
|
|
{
|
2014-11-15 22:40:19 -05:00
|
|
|
for (TrackObject* curr : m_all_objects)
|
2009-12-29 19:08:20 -05:00
|
|
|
{
|
2011-05-23 20:04:43 -04:00
|
|
|
curr->reset();
|
2015-06-17 19:33:53 -04:00
|
|
|
curr->resetEnabled();
|
2009-12-29 19:08:20 -05:00
|
|
|
}
|
|
|
|
} // reset
|
2014-08-05 10:58:40 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** returns a reference to the track object
|
|
|
|
* with a particular ID
|
|
|
|
* \param name Name or ID of track object
|
|
|
|
*/
|
2015-06-05 19:34:46 -04:00
|
|
|
TrackObject* TrackObjectManager::getTrackObject(const std::string& libraryInstance,
|
|
|
|
const std::string& name)
|
2014-06-11 01:22:04 -04:00
|
|
|
{
|
2014-11-15 22:40:19 -05:00
|
|
|
for (TrackObject* curr : m_all_objects)
|
2014-06-18 03:42:51 -04:00
|
|
|
{
|
2015-06-05 19:34:46 -04:00
|
|
|
//if (curr->getParentLibrary() != NULL)
|
|
|
|
// Log::info("TrackObjectManager", "Found %s::%s", curr->getParentLibrary()->getID().c_str(), curr->getID().c_str());
|
|
|
|
//else
|
|
|
|
// Log::info("TrackObjectManager", "Found ::%s", curr->getID().c_str());
|
|
|
|
|
|
|
|
if (curr->getParentLibrary() == NULL)
|
|
|
|
{
|
|
|
|
if (libraryInstance.size() > 0)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
2014-06-11 01:22:04 -04:00
|
|
|
{
|
2015-06-05 19:34:46 -04:00
|
|
|
if (libraryInstance != curr->getParentLibrary()->getID())
|
|
|
|
continue;
|
|
|
|
}
|
2014-06-11 01:22:04 -04:00
|
|
|
|
2015-06-05 19:34:46 -04:00
|
|
|
if (curr->getID() == name)
|
|
|
|
{
|
2014-06-11 01:22:04 -04:00
|
|
|
return curr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//object not found
|
2015-06-05 19:34:46 -04:00
|
|
|
Log::warn("TrackObjectManager", "Object not found : %s::%s", libraryInstance.c_str(), name.c_str());
|
2014-06-11 01:22:04 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2009-12-29 19:08:20 -05:00
|
|
|
/** Handles an explosion, i.e. it makes sure that all physical objects are
|
|
|
|
* affected accordingly.
|
|
|
|
* \param pos Position of the explosion.
|
|
|
|
* \param obj If the hit was a physical object, this object will be affected
|
|
|
|
* more. Otherwise this is NULL.
|
2012-11-04 07:57:03 -05:00
|
|
|
* \param secondary_hits True if items that are not directly hit should
|
|
|
|
* also be affected.
|
2009-12-29 19:08:20 -05:00
|
|
|
*/
|
|
|
|
|
2012-11-04 07:57:03 -05:00
|
|
|
void TrackObjectManager::handleExplosion(const Vec3 &pos, const PhysicalObject *mp,
|
|
|
|
bool secondary_hits)
|
2009-12-29 19:08:20 -05:00
|
|
|
{
|
2011-05-23 20:04:43 -04:00
|
|
|
TrackObject* curr;
|
|
|
|
for_in (curr, m_all_objects)
|
2009-12-29 19:08:20 -05:00
|
|
|
{
|
2014-01-24 06:25:55 -05:00
|
|
|
if(secondary_hits || mp == curr->getPhysicalObject())
|
|
|
|
curr->handleExplosion(pos, mp == curr->getPhysicalObject());
|
2009-12-29 19:08:20 -05:00
|
|
|
}
|
|
|
|
} // handleExplosion
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2011-07-25 22:24:49 -04:00
|
|
|
/** Updates all track objects.
|
|
|
|
* \param dt Time step size.
|
|
|
|
*/
|
2009-12-29 19:08:20 -05:00
|
|
|
void TrackObjectManager::update(float dt)
|
|
|
|
{
|
2011-09-18 13:50:20 -04:00
|
|
|
TrackObject* curr;
|
|
|
|
for_in (curr, m_all_objects)
|
2009-12-29 19:08:20 -05:00
|
|
|
{
|
2011-09-18 13:50:20 -04:00
|
|
|
curr->update(dt);
|
2009-12-29 19:08:20 -05:00
|
|
|
}
|
|
|
|
} // update
|
|
|
|
|
2014-09-11 07:48:03 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
2014-09-11 18:37:03 -04:00
|
|
|
/** Does a raycast against all driveable objects. This way part of the track
|
|
|
|
* can be a physical object, and can e.g. be animated. A separate list of all
|
|
|
|
* driveable objects is maintained (in one case there were over 2000 bodies,
|
|
|
|
* but only one is driveable). The result of the raycast against the track
|
|
|
|
* mesh are the input parameter. It is then tested if the raycast against
|
|
|
|
* a track object gives a 'closer' result. If so, the parameters hit_point,
|
|
|
|
* normal, and material will be updated.
|
|
|
|
* \param from/to The from and to position for the raycast.
|
|
|
|
* \param xyz The position in world where the ray hit.
|
|
|
|
* \param material The material of the mesh that was hit.
|
|
|
|
* \param normal The intrapolated normal at that position.
|
|
|
|
* \param interpolate_normal If true, the returned normal is the interpolated
|
|
|
|
* based on the three normals of the triangle and the location of the
|
|
|
|
* hit point (which is more compute intensive, but results in much
|
|
|
|
* smoother results).
|
|
|
|
* \return True if a triangle was hit, false otherwise (and no output
|
|
|
|
* variable will be set.
|
|
|
|
|
|
|
|
*/
|
2014-09-11 07:48:03 -04:00
|
|
|
void TrackObjectManager::castRay(const btVector3 &from,
|
|
|
|
const btVector3 &to, btVector3 *hit_point,
|
|
|
|
const Material **material,
|
|
|
|
btVector3 *normal,
|
|
|
|
bool interpolate_normal) const
|
|
|
|
{
|
|
|
|
float distance = 9999.9f;
|
|
|
|
// If there was a hit already, compute the current distance
|
|
|
|
if(*material)
|
|
|
|
{
|
|
|
|
distance = hit_point->distance(from);
|
|
|
|
}
|
2014-11-16 17:24:50 -05:00
|
|
|
for (const TrackObject* curr : m_driveable_objects)
|
2014-09-11 07:48:03 -04:00
|
|
|
{
|
2017-11-04 13:32:41 -04:00
|
|
|
if (!curr->isEnabled())
|
|
|
|
{
|
|
|
|
// For example jumping pad in cocoa temple
|
|
|
|
continue;
|
|
|
|
}
|
2014-09-11 07:48:03 -04:00
|
|
|
btVector3 new_hit_point;
|
|
|
|
const Material *new_material;
|
|
|
|
btVector3 new_normal;
|
|
|
|
if(curr->castRay(from, to, &new_hit_point, &new_material, &new_normal,
|
|
|
|
interpolate_normal))
|
|
|
|
{
|
|
|
|
float new_distance = new_hit_point.distance(from);
|
2014-09-11 18:37:03 -04:00
|
|
|
// If the new hit is closer than the current hit, save
|
|
|
|
// the data.
|
2014-09-11 07:48:03 -04:00
|
|
|
if (new_distance < distance)
|
|
|
|
{
|
|
|
|
*material = new_material;
|
|
|
|
*hit_point = new_hit_point;
|
|
|
|
*normal = new_normal;
|
|
|
|
distance = new_distance;
|
|
|
|
} // if new_distance < distance
|
|
|
|
} // if hit
|
|
|
|
} // for all track objects.
|
|
|
|
} // castRay
|
|
|
|
|
2011-07-25 22:24:49 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
2013-04-04 21:30:34 -04:00
|
|
|
void TrackObjectManager::insertObject(TrackObject* object)
|
2011-09-01 16:09:26 -04:00
|
|
|
{
|
|
|
|
m_all_objects.push_back(object);
|
|
|
|
}
|
|
|
|
|
2011-09-01 19:47:15 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
2011-09-15 08:46:03 -04:00
|
|
|
/** Removes the object from the scene graph, bullet, and the list of
|
|
|
|
* track objects, and then frees the object.
|
|
|
|
* \param obj The physical object to remove.
|
|
|
|
*/
|
2013-04-03 21:15:37 -04:00
|
|
|
void TrackObjectManager::removeObject(TrackObject* obj)
|
2011-09-01 19:47:15 -04:00
|
|
|
{
|
2011-09-15 08:46:03 -04:00
|
|
|
m_all_objects.remove(obj);
|
|
|
|
delete obj;
|
|
|
|
} // removeObject
|