RigsofRods
Soft-body Physics Simulation
ActorSlideNode.cpp
Go to the documentation of this file.
1 /*
2  This source file is part of Rigs of Rods
3  Copyright 2005-2012 Pierre-Michel Ricordel
4  Copyright 2007-2012 Thomas Fischer
5  Copyright 2009 Christopher Ritchey
6 
7  For more information, see http://www.rigsofrods.org/
8 
9  Rigs of Rods is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License version 3, as
11  published by the Free Software Foundation.
12 
13  Rigs of Rods is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
25 
26 #include "SlideNode.h"
27 
28 #include "Actor.h"
29 #include "GameContext.h"
30 
31 using namespace RoR;
32 
33 // ug... BAD PERFORMNCE, BAD!!
35 {
36  // for every slide node on this truck
37  for (std::vector<SlideNode>::iterator itNode = m_slidenodes.begin(); itNode != m_slidenodes.end(); itNode++)
38  {
39  std::pair<RailGroup*, Ogre::Real> closest((RailGroup*)NULL, std::numeric_limits<Ogre::Real>::infinity());
40  std::pair<RailGroup*, Ogre::Real> current((RailGroup*)NULL, std::numeric_limits<Ogre::Real>::infinity());
41 
42  // if neither foreign, nor self attach is set then we cannot change the
43  // Rail attachments
44  if (!itNode->sn_attach_self && !itNode->sn_attach_foreign)
45  {
46  continue;
47  }
48 
50  {
51  itNode->AttachToRail(NULL);
52  continue;
53  }
54 
55  // check all the slide rail on all the other trucks :(
56  for (ActorPtr& actor : App::GetGameContext()->GetActorManager()->GetActors())
57  {
58  // make sure this truck is allowed
59  if ((this != actor.GetRef() && !itNode->sn_attach_foreign) || (this == actor.GetRef() && !itNode->sn_attach_self))
60  continue;
61 
62  current = GetClosestRailOnActor(actor, (*itNode));
63  if (current.second < closest.second)
64  closest = current;
65  } // this many
66 
67  itNode->AttachToRail(closest.first);
68  } // nests
69 
71 } // is ugly....
72 
73 std::pair<RailGroup*, Ogre::Real> Actor::GetClosestRailOnActor(ActorPtr actor, const SlideNode& node)
74 {
75  std::pair<RailGroup*, Ogre::Real> closest((RailGroup*)NULL, std::numeric_limits<Ogre::Real>::infinity());
76 
77  RailSegment* curRail = NULL;
78  Ogre::Real lenToCurRail = std::numeric_limits<Ogre::Real>::infinity();
79 
80  for (std::vector<RailGroup*>::iterator itGroup = actor->m_railgroups.begin();
81  itGroup != actor->m_railgroups.end();
82  itGroup++)
83  {
84  // find the rail closest to the Node
85  if (*itGroup == nullptr)
86  continue;
87 
88  curRail = (*itGroup)->FindClosestSegment(node.GetSlideNodePosition());
89  lenToCurRail = node.getLenTo(curRail);
90 
91  if (lenToCurRail < node.GetAttachmentDistance() && lenToCurRail < closest.second)
92  {
93  closest.first = (*itGroup);
94  closest.second = lenToCurRail;
95  }
96  }
97 
98  return closest;
99 }
100 
101 // SlideNode Utility functions /////////////////////////////////////////////////
102 
103 void Actor::updateSlideNodeForces(const Ogre::Real dt)
104 {
105  for (std::vector<SlideNode>::iterator it = m_slidenodes.begin(); it != m_slidenodes.end(); ++it)
106  {
107  it->UpdatePosition();
108  it->UpdateForces(dt);
109  }
110 }
111 
113 {
114  if (m_slidenodes.empty())
115  return;
116  for (std::vector<SlideNode>::iterator it = m_slidenodes.begin(); it != m_slidenodes.end(); ++it)
117  {
118  it->ResetPositions();
119  }
120 }
121 
123 {
124  for (std::vector<SlideNode>::iterator it = m_slidenodes.begin(); it != m_slidenodes.end(); ++it)
125  {
126  it->ResetSlideNode();
127  }
128 }
129 
131 {
132  for (std::vector<SlideNode>::iterator it = m_slidenodes.begin(); it != m_slidenodes.end(); ++it)
133  {
134  it->UpdatePosition();
135  }
136 }
GameContext.h
Game state manager and message-queue provider.
RoR::Actor::m_slidenodes
std::vector< SlideNode > m_slidenodes
all the SlideNodes available on this actor
Definition: Actor.h:528
RoR::Actor::GetClosestRailOnActor
std::pair< RailGroup *, Ogre::Real > GetClosestRailOnActor(ActorPtr actor, const SlideNode &node)
Definition: ActorSlideNode.cpp:73
RoR::RailGroup
A series of RailSegment-s for SlideNode to slide along. Can be closed in a loop.
Definition: SlideNode.h:53
RefCountingObjectPtr< Actor >
Actor.h
RoR::RailSegment
A single beam in a chain.
Definition: SlideNode.h:40
SlideNode.h
RoR::Actor::m_slidenodes_locked
bool m_slidenodes_locked
Physics state; Are SlideNodes locked?
Definition: Actor.h:611
RoR::Actor::resetSlideNodePositions
void resetSlideNodePositions()
Recalculate SlideNode positions.
Definition: ActorSlideNode.cpp:112
RoR::SlideNode::getLenTo
static Ogre::Real getLenTo(const RailGroup *group, const Ogre::Vector3 &point)
Definition: SlideNode.cpp:210
RoR::Actor::updateSlideNodePositions
void updateSlideNodePositions()
incrementally update the position of all SlideNodes
Definition: ActorSlideNode.cpp:130
RoR::App::GetGameContext
GameContext * GetGameContext()
Definition: Application.cpp:279
RoR::Actor::m_railgroups
std::vector< RailGroup * > m_railgroups
all the available RailGroups for this actor
Definition: Actor.h:529
RoR::Actor::toggleSlideNodeLock
void toggleSlideNodeLock()
Definition: ActorSlideNode.cpp:34
RoR::SlideNode::GetSlideNodePosition
const Ogre::Vector3 & GetSlideNodePosition() const
Definition: SlideNode.cpp:195
RoR::Actor::resetSlideNodes
void resetSlideNodes()
Reset all the SlideNodes.
Definition: ActorSlideNode.cpp:122
RoR::Actor::updateSlideNodeForces
void updateSlideNodeForces(const Ogre::Real delta_time_sec)
calculate and apply Corrective forces
Definition: ActorSlideNode.cpp:103
RoR::SlideNode
Definition: SlideNode.h:64
RoR
Definition: AppContext.h:36