Some cleanup to check structures, start adding new cylinder check structure (WIP)
This commit is contained in:
parent
77fac34e41
commit
84e78e0d40
67
src/tracks/check_cylinder.cpp
Normal file
67
src/tracks/check_cylinder.cpp
Normal file
@ -0,0 +1,67 @@
|
||||
//
|
||||
// SuperTuxKart - a fun racing game with go-kart
|
||||
// Copyright (C) 2009-2015 Joerg Henrichs
|
||||
//
|
||||
// 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/check_cylinder.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "io/xml_node.hpp"
|
||||
#include "modes/world.hpp"
|
||||
#include "race/race_manager.hpp"
|
||||
|
||||
CheckCylinder::CheckCylinder(const XMLNode &node, unsigned int index)
|
||||
: CheckStructure(node, index)
|
||||
{
|
||||
m_radius2 = 1;
|
||||
m_height = 0;
|
||||
node.get("height", &m_height);
|
||||
node.get("radius", &m_radius2);
|
||||
m_radius2 *= m_radius2;
|
||||
node.get("xyz", &m_center_point);
|
||||
unsigned int num_karts = race_manager->getNumberOfKarts();
|
||||
m_is_inside.resize(num_karts);
|
||||
m_distance2.resize(num_karts);
|
||||
for (unsigned int i=0; i<num_karts; i++)
|
||||
{
|
||||
m_is_inside[i] = false;
|
||||
}
|
||||
} // CheckCylinder
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
/** True if going from old_pos to new_pos enters or leaves this sphere. This
|
||||
* function is called from update (of the checkline structure). It also
|
||||
* updates the flag about which karts are inside
|
||||
* \param old_pos Position in previous frame.
|
||||
* \param new_pos Position in current frame.
|
||||
* \param kart_id Index of the kart, can be used to store kart specific
|
||||
* additional data.
|
||||
*/
|
||||
bool CheckCylinder::isTriggered(const Vec3 &old_pos, const Vec3 &new_pos,
|
||||
unsigned int kart_id)
|
||||
{
|
||||
// TODO: this is the code for a sphere, rewrite for cylinder
|
||||
float old_dist2 = (old_pos-m_center_point).length2();
|
||||
float new_dist2 = (new_pos-m_center_point).length2();
|
||||
m_is_inside[kart_id] = new_dist2<m_radius2;
|
||||
m_distance2[kart_id] = new_dist2;
|
||||
// Trigger if the kart goes from outside (or border) to inside,
|
||||
// or inside ro outside (or border).
|
||||
return (old_dist2>=m_radius2 && new_dist2 < m_radius2) ||
|
||||
(old_dist2< m_radius2 && new_dist2 >=m_radius2);
|
||||
} // isTriggered
|
66
src/tracks/check_cylinder.hpp
Normal file
66
src/tracks/check_cylinder.hpp
Normal file
@ -0,0 +1,66 @@
|
||||
//
|
||||
// SuperTuxKart - a fun racing game with go-kart
|
||||
// Copyright (C) 2009-2015 Joerg Henrichs
|
||||
//
|
||||
// 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.
|
||||
|
||||
#ifndef HEADER_CHECK_CYLINDER_HPP
|
||||
#define HEADER_CHECK_CYLINDER_HPP
|
||||
|
||||
#include "tracks/check_structure.hpp"
|
||||
|
||||
class XMLNode;
|
||||
class CheckManager;
|
||||
|
||||
/** This class implements a check sphere that is used to change the ambient
|
||||
* light if a kart is inside this sphere. Besides a normal radius this
|
||||
* sphere also has a 2nd 'inner' radius: player karts inside the inner
|
||||
* radius will have the full new ambient light, karts outside the default
|
||||
* light, and karts in between will mix the light dependent on distance.
|
||||
*
|
||||
* \ingroup tracks
|
||||
*/
|
||||
class CheckCylinder : public CheckStructure
|
||||
{
|
||||
private:
|
||||
/** Center of the sphere. */
|
||||
Vec3 m_center_point;
|
||||
/** Squared radius of the cylinder. */
|
||||
float m_radius2;
|
||||
float m_height;
|
||||
/** A flag for each kart to indicate if it's inside of the sphere. */
|
||||
std::vector<bool> m_is_inside;
|
||||
/** Stores the distance of each kart from the center of this sphere.
|
||||
* This saves some computations. */
|
||||
std::vector<float> m_distance2;
|
||||
public:
|
||||
CheckCylinder(const XMLNode &node, unsigned int index);
|
||||
virtual ~CheckCylinder() {};
|
||||
virtual bool isTriggered(const Vec3 &old_pos, const Vec3 &new_pos,
|
||||
unsigned int kart_id);
|
||||
// ------------------------------------------------------------------------
|
||||
/** Returns if kart indx is currently inside of the sphere. */
|
||||
bool isInside(int index) const { return m_is_inside[index]; }
|
||||
// -------------------------------------------------------------------------
|
||||
/** Returns the squared distance of kart index from the enter of
|
||||
* this sphere. */
|
||||
float getDistance2ForKart(int index) const { return m_distance2[index];}
|
||||
// -------------------------------------------------------------------------
|
||||
/** Returns the square of the radius of this sphere. */
|
||||
float getRadius2() const { return m_radius2; }
|
||||
}; // CheckCylinder
|
||||
|
||||
#endif
|
||||
|
@ -55,27 +55,36 @@ void CheckLap::reset(const Track &track)
|
||||
* is called from update (of the checkline structure).
|
||||
* \param old_pos Position in previous frame.
|
||||
* \param new_pos Position in current frame.
|
||||
* \param indx Index of the kart, can be used to store kart specific
|
||||
* \param kart_index Index of the kart, can be used to store kart specific
|
||||
* additional data.
|
||||
*/
|
||||
bool CheckLap::isTriggered(const Vec3 &old_pos, const Vec3 &new_pos,
|
||||
unsigned int indx)
|
||||
unsigned int kart_index)
|
||||
{
|
||||
float track_length = World::getWorld()->getTrack()->getTrackLength();
|
||||
LinearWorld *lin_world = dynamic_cast<LinearWorld*>(World::getWorld());
|
||||
World* w = World::getWorld();
|
||||
LinearWorld* lin_world = dynamic_cast<LinearWorld*>(w);
|
||||
if (lin_world != NULL)
|
||||
{
|
||||
lin_world->getTrackSector(kart_index).setLastTriggeredCheckline(m_index);
|
||||
}
|
||||
|
||||
float track_length = w->getTrack()->getTrackLength();
|
||||
// Can happen if a non-lap based race mode is used with a scene file that
|
||||
// has check defined.
|
||||
if(!lin_world)
|
||||
return false;
|
||||
float current_distance = lin_world->getDistanceDownTrackForKart(indx);
|
||||
bool result =(m_previous_distance[indx]>0.95f*track_length &&
|
||||
float current_distance = lin_world->getDistanceDownTrackForKart(kart_index);
|
||||
bool result = (m_previous_distance[kart_index]>0.95f*track_length &&
|
||||
current_distance<7.0f);
|
||||
if(UserConfigParams::m_check_debug && result)
|
||||
Log::info("CheckLap", "Kart %s crossed start line from %f to %f.",
|
||||
World::getWorld()->getKart(indx)->getIdent().c_str(),
|
||||
m_previous_distance[indx], current_distance);
|
||||
|
||||
m_previous_distance[indx] = current_distance;
|
||||
if (UserConfigParams::m_check_debug && result)
|
||||
{
|
||||
Log::info("CheckLap", "Kart %s crossed start line from %f to %f.",
|
||||
World::getWorld()->getKart(kart_index)->getIdent().c_str(),
|
||||
m_previous_distance[kart_index], current_distance);
|
||||
}
|
||||
|
||||
m_previous_distance[kart_index] = current_distance;
|
||||
|
||||
return result;
|
||||
} // isTriggered
|
||||
|
@ -22,6 +22,7 @@
|
||||
#include "graphics/irr_driver.hpp"
|
||||
#include "io/xml_node.hpp"
|
||||
#include "karts/abstract_kart.hpp"
|
||||
#include "modes/linear_world.hpp"
|
||||
#include "modes/world.hpp"
|
||||
#include "race/race_manager.hpp"
|
||||
|
||||
@ -151,14 +152,21 @@ void CheckLine::changeDebugColor(bool is_active)
|
||||
* additional data.
|
||||
*/
|
||||
bool CheckLine::isTriggered(const Vec3 &old_pos, const Vec3 &new_pos,
|
||||
unsigned int indx)
|
||||
unsigned int kart_index)
|
||||
{
|
||||
World* w = World::getWorld();
|
||||
LinearWorld* lw = dynamic_cast<LinearWorld*>(w);
|
||||
if (lw != NULL)
|
||||
{
|
||||
lw->getTrackSector(kart_index).setLastTriggeredCheckline(m_index);
|
||||
}
|
||||
|
||||
core::vector2df p=new_pos.toIrrVector2d();
|
||||
bool sign = m_line.getPointOrientation(p)>=0;
|
||||
bool result;
|
||||
// If the sign has changed, i.e. the infinite line was crossed somewhere,
|
||||
// check if the finite line was actually crossed:
|
||||
if(sign!=m_previous_sign[indx] &&
|
||||
if (sign != m_previous_sign[kart_index] &&
|
||||
m_line.intersectWith(core::line2df(old_pos.toIrrVector2d(),
|
||||
new_pos.toIrrVector2d()),
|
||||
m_cross_point) )
|
||||
@ -175,17 +183,17 @@ bool CheckLine::isTriggered(const Vec3 &old_pos, const Vec3 &new_pos,
|
||||
if(World::getWorld()->getNumKarts()>0)
|
||||
Log::info("CheckLine", "Kart %s crosses line, but wrong height "
|
||||
"(%f vs %f).",
|
||||
World::getWorld()->getKart(indx)->getIdent().c_str(),
|
||||
World::getWorld()->getKart(kart_index)->getIdent().c_str(),
|
||||
new_pos.getY(), m_min_height);
|
||||
else
|
||||
Log::info("CheckLine", "Kart %d crosses line, but wrong height "
|
||||
"(%f vs %f).",
|
||||
indx, new_pos.getY(), m_min_height);
|
||||
kart_index, new_pos.getY(), m_min_height);
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
result = false;
|
||||
m_previous_sign[indx] = sign;
|
||||
m_previous_sign[kart_index] = sign;
|
||||
return result;
|
||||
} // isTriggered
|
||||
|
@ -198,13 +198,6 @@ void CheckStructure::changeStatus(const std::vector<int> &indices,
|
||||
*/
|
||||
void CheckStructure::trigger(unsigned int kart_index)
|
||||
{
|
||||
World* w = World::getWorld();
|
||||
LinearWorld* lw = dynamic_cast<LinearWorld*>(w);
|
||||
if (lw != NULL)
|
||||
{
|
||||
lw->getTrackSector(kart_index).setLastTriggeredCheckline(m_index);
|
||||
}
|
||||
|
||||
switch(m_check_type)
|
||||
{
|
||||
case CT_NEW_LAP :
|
||||
|
@ -40,6 +40,7 @@
|
||||
#include "modes/world.hpp"
|
||||
#include "scriptengine/script_engine.hpp"
|
||||
#include "states_screens/dialogs/tutorial_message_dialog.hpp"
|
||||
#include "tracks/check_cylinder.hpp"
|
||||
#include "tracks/check_manager.hpp"
|
||||
#include "tracks/check_sphere.hpp"
|
||||
#include "tracks/model_definition_loader.hpp"
|
||||
@ -961,11 +962,12 @@ TrackObjectPresentationActionTrigger::TrackObjectPresentationActionTrigger(
|
||||
{
|
||||
// TODO: rewrite as a sphere check structure?
|
||||
ItemManager::get()->newItem(m_init_xyz, trigger_distance, this);
|
||||
// CheckManager::get()->add(new CheckSphere(xml_node, 0 /* TODO what is this? */));
|
||||
}
|
||||
else if (m_type == TRIGGER_TYPE_CYLINDER)
|
||||
{
|
||||
// TODO: create the right check structure
|
||||
CheckManager::get()->add(new CheckSphere(xml_node, 0 /* TODO what is this? */));
|
||||
CheckManager::get()->add(new CheckCylinder(xml_node, 0 /* TODO what is this? */));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user