Rigs of Rods 2023.09
Soft-body Physics Simulation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
AirBrake.cpp
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
6 For more information, see http://www.rigsofrods.org/
7
8 Rigs of Rods is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 3, as
10 published by the Free Software Foundation.
11
12 Rigs of Rods is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21#include "AirBrake.h"
22
23#include "Application.h"
24#include "SimData.h"
25#include "Actor.h"
26#include "GfxActor.h"
27#include "GfxScene.h"
28
29#include <Ogre.h>
30
31using namespace Ogre;
32using namespace RoR;
33
34Airbrake::Airbrake(ActorPtr actor, const char* basename, int num, node_t* ndref, node_t* ndx, node_t* ndy, node_t* nda, Ogre::Vector3 pos, float width, float length, float maxang, std::string const & texname, float tx1, float ty1, float tx2, float ty2, float lift_coef)
35{
36 snode = 0;
37 noderef = ndref;
38 nodex = ndx;
39 nodey = ndy;
40 nodea = nda;
41 offset = pos;
42 maxangle = maxang;
43 area = width * length * lift_coef;
44 char meshname[256];
45 sprintf(meshname, "airbrakemesh-%s-%i", basename, num);
47 msh = MeshManager::getSingleton().createManual(meshname, actor->GetGfxActor()->GetResourceGroup());
48
49 union
50 {
51 float* vertices;
52 CoVertice_t* covertices;
53 };
54
56 SubMesh* sub = msh->createSubMesh();
57
58 //materials
59 sub->setMaterialName(texname);
60
62 size_t nVertices = 4;
63 size_t vbufCount = (2 * 3 + 2) * nVertices;
64 vertices = (float*)malloc(vbufCount * sizeof(float));
65
66 //textures coordinates
67 covertices[0].texcoord = Vector2(tx1, ty1);
68 covertices[1].texcoord = Vector2(tx2, ty1);
69 covertices[2].texcoord = Vector2(tx2, ty2);
70 covertices[3].texcoord = Vector2(tx1, ty2);
71
74 size_t ibufCount = 3 * 4;
75 unsigned short* faces = (unsigned short*)malloc(ibufCount * sizeof(unsigned short));
76 faces[0] = 0;
77 faces[1] = 1;
78 faces[2] = 2;
79 faces[3] = 0;
80 faces[4] = 2;
81 faces[5] = 3;
82 faces[6] = 0;
83 faces[7] = 2;
84 faces[8] = 1;
85 faces[9] = 0;
86 faces[10] = 3;
87 faces[11] = 2;
88
89 //set coords
90 covertices[0].vertex = Vector3(0, 0, 0);
91 covertices[1].vertex = Vector3(width, 0, 0);
92 covertices[2].vertex = Vector3(width, 0, length);
93 covertices[3].vertex = Vector3(0, 0, length);
94
95 covertices[0].normal = Vector3(0, 1, 0);
96 covertices[1].normal = Vector3(0, 1, 0);
97 covertices[2].normal = Vector3(0, 1, 0);
98 covertices[3].normal = Vector3(0, 1, 0);
99
101 msh->sharedVertexData = new VertexData();
102 msh->sharedVertexData->vertexCount = nVertices;
103
105 VertexDeclaration* decl = msh->sharedVertexData->vertexDeclaration;
106 size_t offset = 0;
107 decl->addElement(0, offset, VET_FLOAT3, VES_POSITION);
108 offset += VertexElement::getTypeSize(VET_FLOAT3);
109 decl->addElement(0, offset, VET_FLOAT3, VES_NORMAL);
110 offset += VertexElement::getTypeSize(VET_FLOAT3);
111 // decl->addElement(0, offset, VET_FLOAT3, VES_DIFFUSE);
112 // offset += VertexElement::getTypeSize(VET_FLOAT3);
113 decl->addElement(0, offset, VET_FLOAT2, VES_TEXTURE_COORDINATES, 0);
114 offset += VertexElement::getTypeSize(VET_FLOAT2);
115
118 HardwareVertexBufferSharedPtr vbuf =
119 HardwareBufferManager::getSingleton().createVertexBuffer(
120 offset, msh->sharedVertexData->vertexCount, HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
121
123 vbuf->writeData(0, vbuf->getSizeInBytes(), vertices, true);
124
126 VertexBufferBinding* bind = msh->sharedVertexData->vertexBufferBinding;
127 bind->setBinding(0, vbuf);
128
130 HardwareIndexBufferSharedPtr faceibuf = HardwareBufferManager::getSingleton().
131 createIndexBuffer(
132 HardwareIndexBuffer::IT_16BIT,
133 ibufCount,
134 HardwareBuffer::HBU_STATIC_WRITE_ONLY);
135
137 faceibuf->writeData(0, faceibuf->getSizeInBytes(), faces, true);
138
140 sub->useSharedVertices = true;
141 sub->indexData->indexBuffer = faceibuf;
142 sub->indexData->indexCount = ibufCount;
143 sub->indexData->indexStart = 0;
144
146 msh->_setBounds(AxisAlignedBox(-1, -1, 0, 1, 1, 0), true);
147 //msh->_setBoundingSphereRadius(Math::Sqrt(1*1+1*1));
148
150 msh->load();
151
152 // create the entity and scene node
153 char entname[256];
154 sprintf(entname, "airbrakenode-%s-%i", basename, num);
155 ec = App::GetGfxScene()->GetSceneManager()->createEntity(entname, meshname, actor->GetGfxActor()->GetResourceGroup());
156 snode = App::GetGfxScene()->GetSceneManager()->getRootSceneNode()->createChildSceneNode();
157 snode->attachObject(ec);
158
159 updatePosition(0.0);
160
161 free(vertices);
162 free(faces);
163}
164
165void Airbrake::updatePosition(float amount)
166{
167 ratio = amount;
168 // Visual update moved from here to `GfxActor::UpdateAirbrakes()`
169}
170
172{
173 //tropospheric model valid up to 11.000m (33.000ft)
174 float altitude = noderef->AbsPosition.y;
175 //float sea_level_temperature=273.15+15.0; //in Kelvin
176 float sea_level_pressure = 101325; //in Pa
177 //float airtemperature=sea_level_temperature-altitude*0.0065; //in Kelvin
178 float airpressure = sea_level_pressure * pow(1.0 - 0.0065 * altitude / 288.15, 5.24947); //in Pa
179 float airdensity = airpressure * 0.0000120896;//1.225 at sea level
180
181 Vector3 wind = -noderef->Velocity;
182 float wspeed = wind.length();
183
184 Vector3 drag = (1.2 * area * sin(fabs(ratio * maxangle / 57.3)) * 0.5 * airdensity * wspeed / 4.0) * wind;
185 noderef->Forces += drag;
186 nodex->Forces += drag;
187 nodey->Forces += drag;
188 nodea->Forces += drag;
189}
Central state/object manager and communications hub.
Manager for all visuals belonging to a single actor.
Core data structures for simulation; Everything affected by by either physics, network or user intera...
GfxActor * GetGfxActor()
Definition Actor.h:309
void applyForce()
Definition AirBrake.cpp:171
node_t * nodex
Definition AirBrake.h:51
node_t * nodea
Definition AirBrake.h:53
Airbrake(ActorPtr actor, const char *basename, int num, node_t *ndref, node_t *ndx, node_t *ndy, node_t *nda, Ogre::Vector3 pos, float width, float length, float maxang, std::string const &texname, float tx1, float tx2, float tx3, float tx4, float lift_coef)
Definition AirBrake.cpp:34
float maxangle
attribute from truckfile
Definition AirBrake.h:56
Ogre::MeshPtr msh
Definition AirBrake.h:48
float ratio
Current state.
Definition AirBrake.h:55
node_t * nodey
Definition AirBrake.h:52
Ogre::SceneNode * snode
Definition AirBrake.h:49
node_t * noderef
Definition AirBrake.h:50
Ogre::Vector3 offset
gfx attribute
Definition AirBrake.h:54
void updatePosition(float amount)
Definition AirBrake.cpp:165
float area
Attribute set at spawn.
Definition AirBrake.h:57
Ogre::Entity * ec
Definition AirBrake.h:59
Ogre::String GetResourceGroup()
Definition GfxActor.h:146
Ogre::SceneManager * GetSceneManager()
Definition GfxScene.h:83
GfxScene * GetGfxScene()
Ogre::Vector2 texcoord
Definition AirBrake.h:45
Physics: A vertex in the softbody structure.
Definition SimData.h:260
Ogre::Vector3 AbsPosition
absolute position in the world (shaky)
Definition SimData.h:267
Ogre::Vector3 Velocity
Definition SimData.h:268
Ogre::Vector3 Forces
Definition SimData.h:269