HOG2
SteeringEnvironment.cpp
Go to the documentation of this file.
1 /*
2  * SteeringEnvironment.cpp
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 4/12/11.
6  * Copyright 2011 University of Denver. All rights reserved.
7  *
8  */
9 
10 #include "SteeringEnvironment.h"
11 
12 float maxSpeed = 0.5f;
13 float worldRadius = 200.0f;
14 
15 void SteeringEnvironment::GetSuccessors(const steeringState &nodeID, std::vector<steeringState> &neighbors) const
16 {
17  std::vector<steeringAction> act;
18  GetActions(nodeID, act);
19  for (unsigned int x = 0; x < act.size(); x++)
20  {
21  steeringState s = nodeID;
22  ApplyAction(s, act[x]);
23  neighbors.push_back(s);
24  }
25 }
26 
27 void SteeringEnvironment::GetActions(const steeringState &nodeID, std::vector<steeringAction> &actions) const
28 {
29  actions.resize(0);
30  actions.push_back(steeringAction(-1, -1));
31  actions.push_back(steeringAction(-1, 0));
32  actions.push_back(steeringAction(-1, 1));
33  actions.push_back(steeringAction( 0, -1));
34  actions.push_back(steeringAction( 0, 0));
35  actions.push_back(steeringAction( 0, 1));
36  actions.push_back(steeringAction( 1, -1));
37  actions.push_back(steeringAction( 1, 0));
38  actions.push_back(steeringAction( 1, -1));
39 }
40 
42 {
43  assert(false);
44  return 0;
45 }
46 
48 {
49  // scale movement vector
50  if (a.dv > maxSpeed)
51  {
52  a.dv = maxSpeed;
53  }
54  else if (a.dv < -maxSpeed)
55  a.dv = -maxSpeed;
56 
57  float yoffset = sin(TWOPI*s.heading/360.0)*s.v;//sin(TWOPI*rot/16)*rad;
58  float xoffset = cos(TWOPI*s.heading/360.0)*s.v;//cos(TWOPI*rot/16)*rad;
59 
60  s.x += xoffset;//a.dx;
61  s.y += yoffset;//a.dy;
62  s.v += a.dv;
63  if (s.v > maxSpeed)
64  s.v = maxSpeed;
65  if (s.v < 0)
66  s.v = 0;
67 
68  s.heading += a.dh;
69  if (s.heading > 360)
70  s.heading-=360;
71  if (s.heading < 0)
72  s.heading+=360;
73 
74  if (s.x > worldRadius)
75  s.x -= worldRadius*2;
76  if (s.x < -worldRadius)
77  s.x += worldRadius*2;
78  if (s.y > worldRadius)
79  s.y -= worldRadius*2;
80  if (s.y < -worldRadius)
81  s.y += worldRadius*2;
82 
83 }
84 
86 {
87  s2 = s1;
88  ApplyAction(s2, a);
89 }
90 
92 { return false; }
93 
95 double SteeringEnvironment::HCost(const steeringState &node1, const steeringState &node2) const
96 { if (node1 == node2) return 0; return 1; }
97 
99 { return 1; }
101 { return 1; }
103 { return node == goal; }
104 
105 
107 { assert(false); return 3; }
108 
110 { assert(false); return 3; }
111 
112 
114 {
115  glLineWidth(10);
116  glColor4f(0.0, 0.0, 1.0, 1.0);
117  glBegin(GL_LINE_LOOP);
118  glVertex3f(-1.0, -1.0, 0);
119  glVertex3f(+1.0, -1.0, 0);
120  glVertex3f(+1.0, +1.0, 0);
121  glVertex3f(-1.0, +1.0, 0);
122  glEnd();
123  glLineWidth(1);
124 }
125 
127 {
128  GLdouble xx, yy, zz, rad;
129  GLfloat r, g, b, t;
130  GetColor(r, g, b, t);
131  xx = s.x/(worldRadius);
132  yy = s.y/(worldRadius);
133  zz = 0;
134  rad = 2.0f/worldRadius;
135  //map->GetOpenGLCoord(l.x, l.y, xx, yy, zz, rad);
136 
137  GLdouble yoffset = sin(TWOPI*s.heading/360.0)*rad;//sin(TWOPI*rot/16)*rad;
138  GLdouble xoffset = cos(TWOPI*s.heading/360.0)*rad;//cos(TWOPI*rot/16)*rad;
139 
140  // glColor3f(0, 0, 1.0);
141  // glBegin(GL_LINE_STRIP);
142  // glVertex3f(xx-rad, yy-rad, zz-rad);
143  // glVertex3f(xx-rad, yy-rad, zz);
144  // glEnd();
145 
146  glBegin(GL_TRIANGLES);
147  recVec surfaceNormal;
148  surfaceNormal.x = (((-0.5*xoffset) * (-rad)) - ((+rad) - (-2*yoffset)));
149  surfaceNormal.y = (((rad) * (-2*xoffset)) - ((0.5*yoffset) - (rad)));
150  surfaceNormal.z = (((0.5*yoffset) * (-2*yoffset)) - ((-0.5*xoffset) - (-2*xoffset)));
151  surfaceNormal.normalise();
152  glNormal3f(surfaceNormal.x, surfaceNormal.y, surfaceNormal.z);
153  glColor4f(r, g, b/2, t);
154  glVertex3f(xx+xoffset, yy+yoffset, zz);
155  glColor4f(r, g/2, b, t);
156  glVertex3f(xx-xoffset, yy-yoffset, zz-rad);
157  glColor4f(r, g, b/2, t);
158  glVertex3f(xx-xoffset+0.5*yoffset, yy-yoffset-0.5*xoffset, zz);
159 
160  surfaceNormal.x = (((+0.5*xoffset) * (-rad)) - ((+rad) - (-2*yoffset)));
161  surfaceNormal.y = (((rad) * (-2*xoffset)) - ((-0.5*yoffset) - (rad)));
162  surfaceNormal.z = (((-0.5*yoffset) * (-2*yoffset)) - ((+0.5*xoffset) - (-2*xoffset)));
163  surfaceNormal.normalise();
164  glNormal3f(surfaceNormal.x, surfaceNormal.y, surfaceNormal.z);
165  glColor4f(r, g/2, b, t);
166  glVertex3f(xx+xoffset, yy+yoffset, zz);
167  glColor4f(r, g, b/2, t);
168  glVertex3f(xx-xoffset, yy-yoffset, zz-rad);
169  glColor4f(r, g/2, b, t);
170  glVertex3f(xx-xoffset-0.5*yoffset, yy-yoffset+0.5*xoffset, zz);
171  glEnd();
172 }
173 
174 void SteeringEnvironment::OpenGLDraw(const steeringState&s1, const steeringState&s2, float mix) const
175 {
176  GLdouble xx, yy, zz, rad;
177  GLfloat r, g, b, t;
178  GetColor(r, g, b, t);
179  xx = (s1.x+s2.x)/(2*worldRadius);
180  yy = (s1.y+s2.y)/(2*worldRadius);
181  zz = 0;
182  rad = 2.0f/worldRadius;
183  //map->GetOpenGLCoord(l.x, l.y, xx, yy, zz, rad);
184 
185  GLdouble yoffset = sin(TWOPI*(s1.heading+s2.heading)/(2*360.0))*rad;//sin(TWOPI*rot/16)*rad;
186  GLdouble xoffset = cos(TWOPI*(s1.heading+s2.heading)/(2*360.0))*rad;//cos(TWOPI*rot/16)*rad;
187 
188  // glColor3f(0, 0, 1.0);
189  // glBegin(GL_LINE_STRIP);
190  // glVertex3f(xx-rad, yy-rad, zz-rad);
191  // glVertex3f(xx-rad, yy-rad, zz);
192  // glEnd();
193 
194  glBegin(GL_TRIANGLES);
195  recVec surfaceNormal;
196  surfaceNormal.x = (((-0.5*xoffset) * (-rad)) - ((+rad) - (-2*yoffset)));
197  surfaceNormal.y = (((rad) * (-2*xoffset)) - ((0.5*yoffset) - (rad)));
198  surfaceNormal.z = (((0.5*yoffset) * (-2*yoffset)) - ((-0.5*xoffset) - (-2*xoffset)));
199  surfaceNormal.normalise();
200  glNormal3f(surfaceNormal.x, surfaceNormal.y, surfaceNormal.z);
201  glColor4f(r, g, b/2, t);
202  glVertex3f(xx+xoffset, yy+yoffset, zz);
203  glColor4f(r, g/2, b, t);
204  glVertex3f(xx-xoffset, yy-yoffset, zz-rad);
205  glColor4f(r, g, b/2, t);
206  glVertex3f(xx-xoffset+0.5*yoffset, yy-yoffset-0.5*xoffset, zz);
207 
208  surfaceNormal.x = (((+0.5*xoffset) * (-rad)) - ((+rad) - (-2*yoffset)));
209  surfaceNormal.y = (((rad) * (-2*xoffset)) - ((-0.5*yoffset) - (rad)));
210  surfaceNormal.z = (((-0.5*yoffset) * (-2*yoffset)) - ((+0.5*xoffset) - (-2*xoffset)));
211  surfaceNormal.normalise();
212  glNormal3f(surfaceNormal.x, surfaceNormal.y, surfaceNormal.z);
213  glColor4f(r, g/2, b, t);
214  glVertex3f(xx+xoffset, yy+yoffset, zz);
215  glColor4f(r, g, b/2, t);
216  glVertex3f(xx-xoffset, yy-yoffset, zz-rad);
217  glColor4f(r, g/2, b, t);
218  glVertex3f(xx-xoffset-0.5*yoffset, yy-yoffset+0.5*xoffset, zz);
219  glEnd();
220 }
SteeringEnvironment::OpenGLDraw
virtual void OpenGLDraw() const
Definition: SteeringEnvironment.cpp:113
SteeringEnvironment::GCost
virtual double GCost(const steeringState &node1, const steeringState &node2) const
Definition: SteeringEnvironment.cpp:98
steeringAction
Definition: SteeringEnvironment.h:39
recVec
A generic vector (essentially the same as a point, but offers normalization)
Definition: GLUtil.h:78
recVec::z
GLdouble z
Definition: GLUtil.h:98
steeringState
Definition: SteeringEnvironment.h:17
SteeringEnvironment::GetAction
virtual steeringAction GetAction(const steeringState &s1, const steeringState &s2) const
Definition: SteeringEnvironment.cpp:41
SteeringEnvironment::GetActions
virtual void GetActions(const steeringState &nodeID, std::vector< steeringAction > &actions) const
Definition: SteeringEnvironment.cpp:27
SteeringEnvironment::GetNextState
virtual void GetNextState(const steeringState &, steeringAction, steeringState &) const
Definition: SteeringEnvironment.cpp:85
SteeringEnvironment::InvertAction
virtual bool InvertAction(steeringAction &a) const
Definition: SteeringEnvironment.cpp:91
steeringState::v
float v
Definition: SteeringEnvironment.h:20
SteeringEnvironment.h
maxSpeed
float maxSpeed
Definition: SteeringEnvironment.cpp:12
steeringState::heading
float heading
Definition: SteeringEnvironment.h:21
SteeringEnvironment::GetSuccessors
virtual void GetSuccessors(const steeringState &nodeID, std::vector< steeringState > &neighbors) const
Definition: SteeringEnvironment.cpp:15
steeringAction::dh
float dh
Definition: SteeringEnvironment.h:42
steeringState::y
float y
Definition: SteeringEnvironment.h:19
SteeringEnvironment::GetActionHash
virtual uint64_t GetActionHash(steeringAction act) const
Definition: SteeringEnvironment.cpp:109
SearchEnvironment< steeringState, steeringAction >::GetColor
virtual rgbColor GetColor() const
Definition: SearchEnvironment.h:105
SteeringEnvironment::GoalTest
virtual bool GoalTest(const steeringState &node, const steeringState &goal) const
Definition: SteeringEnvironment.cpp:102
SteeringEnvironment::HCost
virtual double HCost(const steeringState &node1, const steeringState &node2) const
Heuristic value between two arbitrary nodes.
Definition: SteeringEnvironment.cpp:95
worldRadius
float worldRadius
Definition: SteeringEnvironment.cpp:13
steeringState::x
float x
Definition: SteeringEnvironment.h:19
TWOPI
static const double TWOPI
Definition: GLUtil.h:65
SteeringEnvironment::ApplyAction
virtual void ApplyAction(steeringState &s, steeringAction a) const
Definition: SteeringEnvironment.cpp:47
recVec::y
GLdouble y
Definition: GLUtil.h:98
recVec::x
GLdouble x
Definition: GLUtil.h:98
recVec::normalise
void normalise()
Normalize a vector.
Definition: GLUtil.cpp:39
SteeringEnvironment::GetStateHash
virtual uint64_t GetStateHash(const steeringState &node) const
Definition: SteeringEnvironment.cpp:106
steeringAction::dv
float dv
Definition: SteeringEnvironment.h:42
node
Nodes to be stored within a Graph.
Definition: Graph.h:170