summaryrefslogblamecommitdiffstats
path: root/game/code/worldsim/redbrick/wheel.cpp
blob: f8899097309ab4b61da714095e593a3600d6def9 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533




















































































































































































































































































































































































































































































































































                                                                                                                               
/*===========================================================================
   wheel.cpp

   created Jan 29, 2002
   by Greg Mayer

   Copyright (c) 2002 Radical Entertainment, Inc.
   All rights reserved.


===========================================================================*/

#include <worldsim/redbrick/wheel.h>
#include <radmath/radmath.hpp>
#include <worldsim/worldphysicsmanager.h>


//------------------------------------------------------------------------
Wheel::Wheel()
{
    mRadius = 0.0f;    
    mObjectSpaceYOffsetFromCurrentPosition = 0.0f;

    mWheelNum = -1;     // unset flag
    mYOffset = 0.0f;

    mLimit = 0.0f;
    mk = 0.0f;   
    mc = 0.0f;   

    mWheelInCollision = false;  // TODO - is this method adequate!?
    mWheelBottomedOutThisFrame = false;

    mSpringRelaxRate = 0.5f;    // second to move from limit to 0 when not in collision

    mWheelTurnAngle = 0.0f;
    
    mRotAngle = 0.0f;
    mCumulativeRot = 0.0f;
    mTotalTime = 0.0f;

    mWheelRenderSpeedRadPerSecond = 0.0f;
    //mSpinUpRate = 10.0f;    //? - I think this is in (rad/s)/s

    //mRenderingSpinUpRateBase = 0.1f;    // this is actually in units of rad/s - 
                           // it's a direct setting of the increment amount, for now

    mRenderingSpinUpRateBase = 100.0f;    // back to rad/s

    mRenderingSpinUpRate = 0.0f; // this one is scaled by the gas input

    // % of top speed at which mode goes back to NORMAL, if we were slipping 'cause we floored it initially
    mDpRenderingSpinUpSpeedThresholdPercentage = 0.3f;
    //mDpRenderingSpinUpSpeedThresholdPercentage = 0.2f;


    mSteerWheel = false;
    mDriveWheel = false;

   
    mVehicle = 0;

    //----------------
    // designer values
    //----------------

    mWheelState = WS_NORMAL;


    //---------
    // redbrick     normal
    //---------
 
    // NOTE: these are all overwritten shortly after init
    // these are just default values that are ok for the ferrari
    
    mDpSuspensionLimit = 0.4f;  // try this for ferrari
    
    mDpSpringk = 0.5f; // prett good value for redbrick
    mDpDamperc = 0.2f;  // scale on critical damping            temp for debugging

      
}
    

//------------------------------------------------------------------------
Wheel::~Wheel()
{
    //
}


//------------------------------------------------------------------------
void Wheel::Init(Vehicle* vehicle, int wheelNum, float radius, bool steer, bool drive)
{
    mVehicle = vehicle;

    mWheelNum = wheelNum;
    mRadius = radius;  

    mSteerWheel = steer;
    mDriveWheel = drive;

    //
    // calculate 'real' values from designer params
    //

    mLimit = mDpSuspensionLimit * mRadius;

    // TODO
    // temp hack for lack of phizsim
    //float gravity_y = 9.81f;

    const rmt::Vector& gravity = GetWorldPhysicsManager()->mSimEnvironment->Gravity();// this or CGS ?

    //float gravity_y = 9.81f;
    float gravity_y = -1.0f * gravity.y;

    //rmt::Vector gravity = SG::phizSim.mSimEnvironment->Gravity();
    //float force = mVehicle->mDesignerParams.mDpMass * rmt::Fabs(gravity.y);
    float force = mVehicle->mDesignerParams.mDpMass * gravity_y;
    mk = force / mLimit;
    mk *= mDpSpringk;

    //mqk = rmt::Sqrt(mk) * 2.0f; // first guess at quad spring
    mqk = mk * 5.0f; // first guess at quad spring
    //mqk = mk * 10.0f; // first guess at quad spring

    float criticalDamping = 2.0f * (float)rmt::Sqrt(mk * mVehicle->mDesignerParams.mDpMass);
    mc = criticalDamping * mDpDamperc;
    

    // these will get recalculated wheh SetDesignParams is called

}

//------------------------------------------------------------------------
void Wheel::SetDesignerParams(Vehicle::DesignerParams* dp)
{

    mDpSuspensionLimit = dp->mDpSuspensionLimit;
    
    mDpSpringk = dp->mDpSpringk;
    mDpDamperc = dp->mDpDamperc;

    
    //----------

    mLimit = mDpSuspensionLimit * mRadius;

    // TODO
    // temp hack for lack of phizsim
    //float gravity_y = 9.81f;

    const rmt::Vector& gravity = GetWorldPhysicsManager()->mSimEnvironment->Gravity();// this or CGS ?

    //float gravity_y = 9.81f;
    float gravity_y = -1.0f * gravity.y;

    //rmt::Vector gravity = SG::phizSim.mSimEnvironment->Gravity();
    //float force = mPhysicsVehicleOwner->mDpMass * rmt::Fabs(gravity.y);
    float force = mVehicle->mDesignerParams.mDpMass * gravity_y;
    mk = force / mLimit;
    mk *= mDpSpringk;

    //mqk = rmt::Sqrt(mk) * 2.0f; // first guess at quad spring
    mqk = mk * 5.0f; // first guess at quad spring
    //mqk = mk * 10.0f; // first guess at quad spring


    float criticalDamping = 2.0f * (float)rmt::Sqrt(mk * mVehicle->mDesignerParams.mDpMass);
    mc = criticalDamping * mDpDamperc;


    CalculateRenderingSpinUpRateBase(dp->mDpTopSpeedKmh);

}

//------------------------------------------------------------------------
float Wheel::CalculateSuspensionForce(float suspensionPointYVelocity, float dt)
{
    
    rAssert(0);

    float force = 0.0f;

    if(mWheelInCollision)
    {

        float springForce = mk * mYOffset;

        // apply extra force if we're close to bottoming out
        //
        // TODO
        // hmmmm..... revisit this...
        // replace with quadratic

        //
        // or... topping out!
        //
        if((mLimit - rmt::Fabs(mYOffset)) < mLimit * 0.25f)        
        {            
            springForce *= 3.0f;
        }

        // for now, only add damping if chassis is trying to compress suspension - ie. y velocity is down, -ve
        float damperForce = 0.0f;

        if(suspensionPointYVelocity < 0.0f)
        {
            damperForce = mc * -1.0f * suspensionPointYVelocity;
        }
        else
        {
            // like the havok guys, only let damper pull down at about 1/3 of push up
            damperForce = mc * -1.0f * suspensionPointYVelocity * 0.3f;

        }
 
        force = springForce + damperForce;
    }
    else
    {
        // need to relax spring somehow
        float relaxDistance = mLimit / mSpringRelaxRate * dt;
        if(mYOffset >= relaxDistance)
        {
            mYOffset -= relaxDistance;
        }
        else if(mYOffset <= -relaxDistance)
        {
            mYOffset += relaxDistance;
        }

    }
    return force;


}



//------------------------------------------------------------------------------------------------
void Wheel::CalculateRenderingSpinUpRateBase(float topSpeedKmh)
{
    // mDpRenderingSpinUpSpeedThreshold;    
    
    // at what rad/s is the wheel going % of top speed

    float topspeedmps = topSpeedKmh / 3.6f;

    float thresholdmps = topspeedmps * mDpRenderingSpinUpSpeedThresholdPercentage;

    float linearDistancePerRev = rmt::PI * mRadius * 2.0f;

    // rev/s = rev/dist * dist/s

    // ! fuck you idiot
    // we need rads not revs
    float linearDistancePerRad = linearDistancePerRev / (2.0f * rmt::PI);
    

    mRenderingSpinUpRateBase = (1.0f / linearDistancePerRad) * thresholdmps;



}


//------------------------------------------------------------------------
void Wheel::CalculateRotAngle(float dt) 
{
    if(mVehicle->mEBrake > 0.0f && mWheelInCollision)
    {    
        // now, if no gas, don't rotate
        if(mVehicle->mGas == 0.0f && this->mDriveWheel == true)
        {
            mRotAngle = 0.0f;     
            return;
        }
                
    }


    float speedAlongFacing;

    rmt::Vector wheelDirection = mVehicle->mVehicleFacing;

    if(mSteerWheel)
    {        
        rmt::Matrix steeringMatrix; // TODO - make this a class member?
        steeringMatrix.Identity();
        steeringMatrix.FillRotateY(mWheelTurnAngle);

        wheelDirection.Transform(steeringMatrix);
    }

    speedAlongFacing = wheelDirection.DotProduct(mVehicle->mSuspensionPointVelocities[mWheelNum]);

    //float linearDistance = speedAlongFacing * dt;

    float linearDistancePerRev = rmt::PI * mRadius * 2.0f;

    float revPerTime = speedAlongFacing / linearDistancePerRev;

    float radiansPerTime = revPerTime * 2.0f * rmt::PI;

    // ?
    // see if vehicle's doin' a burnout
    if(mVehicle->mBurnoutLevel > 0.0f && mDriveWheel)
    {
        if(mVehicle->mGas > mVehicle->mBrake)
        {
            radiansPerTime = mRenderingSpinUpRateBase;
        }
        else
        {
           radiansPerTime = -1.0f * mRenderingSpinUpRateBase;
        }
    }


    /*
    if(mWheelState == WS_LOCOMOTIVE_SLIDE || mWheelState == WS_LOCOMOTIVE_FREE_SPIN)
    {
        if(radiansPerTime >= mRenderingSpinUpRate)
        {
            // kick out of thid mode and use the calculated value
            //
            // TODO - how to then easily tune the mRenderingSpinUpRateBase?
            //
            // should be able to calculate based on some % of top speed
            // yeah - no fucking timer needed!

            mWheelState = WS_NORMAL;

        }
        else
        {
            //mRotAngle *= 1.5f;
            //otAngle = mRenderingSpinUpRate;
            radiansPerTime = mRenderingSpinUpRate;
        }
        
    }
    */

    mRotAngle = radiansPerTime * dt;

    //if(rmt::Fabs(radiansPerTime - (2.0f * rmt::PI)) < 0.1f)
    //{
    //    int stophere = 1;
   // }


    // fucking hack to (temporarily?) fix wheel rot on ps2
    /*
    mTotalTime += dt;

    if(mTotalTime > 30.0f)  // value is in seconds
    {
        mTotalTime = 0.0f;
        mCumulativeRot = 0.0f;
    }
    */

    mCumulativeRot += mRotAngle;
   
    if(mCumulativeRot > rmt::PI_2)
    {
        mCumulativeRot -= rmt::PI_2;

        //safeguard - higly unlikely we'd get here...
        if(mCumulativeRot > rmt::PI_2)
        {
            // still?
            // something strange must have happened
            mCumulativeRot = 0.0f;
        }
        
    }
   



    // TODO 
    // what we might want to try here is always calculate the wheel rotation to render based on desired forces
    // not the actual forces we apply.
    //
    // add booleans to the Cap methods?
    //
    // also need to throw in some consideration for braking - draw wheels locked.
    //
    // furthermore, this should be done before the vehicle PostUpdate where the pose drivers actually change the joint matrices


}

//=============================================================================
// Wheel::SetYOffsetFromCurrentPosition
//=============================================================================
// Description: 
//
// Parameters:  (float yoffset)
//
// Return:      float 
//
//      note -  if we've bottomed out, this will return the overflow
//              amount that the suspension could not absorb
//              if we didn't bottom out it will return 0.0f;
//
//
//=============================================================================
float Wheel::SetYOffsetFromCurrentPosition(float yoffset)
{

    // the parameter mObjectSpaceYOffsetFromCurrentPosition is a bit verbose at this
    // point since before doing wheel collision we set the wheels to the bottom of the 
    // suspension limit, so at this poitn the currentposition is always the same

    if(yoffset > mObjectSpaceYOffsetFromCurrentPosition)
    {
        mObjectSpaceYOffsetFromCurrentPosition = yoffset;
    }
    // TODO - make sure this gets reset to 0.0 at the right place


    // this call got propagated to us from the collision solver, so I think this is the place to set the flag
    mWheelInCollision = true;    

    // temp
    // TODO - remove
    //return 0.0f;


    if(mObjectSpaceYOffsetFromCurrentPosition > 2.0f * mLimit)
    {
        // suspension has bottomed out
        //
        //  TODO - rename this method?
        //
        // TODO - double check this

        float overflow = mObjectSpaceYOffsetFromCurrentPosition - (2.0f * mLimit);
        mObjectSpaceYOffsetFromCurrentPosition = 2.0f * mLimit;

        return overflow;
        //return true;
    }

    // ??
    // TODO
    // is this ok?
    // should be if we always started out at the bottom of the suspension limit
    if(mObjectSpaceYOffsetFromCurrentPosition < 0.0f)
    {
        rAssert(0);
        mObjectSpaceYOffsetFromCurrentPosition = 0.0f;
    }

    return 0.0f;

}


//------------------------------------------------------------------------
void Wheel::ResolveOffset()
{
    // we assume at this point anyone who wants to has called SetYOffsetFromCurrentPosition
    mYOffset += mObjectSpaceYOffsetFromCurrentPosition;


    // temp
    // TODO - remove
    //mObjectSpaceYOffsetFromCurrentPosition = 0.0f;
    //return;
    
    if(mYOffset > mLimit)
    {
        mYOffset = mLimit;
    }
    if(mYOffset < -mLimit)
    {
        mYOffset = -mLimit;
    }

    // !
    //
    // mYOffset is only of use internally to this class
    //
    // this is NOT the value other shit should use to move suspension points around and what have you.



    mObjectSpaceYOffsetFromCurrentPosition = 0.0f;
    //mWheelInCollision = false;  // TODO - is this method adequate!?

}


//------------------------------------------------------------------------
float Wheel::GetYCorrectionValue()
{
    return mObjectSpaceYOffsetFromCurrentPosition;
}




//------------------------------------------------------------------------
void Wheel::Reset()
{
    mYOffset = 0.0f;
    mObjectSpaceYOffsetFromCurrentPosition = 0.0f;

    mRotAngle = 0.0f;

    mCumulativeRot = 0.0f;
    mTotalTime = 0.0f;

    mWheelInCollision = false;
    mWheelBottomedOutThisFrame = false;

    mWheelState = WS_NORMAL;

    mWheelRenderSpeedRadPerSecond = 0.0f;

}