Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
prediction.cpp
Go to the documentation of this file.
1 #include "client.h"
2 #include "prediction.h"
3 #include "gfx/lerp.h"
4 #include "cmd/unit_generic.h"
5 
6 /*
7  ************************************************************************************
8  **** Prediction virtual class ***
9  ************************************************************************************
10  */
11 
13  A0( 0, 0, 0 )
14  , B( 0, 0, 0 )
15  , A1( 0, 0, 0 )
16  , A2( 0, 0, 0 )
17  , A3( 0, 0, 0 )
18  , VA( 0, 0, 0 )
19  , VB( 0, 0, 0 )
20  , AA( 0, 0, 0 )
21  , AB( 0, 0, 0 )
22  , OA()
23  , OB()
24 {
25  this->deltatime = 0.0;
26 }
28 {}
29 
31  const ClientState &last_packet_state,
32  double elapsed_since_last_packet,
33  double deltatime )
34 {
35  //This function is to call after the state have been updated (which should be after receiving a SNAPSHOT)
36 
37  //This function computes 4 splines points needed for a spline creation
38  //- compute a point on the current spline using blend as t value
39  //- parameter A and B are old_position and new_position (received in the latest packet)
40 
41  /************* VA IS TO BE UNCOMMENTED ****************/
42  //Unit * un = clt->game_unit.GetUnit();
43 
44 //NETFIXME: Why cast to int and then back to double?
45 //double delay = (double)(unsigned int)deltatime;
46 
47  double delay = deltatime;
48  this->deltatime = deltatime;
49  //A is last known position and B is the position we just received
50  //A1 is computed from position A and velocity VA
51  A0 = un->old_state.getPosition();
53  OA = un->old_state.getOrientation();
57  if (elapsed_since_last_packet > 0.)
58  AB = (last_packet_state.getVelocity()*last_packet_state.getSpecMult()-VB)
59  /( (float) elapsed_since_last_packet );
60  //cerr<<"lastvel=("<<last_packet_state.getVelocity().i<<",,"<<last_packet_state.getVelocity().k<<")"<<endl;
61  else
62  AB = un->GetAcceleration();
63  AA = AB; //un->old_state.getAcceleration();//no longer supproted :-/ //NETFIXME now we don't have velocity
64 
65  //A1 = old_position + old_velocity * 1 sec
66  A1 = A0+VA;
67  //A3 is computed from position B and velocity VB
68  //A3 = new_position + new_velocity
69  A2 = B+VB*delay+AB*delay*delay*0.5;
70  //A2 is the predicted position
71  //A2 = new_position + new_velocity
72  A3 = A2+(VB+AB*delay);
74  //cerr<<" *** InitInterpolation "<<un->getFullname()<<","<<un->GetSerial()<<" ";
75  //cerr << un->old_state<<", B="<<B.i<<",,"<<B.k<<", VB="<<VB.i<<",,"<<VB.k<<", delay="<<delay<<", ACCEL="<<AB.i<<","<<AB.j<<","<<AB.k<<endl;
76 }
77 
78 Transformation Prediction::Interpolate( Unit *un, double deltatime ) const
79 {
80  return Transformation( InterpolateOrientation( un, deltatime ), InterpolatePosition( un, deltatime ) );
81 }
82 
83 /*
84  ************************************************************************************
85  **** NullPrediction class : doesn't do any prediction/interpolation ***
86  ************************************************************************************
87  */
88 
90  const ClientState &last_packet_state,
91  double elapsed_since_last_packet,
92  double deltatime )
93 {}
94 
95 QVector NullPrediction::InterpolatePosition( Unit *un, double deltatime ) const
96 {
97  return A2+VB*deltatime;
98 }
99 
101 {
102  return OB; //un->curr_physical_state.orientation;
103 }
104 
105 /*
106  ************************************************************************************
107  **** LinearPrediction class : based on lerp.c stuff also used in non networking ***
108  ************************************************************************************
109  */
110 
111 QVector LinearPrediction::InterpolatePosition( Unit *un, double deltatime ) const
112 {
113  return Interpolate( un, deltatime ).position;
114 }
115 
117 {
118  return Interpolate( un, deltatime ).orientation;
119 }
120 
121 Transformation LinearPrediction::Interpolate( Unit *un, double deltatime ) const
122 {
123  static bool no_interp = XMLSupport::parse_bool( vs_config->getVariable( "network", "no_interpolation", "false" ) );
124  if (no_interp)
125  return Transformation( OB, B );
126  if (deltatime > this->deltatime || this->deltatime == 0) {
127  double delay = deltatime-this->deltatime;
128  //cerr << "Using new Pos "<<un->GetSerial()<<": A2=("<<A2.i<<",,"<<A2.k<<"), VB=("<<VB.i<<",,"<<VB.k<<"), delay="<<delay<<endl;
129  return Transformation( OB, A2+VB*delay );
130  } else {
131  const Transformation old_pos( OA, A0 ); //un->curr_physical_state);
132  const Transformation new_pos( OB, A2 );
133  //cerr << "Using OLD Pos "<<un->GetSerial()<<": A2=("<<A2.i<<",,"<<A2.k<<"), delay="<<(deltatime/this->deltatime)<<", A0=("<<A0.i<<",,"<<A0.k<<")"<<endl;
134  return linear_interpolate_uncapped( old_pos, new_pos, deltatime/this->deltatime );
135  }
136 }
137 
138 /*
139  ************************************************************************************
140  **** CubicSplinePrediction class : based on cubic spline interpolation ***
141  ************************************************************************************
142  */
143 
145  const ClientState &last_packet_state,
146  double elapsed_since_last_packet,
147  double deltatime )
148 {
149  Prediction::InitInterpolation( un, last_packet_state, elapsed_since_last_packet, deltatime );
150 
151  interpolation_spline.createSpline( A0, A1, A2, A3 );
152 }
153 
154 //Don't know how to do here maybe should have a Spline for each 3 vectors of the rotation quaternion
155 //Do at least linear interpolation since we inherit from LinearPrediction
157 {
158  return OB; //un->curr_physical_state.orientation;
159 }
160 
162 {
163  //There should be another function called when received a new position update and creating the spline
164  if (this->deltatime == 0 || deltatime > this->deltatime) {
165  double delay = deltatime-this->deltatime;
166  return A2+VB*delay;
167  } else {
168  return QVector( interpolation_spline.computePoint( deltatime/this->deltatime ) );
169  }
170 }
171 
172 /*
173  ************************************************************************************
174  **** MixedPrediction class : Linear for orientation, CubicSpline for position ***
175  ************************************************************************************
176  */
177 
179 {
180  return LinearPrediction::InterpolateOrientation( un, deltatime );
181 }
182 
183 QVector MixedPrediction::InterpolatePosition( Unit *un, double deltatime ) const
184 {
185  return CubicSplinePrediction::InterpolatePosition( un, deltatime );
186 }
187 
188 Transformation MixedPrediction::Interpolate( Unit *un, double deltatime ) const
189 {
190  Transformation linear( LinearPrediction::Interpolate( un, deltatime ) );
191  linear.position = CubicSplinePrediction::InterpolatePosition( un, deltatime );
192  return linear;
193 }
194