Debian Codegraph @

src/game/tire.cpp

raw symbols SCIP
1 // This file is part of Dust Racing 2D.
2 // Copyright (C) 2014 Jussi Lind <[email protected]>
3 //
4 // Dust Racing 2D is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 // Dust Racing 2D is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>.
15
16 #include "tire.hpp"
17 #include "car.hpp"
18
19 #include <MCAssetManager>
20 #include <MCMathUtil>
21 #include <MCPhysicsComponent>
22 #include <MCSurface>
23 #include <MCTrigonom>
24
25 Tire::Tire(Car & car, float friction, float offTrackFriction)
26 : MCObject(MCAssetManager::surfaceManager().surface("frontTire"), "Tire")
27 , m_isOffTrack(false)
28 , m_friction(friction)
29 , m_offTrackFriction(offTrackFriction)
30 , m_spinCoeff(1.0f)
31 , m_car(car)
32 {
33 setBypassCollisions(true);
34 }
35
36 void Tire::setIsOffTrack(bool flag)
37 {
38 m_isOffTrack = flag;
39 }
40
41 void Tire::setSpinCoeff(float spinCoeff)
42 {
43 m_spinCoeff = spinCoeff;
44 }
45
46 void Tire::onStepTime(int)
47 {
48 if (physicsComponent().velocity().lengthFast() > 0)
49 {
50 const float tireNormalAngle = angle() + 90;
51 const MCVector2dF tire(
52 MCTrigonom::cos(tireNormalAngle), MCTrigonom::sin(tireNormalAngle));
53 MCVector2dF v = physicsComponent().velocity();
54 v.clampFast(0.999f); // Clamp instead of normalizing to avoid artifacts on small values
55 MCVector2dF impulse =
56 MCVector2dF::projection(v, tire) * (m_isOffTrack ? m_offTrackFriction : m_friction) * m_spinCoeff * -MCWorld::instance().gravity().k() * parent().physicsComponent().mass();
57 impulse.clampFast(parent().physicsComponent().mass() * 7.0f * m_car.tireWearFactor());
58 parent().physicsComponent().addForce(-impulse, location());
59
60 if (m_car.isBraking())
61 {
62 MCVector2dF impulse =
63 v * 0.5f * (m_isOffTrack ? m_offTrackFriction : m_friction) * -MCWorld::instance().gravity().k() * parent().physicsComponent().mass() * m_car.tireWearFactor();
64 parent().physicsComponent().addForce(-impulse, location());
65 }
66 }
67 }