RigsofRods
Soft-body Physics Simulation
SlideNode.h
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  Copyright 2018 Petr Ohlidal
7 
8  For more information, see http://www.rigsofrods.org/
9 
10  Rigs of Rods is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License version 3, as
12  published by the Free Software Foundation.
13 
14  Rigs of Rods is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
21 */
22 
26 
27 #pragma once
28 
29 #include "ForwardDeclarations.h"
30 
31 #include <OgreVector3.h>
32 #include <vector>
33 
34 namespace RoR {
35 
38 
41 {
42  RailSegment(beam_t* beam): rs_prev(nullptr), rs_next(nullptr), rs_beam(beam) {}
43 
45  RailSegment* CheckCurSlideSegment(Ogre::Vector3 const& point );
46 
50 };
51 
53 struct RailGroup
54 {
55  RailGroup(): rg_id(-1) {}
56 
58  RailSegment* FindClosestSegment(Ogre::Vector3 const& point );
59 
60  std::vector<RailSegment> rg_segments;
61  int rg_id;
62 };
63 
64 class SlideNode
65 {
66 public:
69  SlideNode(node_t* sliding_node, RailGroup* rail);
70 
72  int GetSlideNodeId();
73 
76  void UpdateForces(float dt);
77 
79  void UpdatePosition();
80 
83  {
85  this->ResetPositions();
86  }
87 
89  void ResetPositions();
90 
93  {
95  sn_slide_broken = false;
96  this->ResetPositions();
97  }
98 
99  void SetCorThreshold(float threshold ) { m_initial_threshold = m_cur_threshold = fabs( threshold ); }
100  void SetSpringRate(float rate ) { m_spring_rate = fabs( rate); }
101  void SetBreakForce(float breakRate ) { m_break_force = fabs( breakRate); }
102  void SetAttachmentRate(float rate ) { m_attach_rate = fabs( rate); }
103  void SetAttachmentDistance(float dist ) { m_attach_distance = fabs( dist); }
104  float GetAttachmentDistance() const { return m_attach_distance; }
105 
106  bool sn_attach_self:1;
109 
110 private:
112  Ogre::Vector3 CalcCorrectiveForces();
113 
120  Ogre::Vector3 m_ideal_position;
127 
128 // Methods //////////////////////////// TO BE SORTED /////////////////////////////////////////
129 public:
130 
134  const Ogre::Vector3& GetSlideNodePosition() const;
135 
140  void AttachToRail(RailGroup* toAttach)
141  {
142  m_cur_railgroup = toAttach;
143  this->ResetPositions();
145  }
146 
152  static Ogre::Real getLenTo( const RailGroup* group, const Ogre::Vector3& point );
153 
159  static Ogre::Real getLenTo( const RailSegment* rail, const Ogre::Vector3& point );
160 
166  static Ogre::Real getLenTo( const beam_t* beam, const Ogre::Vector3& point );
167 
168 
173  Ogre::Real getLenTo( const RailGroup* group ) const;
174 
179  Ogre::Real getLenTo( const RailSegment* rail ) const;
180 
186  Ogre::Real getLenTo( const beam_t* beam) const;
187 };
188 
190 
191 } // namespace RoR
192 
ForwardDeclarations.h
Global forward declarations.
RoR::SlideNode::m_ideal_position
Ogre::Vector3 m_ideal_position
Where the node SHOULD be. (World, m)
Definition: SlideNode.h:120
RoR::SlideNode::AttachToRail
void AttachToRail(RailGroup *toAttach)
Definition: SlideNode.h:140
RoR::SlideNode::m_initial_threshold
float m_initial_threshold
Distance from beam calculating corrective forces (m)
Definition: SlideNode.h:121
RoR::SlideNode::ResetSlideNode
void ResetSlideNode()
Move back to initial rail, reset 'broken' flag and recalculate closest position.
Definition: SlideNode.h:92
RoR::SlideNode::sn_attach_foreign
bool sn_attach_foreign
Attach/detach to rails only on other vehicles.
Definition: SlideNode.h:107
RoR::RailGroup
A series of RailSegment-s for SlideNode to slide along. Can be closed in a loop.
Definition: SlideNode.h:53
RoR::SlideNode::m_spring_rate
float m_spring_rate
Spring rate holding node to rail (N/m)
Definition: SlideNode.h:123
RoR::RailSegment::CheckCurSlideSegment
RailSegment * CheckCurSlideSegment(Ogre::Vector3 const &point)
Check if the slidenode should skip to a neighbour rail segment.
Definition: SlideNode.cpp:132
RoR::SlideNode::sn_attach_self
bool sn_attach_self
Attach/detach to rails on the current vehicle only.
Definition: SlideNode.h:106
RoR::SlideNode::m_node_forces_ratio
float m_node_forces_ratio
Ratio of length along the slide beam where the virtual node is "0.0f = p1, 1.0f = p2".
Definition: SlideNode.h:119
RoR::RailSegment::rs_beam
beam_t * rs_beam
Definition: SlideNode.h:49
RoR::SlideNode::m_cur_railgroup
RailGroup * m_cur_railgroup
Current Rail group, used for attachments.
Definition: SlideNode.h:117
RoR::SlideNode::m_cur_threshold
float m_cur_threshold
Distance away from beam before corrective forces begin to act on the node (m)
Definition: SlideNode.h:122
RoR::RailSegment
A single beam in a chain.
Definition: SlideNode.h:40
RoR::beam_t
Simulation: An edge in the softbody structure.
Definition: SimData.h:330
RoR::SlideNode::UpdatePosition
void UpdatePosition()
Checks for current rail segment and updates ideal position of the node.
Definition: SlideNode.cpp:164
RoR::SlideNode::m_initial_railgroup
RailGroup * m_initial_railgroup
Initial Rail group on spawn.
Definition: SlideNode.h:116
RoR::RailSegment::rs_next
RailSegment * rs_next
Definition: SlideNode.h:48
RoR::SlideNode::GetSlideNodeId
int GetSlideNodeId()
Returns the node index of the slide node.
Definition: SlideNode.cpp:159
RoR::SlideNode::m_attach_rate
float m_attach_rate
How fast the cur threshold changes when attaching (i.e. how long it will take for springs to fully at...
Definition: SlideNode.h:125
RoR::RailGroup::rg_segments
std::vector< RailSegment > rg_segments
Definition: SlideNode.h:60
RoR::SlideNode::getLenTo
static Ogre::Real getLenTo(const RailGroup *group, const Ogre::Vector3 &point)
Definition: SlideNode.cpp:210
RoR::SlideNode::m_cur_rail_seg
RailSegment * m_cur_rail_seg
Current rail segment we are sliding on.
Definition: SlideNode.h:118
RoR::node_t
Physics: A vertex in the softbody structure.
Definition: SimData.h:286
RoR::SlideNode::ResetPositions
void ResetPositions()
Recalculates the closest position on current RailGroup.
Definition: SlideNode.cpp:200
RoR::SlideNode::sn_slide_broken
bool sn_slide_broken
The slidenode was pulled away from the rail.
Definition: SlideNode.h:108
RoR::RailSegment::rs_prev
RailSegment * rs_prev
Definition: SlideNode.h:47
RoR::SlideNode::SlideNode
SlideNode(node_t *sliding_node, RailGroup *rail)
Definition: SlideNode.cpp:63
RoR::RailGroup::RailGroup
RailGroup()
Definition: SlideNode.h:55
RoR::SlideNode::GetSlideNodePosition
const Ogre::Vector3 & GetSlideNodePosition() const
Definition: SlideNode.cpp:195
RoR::SlideNode::CalcCorrectiveForces
Ogre::Vector3 CalcCorrectiveForces()
Calculate forces between the ideal and actual position of the sliding node.
Definition: SlideNode.cpp:249
RoR::SlideNode::UpdateForces
void UpdateForces(float dt)
Updates the corrective forces and applies these forces to the beam.
Definition: SlideNode.cpp:87
RoR::SlideNode::m_sliding_node
node_t * m_sliding_node
Pointer to node that is sliding.
Definition: SlideNode.h:114
RoR::RailGroup::rg_id
int rg_id
Spawn context - matching separately defined rails with slidenodes.
Definition: SlideNode.h:61
RoR::SlideNode::m_attach_distance
float m_attach_distance
Maximum distance slide node will attach to a beam (m)
Definition: SlideNode.h:126
RoR::SlideNode::SetDefaultRail
void SetDefaultRail(RailGroup *rail)
Sets rail to initially use when spawned or reset.
Definition: SlideNode.h:82
RoR::SlideNode::m_sliding_beam
beam_t * m_sliding_beam
Pointer to current beam sliding on.
Definition: SlideNode.h:115
RoR::RailGroup::FindClosestSegment
RailSegment * FindClosestSegment(Ogre::Vector3 const &point)
Search for closest rail segment (the one with closest node in it) in the entire RailGroup.
Definition: SlideNode.cpp:114
RoR::SlideNode
Definition: SlideNode.h:64
RoR::SlideNode::m_break_force
float m_break_force
Force at which Slide Node breaks away from the rail (N)
Definition: SlideNode.h:124
RoR
Definition: AppContext.h:36
RoR::RailSegment::RailSegment
RailSegment(beam_t *beam)
Definition: SlideNode.h:42