-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathLowSpeedTurn.cpp
242 lines (187 loc) · 7.67 KB
/
LowSpeedTurn.cpp
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
// ----------------------------------------------------------------------------
//
//
// OpenSteer -- Steering Behaviors for Autonomous Characters
//
// Copyright (c) 2002-2005, Sony Computer Entertainment America
// Original author: Craig Reynolds <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
//
// ----------------------------------------------------------------------------
//
//
// "Low Speed Turn" test fixture
//
// Used to evaluate vehicle response at low speed to backward-directed
// steering force
//
// 08-20-02 cwr: created
//
//
// ----------------------------------------------------------------------------
#include <iomanip>
#include <sstream>
#include "OpenSteer/SimpleVehicle.h"
#include "OpenSteer/OpenSteerDemo.h"
#include "OpenSteer/Color.h"
namespace {
using namespace OpenSteer;
// ----------------------------------------------------------------------------
class LowSpeedTurn : public SimpleVehicle
{
public:
// constructor
LowSpeedTurn () {reset ();}
// reset state
void reset (void)
{
// reset vehicle state
SimpleVehicle::reset ();
// speed along Forward direction.
setSpeed (startSpeed);
// initial position along X axis
setPosition (startX, 0, 0);
// steering force clip magnitude
setMaxForce (0.3f);
// velocity clip magnitude
setMaxSpeed (1.5f);
// for next instance: step starting location
startX += 2;
// for next instance: step speed
startSpeed += 0.15f;
// 15 seconds and 150 points along the trail
setTrailParameters (15, 150);
}
// draw into the scene
void draw (void)
{
drawBasic2dCircularVehicle (this, gGray50);
drawTrail ();
}
// per frame simulation update
void update (const float currentTime, const float elapsedTime)
{
applySteeringForce (steering (), elapsedTime);
// annotation
annotationVelocityAcceleration ();
recordTrailVertex (currentTime, position());
}
// reset starting positions
static void resetStarts (void)
{
startX = 0;
startSpeed = 0;
}
// constant steering force
Vec3 steering (void) {return Vec3 (1, 0, -1);}
// for stepping the starting conditions for next vehicle
static float startX;
static float startSpeed;
};
float LowSpeedTurn::startX;
float LowSpeedTurn::startSpeed;
// ----------------------------------------------------------------------------
// PlugIn for OpenSteerDemo
const int lstCount = 5;
const float lstLookDownDistance = 18;
const Vec3 lstViewCenter (7, 0, -2);
const Vec3 lstPlusZ (0, 0, 1);
class LowSpeedTurnPlugIn : public PlugIn
{
public:
const char* name (void) {return "Low Speed Turn";}
float selectionOrderSortKey (void) {return 0.05f;}
// be more "nice" to avoid a compiler warning
virtual ~LowSpeedTurnPlugIn() {}
void open (void)
{
// create a given number of agents with stepped inital parameters,
// store pointers to them in an array.
LowSpeedTurn::resetStarts ();
for (int i = 0; i < lstCount; i++) all.push_back (new LowSpeedTurn);
// initial selected vehicle
OpenSteerDemo::selectedVehicle = *all.begin();
// initialize camera
OpenSteerDemo::camera.mode = Camera::cmFixed;
OpenSteerDemo::camera.fixedUp = lstPlusZ;
OpenSteerDemo::camera.fixedTarget = lstViewCenter;
OpenSteerDemo::camera.fixedPosition = lstViewCenter;
OpenSteerDemo::camera.fixedPosition.y += lstLookDownDistance;
OpenSteerDemo::camera.lookdownDistance = lstLookDownDistance;
OpenSteerDemo::camera.fixedDistVOffset = OpenSteerDemo::camera2dElevation;
OpenSteerDemo::camera.fixedDistDistance = OpenSteerDemo::cameraTargetDistance;
}
void update (const float currentTime, const float elapsedTime)
{
// update, draw and annotate each agent
for (iterator i = all.begin(); i != all.end(); i++)
{
(**i).update (currentTime, elapsedTime);
}
}
void redraw (const float currentTime, const float elapsedTime)
{
// selected vehicle (user can mouse click to select another)
AbstractVehicle* selected = OpenSteerDemo::selectedVehicle;
// vehicle nearest mouse (to be highlighted)
AbstractVehicle* nearMouse = OpenSteerDemo::vehicleNearestToMouse ();
// update camera
OpenSteerDemo::updateCamera (currentTime, elapsedTime, selected);
// draw "ground plane"
OpenSteerDemo::gridUtility (selected->position());
// update, draw and annotate each agent
for (iterator i = all.begin(); i != all.end(); i++)
{
// draw this agent
LowSpeedTurn& agent = **i;
agent.draw ();
// display speed near agent's screen position
const Color textColor (0.8f, 0.8f, 1.0f);
const Vec3 textOffset (0, 0.25f, 0);
const Vec3 textPosition = agent.position() + textOffset;
std::ostringstream annote;
annote << std::setprecision (2)
<< std::setiosflags (std::ios::fixed)
<< agent.speed()
<< std::ends;
draw2dTextAt3dLocation (annote, textPosition, textColor, drawGetWindowWidth(), drawGetWindowHeight());
}
// highlight vehicle nearest mouse
OpenSteerDemo::highlightVehicleUtility (nearMouse);
}
void close (void)
{
for (iterator i = all.begin(); i!=all.end(); i++) delete (*i);
all.clear ();
}
void reset (void)
{
// reset each agent
LowSpeedTurn::resetStarts ();
for (iterator i = all.begin(); i!=all.end(); i++) (**i).reset();
}
const AVGroup& allVehicles (void) {return (const AVGroup&) all;}
std::vector<LowSpeedTurn*> all; // for allVehicles
typedef std::vector<LowSpeedTurn*>::const_iterator iterator;
};
LowSpeedTurnPlugIn gLowSpeedTurnPlugIn;
// ----------------------------------------------------------------------------
} // anonymous namespace