1 #include "pch.h"
2 #include "CarPosInfo.h"
3 #include "Replay.h"
4 #include "../vdrift/cardynamics.h"
5 #include "../vdrift/car.h"
6 #include "common/data/SceneXml.h"
7 #include "common/Axes.h"
8 using namespace Ogre;
9 
10 
11 //  ctor
PosInfo()12 PosInfo::PosInfo()
13 	:bNew(false)  // not inited
14 	,pos(0,-200,0), percent(0.f), braking(0)
15 	,speed(0.f), fboost(0.f), steer(0.f)
16 	,hov_roll(0.f), hov_throttle(0.f)
17 	,fHitTime(0.f), fParIntens(0.f), fParVel(0.f)
18 {
19 	camPos = camOfs = Vector3::ZERO;
20 	rot = camRot = Quaternion::IDENTITY;
21 	carY = -Vector3::UNIT_Y;
22 	for (int w=0; w < MAX_WHEELS; ++w)
23 	{
24 		whPos[w] = Vector3::ZERO;
25 		whRot[w] = Quaternion::IDENTITY;
26 		whVel[w] = whSlide[w] = whSqueal[w] = 0.f;
27 		whTerMtr[w]=0;  whRoadMtr[w]=0;  whP[w]=0;
28 		whH[w] = whAngVel[w] = whSteerAng[w] = 0.f;
29 	}
30 	vHitPos = vHitNorm = Vector3::UNIT_Y;
31 }
32 
33 ///  pos from new Replay/ghost  New
34 //-----------------------------------------------------------------------
FromRpl2(const ReplayFrame2 * rf,CARDYNAMICS * cd)35 void PosInfo::FromRpl2(const ReplayFrame2* rf, CARDYNAMICS* cd)
36 {
37 	//  car
38 	Axes::toOgre(pos, rf->pos);
39 	if (cd && cd->vtype == V_Sphere)
40 	{
41 		cd->sphereYaw = rf->hov_roll;
42 		rot.FromAngleAxis(Radian(-rf->hov_roll), Vector3::UNIT_Y);
43 		carY = Vector3::UNIT_Y;
44 	}else
45 	{	rot = Axes::toOgre(rf->rot);
46 		carY = rot * Vector3::UNIT_Y;
47 	}
48 	//  hud
49 	speed = rf->speed;
50 	fboost = rf->fboost /255.f;  steer = rf->steer /127.f;
51 	braking = rf->get(b_braking);  percent = rf->percent /255.f*100.f;
52 	hov_roll = rf->hov_roll;	hov_throttle = rf->throttle /255.f;
53 
54 	fHitTime = rf->fHitTime;
55 	if (!rf->hit.empty())
56 	{
57 		const RHit& h = rf->hit[0];  //fHitForce = h.fHitForce;
58 		fParIntens = h.fParIntens;  fParVel = h.fParVel;
59 		vHitPos = h.vHitPos;  vHitNorm = h.vHitNorm;
60 	}
61 	//get(b_scrap) in car_sound
62 
63 	//  wheels
64 	int ww = rf->wheels.size();
65 	for (int w=0; w < ww; ++w)
66 	{
67 		const RWheel& wh = rf->wheels[w];
68 		Axes::toOgre(whPos[w], wh.pos);
69 		whRot[w] = Axes::toOgreW(wh.rot);
70 		//whR[w] = outside
71 
72 		whVel[w] = wh.whVel;
73 		whSlide[w] = wh.slide;  whSqueal[w] = wh.squeal;
74 
75 		whTerMtr[w] = wh.whTerMtr;  whRoadMtr[w] = wh.whRoadMtr;
76 
77 		whH[w] = wh.whH;  whP[w] = wh.whP;
78 		whAngVel[w] = wh.whAngVel;
79 		whSteerAng[w] = wh.whSteerAng;
80 	}
81 }
82 
83 //  pos from replay/ghost  Old
84 //-----------------------------------------------------------------------
FromRpl(const ReplayFrame * rf)85 void PosInfo::FromRpl(const ReplayFrame* rf)
86 {
87 	//  car
88 	Axes::toOgre(pos, rf->pos);
89 	rot = Axes::toOgre(rf->rot);
90 	carY = rot * Vector3::UNIT_Y;
91 
92 	//  hud
93 	speed = rf->speed;
94 	fboost = rf->fboost;  steer = rf->steer;
95 	braking = rf->braking;  percent = rf->percent;
96 	hov_roll = rf->hov_roll;  hov_throttle = rf->throttle;
97 
98 	fHitTime = rf->fHitTime;  fParIntens = rf->fParIntens;  fParVel = rf->fParVel;
99 	vHitPos = rf->vHitPos;  vHitNorm = rf->vHitNorm;
100 
101 	//  wheels
102 	for (int w=0; w < 4; ++w)
103 	{
104 		Axes::toOgre(whPos[w], rf->whPos[w]);
105 		whRot[w] = Axes::toOgreW(rf->whRot[w]);
106 		//whR[w] = outside
107 
108 		whVel[w] = rf->whVel[w];
109 		whSlide[w] = rf->slide[w];  whSqueal[w] = rf->squeal[w];
110 
111 		whTerMtr[w] = rf->whTerMtr[w];  whRoadMtr[w] = rf->whRoadMtr[w];
112 
113 		whH[w] = rf->whH[w];  whP[w] = rf->whP[w];
114 		whAngVel[w] = rf->whAngVel[w];
115 		if (w < 2)  whSteerAng[w] = rf->whSteerAng[w];
116 	}
117 }
118 
119 ///  pos from Simulation
120 //-----------------------------------------------------------------------
FromCar(CAR * pCar)121 void PosInfo::FromCar(CAR* pCar)
122 {
123 	const CARDYNAMICS* cd = &pCar->dynamics;
124 	//  car
125 	Axes::toOgre(pos, cd->GetPosition());
126 	if (cd->vtype == V_Sphere)
127 	{	rot.FromAngleAxis(Radian(-cd->sphereYaw), Vector3::UNIT_Y);
128 		carY = Vector3::UNIT_Y;
129 		hov_roll = -cd->sphereYaw;
130 	}else
131 	{	rot = Axes::toOgre(cd->GetOrientation());
132 		carY = rot * Vector3::UNIT_Y;
133 		hov_roll = cd->hov_roll;
134 	}
135 	//  hud
136 	speed = pCar->GetSpeed();
137 	fboost = cd->boostVal;	//posInfo.steer = cd->steer;
138 	braking = cd->IsBraking();  //percent = outside
139 	hov_throttle = cd->hov_throttle;
140 
141 	fHitTime = cd->fHitTime;  fParIntens = cd->fParIntens;  fParVel = cd->fParVel;
142 	vHitPos = cd->vHitPos;  vHitNorm = cd->vHitNorm;
143 
144 	//  wheels
145 	for (int w=0; w < cd->numWheels; ++w)
146 	{	WHEEL_POSITION wp = WHEEL_POSITION(w);
147 
148 		Axes::toOgre(whPos[w], cd->GetWheelPosition(wp));
149 		whRot[w] = Axes::toOgreW(cd->GetWheelOrientation(wp));
150 
151 		whVel[w] = cd->GetWheelVelocity(wp).Magnitude();
152 		whSlide[w] = -1.f;  whSqueal[w] = pCar->GetTireSquealAmount(wp, &whSlide[w]);  //!?
153 
154 		whTerMtr[w] = cd->whTerMtr[w];  whRoadMtr[w] = cd->whRoadMtr[w];
155 
156 		whH[w] = cd->whH[w];  whP[w] = cd->whP[w];
157 		whAngVel[w] = cd->wheel[w].GetAngularVelocity();
158 		if (w < 2)  whSteerAng[w] = cd->wheel[w].GetSteerAngle();
159 	}
160 	camOfs = Axes::toOgre(cd->cam_body.GetPosition());  ///..
161 }
162 
163 
164 //  replay from simulation  Old
165 //-----------------------------------------------------------------------
FromCar(const CAR * pCar)166 void ReplayFrame::FromCar(const CAR* pCar)
167 {
168 	//  car
169 	const CARDYNAMICS& cd = pCar->dynamics;
170 	pos = cd.GetPosition();
171 	rot = cd.GetOrientation();
172 
173 	//  wheels
174 	for (int w=0; w < cd.numWheels; ++w)
175 	{	WHEEL_POSITION wp = WHEEL_POSITION(w);
176 		whPos[w] = cd.GetWheelPosition(wp);
177 		whRot[w] = cd.GetWheelOrientation(wp);
178 
179 		const TRACKSURFACE* surface = cd.GetWheelContact(wp).GetSurfacePtr();
180 		surfType[w] = !surface ? TRACKSURFACE::NONE : surface->type;
181 		//  squeal
182 		slide[w] = -1.f;  squeal[w] = pCar->GetTireSquealAmount(wp, &slide[w]);
183 		whVel[w] = cd.GetWheelVelocity(wp).Magnitude();
184 		//  susp
185 		suspVel[w] = cd.GetSuspension(wp).GetVelocity();
186 		suspDisp[w] = cd.GetSuspension(wp).GetDisplacementPercent();
187 
188 		whTerMtr[w] = cd.whTerMtr[w];  whRoadMtr[w] = cd.whRoadMtr[w];
189 		//  fluids
190 		whH[w] = cd.whH[w];  whP[w] = cd.whP[w];
191 		whAngVel[w] = cd.wheel[w].GetAngularVelocity();
192 		bool inFl = cd.inFluidsWh[w].size() > 0;
193 		int idPar = -1;
194 		if (inFl)
195 		{	const FluidBox* fb = *cd.inFluidsWh[w].begin();
196 			idPar = fb->idParticles;  }
197 		whP[w] = idPar;
198 		if (w < 2)  whSteerAng[w] = cd.wheel[w].GetSteerAngle();
199 	}
200 	//  hud
201 	vel = pCar->GetSpeedometer();  rpm = pCar->GetEngineRPM();
202 	gear = pCar->GetGear();  clutch = pCar->GetClutch();
203 	throttle = cd.GetThrottle();
204 	steer = pCar->GetLastSteer();
205 	fboost = cd.doBoost;
206 	//  eng snd
207 	posEngn = cd.GetEnginePosition();
208 	speed = pCar->GetSpeed();
209 	dynVel = cd.GetVelocity().Magnitude();
210 	braking = cd.IsBraking();
211 	if (cd.vtype == V_Sphere)
212 		hov_roll = cd.sphereYaw;
213 	else
214 		hov_roll = cd.hov_roll;
215 
216 	//  hit sparks
217 	fHitTime = cd.fHitTime;	fParIntens = cd.fParIntens;	fParVel = cd.fParVel;
218 	vHitPos = cd.vHitPos;	vHitNorm = cd.vHitNorm;
219 	whMudSpin = pCar->sounds.whMudSpin;
220 	fHitForce = cd.fHitForce;
221 	fCarScrap = std::min(1.f, cd.fCarScrap);
222 	fCarScreech = std::min(1.f, cd.fCarScreech);
223 }
224 
225 ///  replay from Simulation  New
226 //-----------------------------------------------------------------------
FromCar(const CAR * pCar,half prevHitTime)227 void ReplayFrame2::FromCar(const CAR* pCar, half prevHitTime)
228 {
229 	//  car
230 	const CARDYNAMICS& cd = pCar->dynamics;
231 	pos = cd.GetPosition();
232 	rot = cd.GetOrientation();
233 
234 	//  wheels
235 	//wheels.clear();
236 	for (int w=0; w < cd.numWheels; ++w)
237 	{
238 		RWheel wh;
239 		WHEEL_POSITION wp = WHEEL_POSITION(w);
240 		wh.pos = cd.GetWheelPosition(wp);
241 		wh.rot = cd.GetWheelOrientation(wp);
242 
243 		const TRACKSURFACE* surface = cd.GetWheelContact(wp).GetSurfacePtr();
244 		wh.surfType = !surface ? TRACKSURFACE::NONE : surface->type;
245 		//  squeal
246 		float slide = -1.f;
247 		wh.squeal = pCar->GetTireSquealAmount(wp, &slide);  wh.slide = slide;
248 		wh.whVel = cd.GetWheelVelocity(wp).Magnitude();
249 		//  susp
250 		wh.suspVel = cd.GetSuspension(wp).GetVelocity();
251 		wh.suspDisp = cd.GetSuspension(wp).GetDisplacementPercent();
252 
253 		wh.whTerMtr = cd.whTerMtr[w];  wh.whRoadMtr = cd.whRoadMtr[w];
254 		//  fluids
255 		wh.whH = cd.whH[w];  wh.whP = cd.whP[w];
256 		wh.whAngVel = cd.wheel[w].GetAngularVelocity();
257 		bool inFl = cd.inFluidsWh[w].size() > 0;
258 		int idPar = -1;
259 		if (inFl)
260 		{	const FluidBox* fb = *cd.inFluidsWh[w].begin();
261 			idPar = fb->idParticles;  }
262 		wh.whP = idPar;
263 		wh.whSteerAng = cd.wheel[w].GetSteerAngle();
264 		wheels.push_back(wh);
265 	}
266 	//  hud
267 	vel = pCar->GetSpeedometer();  rpm = pCar->GetEngineRPM();  gear = pCar->GetGear();
268 	throttle = cd.GetThrottle() *255.f;  clutch = pCar->GetClutch() *255.f;
269 	steer = pCar->GetLastSteer() *127.f;  fboost = cd.doBoost *255.f;
270 	damage = cd.fDamage /100.f*255.f;  //percent set outside
271 
272 	//  eng snd
273 	speed = pCar->GetSpeed();  dynVel = cd.GetVelocity().Magnitude();
274 	set(b_braking, cd.IsBraking());
275 
276 	bool hov = cd.vtype != V_Car;
277 	set(b_hov, hov);
278 	if (hov)
279 		hov_roll = cd.vtype == V_Sphere ? cd.sphereYaw : cd.hov_roll;
280 
281 
282 	// fluid
283 	bool mud = pCar->sounds.whMudSpin < 0.01f;
284 	set(b_fluid, mud);
285 	whMudSpin = pCar->sounds.whMudSpin;
286 
287 	//  scrap
288 	bool scr = cd.fCarScrap > 0.01f || cd.fCarScreech > 0.01f;
289 	set(b_scrap, scr);
290 	if (scr)
291 	{	RScrap sc;
292 		sc.fScrap = std::min(1.f, cd.fCarScrap);
293 		sc.fScreech = std::min(1.f, cd.fCarScreech);
294 		scrap.push_back(sc);
295 	}
296 
297 	//  hit sparks
298 	fHitTime = cd.fHitTime;
299 	bool ht = fHitTime >= prevHitTime;
300 	set(b_hit, ht);
301 	if (ht)  // hit, new data
302 	{
303 		RHit h;
304 		h.fHitForce = cd.fHitForce;
305 		h.fParIntens = cd.fParIntens;  h.fParVel = cd.fParVel;
306 		h.vHitPos = cd.vHitPos;  h.vHitNorm = cd.vHitNorm;
307 		hit.push_back(h);
308 	}
309 }
310