| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved. |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* Further, this software is distributed without any warranty that it is |
| 16 |
* free of the rightful claim of any third person regarding infringement |
| 17 |
* or the like. Any license provided herein, whether implied or |
| 18 |
* otherwise, applies only to this software file. Patent licenses, if |
| 19 |
* any, provided herein do not apply to combinations of this program with |
| 20 |
* other software, or any other product whatsoever. |
| 21 |
* |
| 22 |
* You should have received a copy of the GNU Lesser General Public |
| 23 |
* License along with this library; if not, write to the Free Software |
| 24 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 |
* |
| 26 |
* Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 27 |
* Mountain View, CA 94043, or: |
| 28 |
* |
| 29 |
* http://www.sgi.com |
| 30 |
* |
| 31 |
* For further information regarding this notice, see: |
| 32 |
* |
| 33 |
* http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 34 |
* |
| 35 |
*/ |
| 36 |
|
| 37 |
// |
| 38 |
// maze - this sample program displays a maze with a steel ball that |
| 39 |
// the user maneuvers through to the end. |
| 40 |
// |
| 41 |
// See the README file in this directory for a complete explanation. |
| 42 |
// |
| 43 |
|
| 44 |
#include <cstdlib> |
| 45 |
#include <cstdio> |
| 46 |
#include <X11/Intrinsic.h> |
| 47 |
#include <cmath> |
| 48 |
|
| 49 |
#include <Inventor/SoInput.h> |
| 50 |
#include <Inventor/SoDB.h> |
| 51 |
#include <Inventor/actions/SoWriteAction.h> |
| 52 |
#include <Inventor/events/SoLocation2Event.h> |
| 53 |
#include <Inventor/events/SoMouseButtonEvent.h> |
| 54 |
#include <Inventor/events/SoKeyboardEvent.h> |
| 55 |
#include <Inventor/nodes/SoBaseColor.h> |
| 56 |
#include <Inventor/nodes/SoCallback.h> |
| 57 |
#include <Inventor/nodes/SoCoordinate3.h> |
| 58 |
#include <Inventor/nodes/SoCube.h> |
| 59 |
#include <Inventor/nodes/SoEventCallback.h> |
| 60 |
#include <Inventor/nodes/SoFont.h> |
| 61 |
#include <Inventor/nodes/SoIndexedFaceSet.h> |
| 62 |
#include <Inventor/nodes/SoLightModel.h> |
| 63 |
#include <Inventor/nodes/SoMaterial.h> |
| 64 |
#include <Inventor/nodes/SoMaterialBinding.h> |
| 65 |
#include <Inventor/nodes/SoNormal.h> |
| 66 |
#include <Inventor/nodes/SoNormalBinding.h> |
| 67 |
#include <Inventor/nodes/SoPerspectiveCamera.h> |
| 68 |
#include <Inventor/nodes/SoRotationXYZ.h> |
| 69 |
#include <Inventor/nodes/SoText3.h> |
| 70 |
#include <Inventor/nodes/SoTexture2.h> |
| 71 |
#include <Inventor/nodes/SoTextureCoordinateEnvironment.h> |
| 72 |
#include <Inventor/nodes/SoTranslation.h> |
| 73 |
#include <Inventor/nodes/SoSphere.h> |
| 74 |
#include <Inventor/nodes/SoSeparator.h> |
| 75 |
#include <Inventor/nodes/SoTriangleStripSet.h> |
| 76 |
#include <Inventor/sensors/SoAlarmSensor.h> |
| 77 |
#include <Inventor/sensors/SoTimerSensor.h> |
| 78 |
#include <Inventor/Xt/SoXt.h> |
| 79 |
#include <Inventor/Xt/viewers/SoXtExaminerViewer.h> |
| 80 |
|
| 81 |
#include "../../samples/common/InventorLogo.h" |
| 82 |
|
| 83 |
#include "box.h" |
| 84 |
#include "mazes.h" |
| 85 |
#ifndef NO_AUDIO |
| 86 |
#include "PlayClass.h" |
| 87 |
#endif |
| 88 |
|
| 89 |
#define GRAVITATIONAL_CONSTANT 50.0 |
| 90 |
#define WALL_THICKNESS 0.08 |
| 91 |
#define WALL_HEIGHT 0.6 |
| 92 |
#define BALL_RADIUS 0.32 |
| 93 |
#define HOLE_RADIUS 0.38 |
| 94 |
#define GAME_WIDTH 11.0 |
| 95 |
#define GRID_RESOLUTION 11 |
| 96 |
#define GRID_RES2 5.5 |
| 97 |
#define EDGE_RESOLUTION 12 |
| 98 |
#define WALL_DAMPENING 10.0 |
| 99 |
#define HOLE_SEGMENTS 8 |
| 100 |
#define ROTATION_LIMIT 0.2 |
| 101 |
#define MAX_ELAPSED_TIME 0.06 |
| 102 |
|
| 103 |
// #define NO_HOLES |
| 104 |
|
| 105 |
static float stdDist = WALL_THICKNESS + BALL_RADIUS; |
| 106 |
static int startGridRow = 0; |
| 107 |
static int startGridCol = 7; |
| 108 |
static int currentGridRow = 0; |
| 109 |
static int currentGridCol = 7; |
| 110 |
static SbBool isBallFalling = FALSE; |
| 111 |
|
| 112 |
short mazeRows[12][12]; |
| 113 |
short mazeColumns[12][12]; |
| 114 |
short mazeHoles[11][11]; |
| 115 |
SbBool isMouseDown = FALSE; |
| 116 |
SbVec2f mouseRotation; |
| 117 |
SbVec2s mouseLocation; |
| 118 |
SbVec2s startLocation, finishLocation; |
| 119 |
SoRotationXYZ *rotationX, *rotationZ; |
| 120 |
SoTranslation *startTrans, *finishTrans; |
| 121 |
SoTranslation *ballTranslation; |
| 122 |
SbTime animationTime; |
| 123 |
SbVec2f ballPosition; |
| 124 |
SbVec2f lastBallPosition; |
| 125 |
SbVec2f ballVelocity; |
| 126 |
SbVec2f ballAcceleration; |
| 127 |
float ballHeight = 0.0; |
| 128 |
float dropVelocity = 0.0; |
| 129 |
SbVec2f oneVector; |
| 130 |
|
| 131 |
#ifndef NO_AUDIO |
| 132 |
PlayClass *victory; |
| 133 |
PlayClass *ballSound; |
| 134 |
|
| 135 |
// Audio files have moved between IRIX 5.3 and IRIX 6.2 |
| 136 |
#if defined(LIBAUDIOFILE_VERSION) && LIBAUDIOFILE_VERSION == 2 |
| 137 |
static char *myVictorySound = IVPREFIX "/share/data/sounds/prosonus/musictags/tag3.aiff"; |
| 138 |
static char *myBallSound = IVPREFIX "/share/data/sounds/prosonus/sfx/glass_break.aiff"; |
| 139 |
#else |
| 140 |
static char *myVictorySound = "/usr/lib/sounds/prosonus/musictags/tag3.aiff"; |
| 141 |
static char *myballSound = "/usr/lib/sounds/prosonus/sfx/glass_break.aiff"; |
| 142 |
#endif |
| 143 |
|
| 144 |
#endif |
| 145 |
|
| 146 |
SbBool doneGame = FALSE; |
| 147 |
SoCoordinate3 *mazeCoords; |
| 148 |
SoIndexedFaceSet *mazeFaces; |
| 149 |
SoCoordinate3 *holeCoords; |
| 150 |
SoTriangleStripSet *holeStrips; |
| 151 |
SoTimerSensor *timer; |
| 152 |
SoAlarmSensor *resetTimer; |
| 153 |
|
| 154 |
|
| 155 |
//////////////////////////////////////////////////////////////////////// |
| 156 |
// |
| 157 |
// Description: |
| 158 |
// Generates a wall of the maze given start and endpoints on the floor |
| 159 |
// of the maze. |
| 160 |
// |
| 161 |
// Use: private |
| 162 |
|
| 163 |
static void |
| 164 |
readMazeFile( |
| 165 |
const char *filename) |
| 166 |
|
| 167 |
// |
| 168 |
//////////////////////////////////////////////////////////////////////// |
| 169 |
{ |
| 170 |
// Read the maze file and fill up the row and column arrays. |
| 171 |
FILE *fp; |
| 172 |
int i, j; |
| 173 |
|
| 174 |
if ((fp = fopen(filename, "r")) == NULL) { |
| 175 |
fprintf(stderr, "ERROR: Could not initialize maze %s.\n", filename); |
| 176 |
exit(-1); |
| 177 |
} |
| 178 |
|
| 179 |
for (i=0; i<EDGE_RESOLUTION; i++) |
| 180 |
fscanf(fp, "%hd %hd %hd %hd %hd %hd %hd %hd %hd %hd %hd %hd", |
| 181 |
&mazeRows[i][0], &mazeRows[i][1], &mazeRows[i][2], &mazeRows[i][3], |
| 182 |
&mazeRows[i][4], &mazeRows[i][5], &mazeRows[i][6], &mazeRows[i][7], |
| 183 |
&mazeRows[i][8], &mazeRows[i][9], &mazeRows[i][10], &mazeRows[i][11]); |
| 184 |
|
| 185 |
for (i=0; i<EDGE_RESOLUTION; i++) |
| 186 |
fscanf(fp, "%hd %hd %hd %hd %hd %hd %hd %hd %hd %hd %hd %hd", |
| 187 |
&mazeColumns[i][0], &mazeColumns[i][1], &mazeColumns[i][2], &mazeColumns[i][3], |
| 188 |
&mazeColumns[i][4], &mazeColumns[i][5], &mazeColumns[i][6], &mazeColumns[i][7], |
| 189 |
&mazeColumns[i][8], &mazeColumns[i][9], &mazeColumns[i][10], &mazeColumns[i][11]); |
| 190 |
|
| 191 |
for (i=0; i<GRID_RESOLUTION; i++) |
| 192 |
fscanf(fp, "%hd %hd %hd %hd %hd %hd %hd %hd %hd %hd %hd", |
| 193 |
&mazeHoles[i][0], &mazeHoles[i][1], &mazeHoles[i][2], &mazeHoles[i][3], |
| 194 |
&mazeHoles[i][4], &mazeHoles[i][5], &mazeHoles[i][6], &mazeHoles[i][7], |
| 195 |
&mazeHoles[i][8], &mazeHoles[i][9], &mazeHoles[i][10]); |
| 196 |
|
| 197 |
// Find the starting and ending locations |
| 198 |
for (i=0; i<GRID_RESOLUTION; i++) { |
| 199 |
for (j=0; j<GRID_RESOLUTION; j++) { |
| 200 |
if (mazeHoles[i][j] == 2) { |
| 201 |
|
| 202 |
// This is the starting location. Store the grid location |
| 203 |
// away and replace the 2 with a 0 to put floor there. |
| 204 |
startGridRow = i; |
| 205 |
startGridCol = j; |
| 206 |
mazeHoles[i][j] = 0; |
| 207 |
} else if (mazeHoles[i][j] == 3) { |
| 208 |
|
| 209 |
// This is the finishing location. Store the grid location |
| 210 |
// away and replace the 3 with a 0 to put floor there. |
| 211 |
finishLocation[0] = i; |
| 212 |
finishLocation[1] = j; |
| 213 |
mazeHoles[i][j] = 0; |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
currentGridRow = startGridRow; |
| 219 |
currentGridCol = startGridCol; |
| 220 |
fclose(fp); |
| 221 |
} |
| 222 |
|
| 223 |
//////////////////////////////////////////////////////////////////////// |
| 224 |
// |
| 225 |
// Description: |
| 226 |
// Generates a wall of the maze given start and endpoints on the floor |
| 227 |
// of the maze. |
| 228 |
// |
| 229 |
// Use: private |
| 230 |
|
| 231 |
static void |
| 232 |
setStandardMaze( |
| 233 |
int mazeNum ) |
| 234 |
|
| 235 |
// |
| 236 |
//////////////////////////////////////////////////////////////////////// |
| 237 |
{ |
| 238 |
int i, j; |
| 239 |
short *r, *c, *h; |
| 240 |
|
| 241 |
// Select the correct maze to build |
| 242 |
switch (mazeNum) { |
| 243 |
case 1: |
| 244 |
r = maze1Rows[0]; |
| 245 |
c = maze1Columns[0]; |
| 246 |
h = maze1Holes[0]; |
| 247 |
break; |
| 248 |
case 2: |
| 249 |
r = maze2Rows[0]; |
| 250 |
c = maze2Columns[0]; |
| 251 |
h = maze2Holes[0]; |
| 252 |
break; |
| 253 |
case 3: |
| 254 |
r = maze3Rows[0]; |
| 255 |
c = maze3Columns[0]; |
| 256 |
h = maze3Holes[0]; |
| 257 |
break; |
| 258 |
} |
| 259 |
|
| 260 |
// Load the maze into the row, column, and hole arrays |
| 261 |
for (i=0; i<EDGE_RESOLUTION; i++) |
| 262 |
for (j=0; j<EDGE_RESOLUTION; j++) |
| 263 |
mazeRows[i][j] = *r++; |
| 264 |
|
| 265 |
for (i=0; i<EDGE_RESOLUTION; i++) |
| 266 |
for (j=0; j<EDGE_RESOLUTION; j++) |
| 267 |
mazeColumns[i][j] = *c++; |
| 268 |
|
| 269 |
for (i=0; i<GRID_RESOLUTION; i++) { |
| 270 |
for (j=0; j<GRID_RESOLUTION; j++, h++) { |
| 271 |
if (*h == 2) { |
| 272 |
|
| 273 |
// This is the starting location. Store the grid location |
| 274 |
// away and replace the 2 with a 0 to put floor there. |
| 275 |
startGridRow = i; |
| 276 |
startGridCol = j; |
| 277 |
mazeHoles[i][j] = 0; |
| 278 |
} else if (*h == 3) { |
| 279 |
|
| 280 |
// This is the finishing location. Store the grid location |
| 281 |
// away and replace the 3 with a 0 to put floor there. |
| 282 |
finishLocation[0] = i; |
| 283 |
finishLocation[1] = j; |
| 284 |
mazeHoles[i][j] = 0; |
| 285 |
} else { |
| 286 |
mazeHoles[i][j] = *h; |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
currentGridRow = startGridRow; |
| 292 |
currentGridCol = startGridCol; |
| 293 |
} |
| 294 |
|
| 295 |
//////////////////////////////////////////////////////////////////////// |
| 296 |
// |
| 297 |
// Description: |
| 298 |
// Generates a wall of the maze given start and endpoints on the floor |
| 299 |
// of the maze. |
| 300 |
// |
| 301 |
// Use: private |
| 302 |
|
| 303 |
static void |
| 304 |
generateWall( |
| 305 |
const SbVec2f &startCoord, |
| 306 |
const SbVec2f &endCoord, |
| 307 |
SoCoordinate3 *coords, |
| 308 |
SoIndexedFaceSet *fset ) |
| 309 |
|
| 310 |
// |
| 311 |
//////////////////////////////////////////////////////////////////////// |
| 312 |
{ |
| 313 |
// Generate coordinates and add them to the coordinate node |
| 314 |
SbVec3f c; |
| 315 |
SbVec2f &start = (SbVec2f &)startCoord, &end = (SbVec2f &)endCoord; |
| 316 |
float xwidth, zwidth; |
| 317 |
int coordStartNum = coords->point.getNum(); |
| 318 |
int faceStartNum = fset->coordIndex.getNum(); |
| 319 |
int normNum = fset->normalIndex.getNum(); |
| 320 |
int cNum = coordStartNum; |
| 321 |
int fNum = faceStartNum; |
| 322 |
int cIndex; |
| 323 |
int normShift; |
| 324 |
|
| 325 |
if (startCoord[1] == endCoord[1]) { |
| 326 |
|
| 327 |
// Wall is horizontal |
| 328 |
if (startCoord[0] > endCoord[0]) { |
| 329 |
start = (SbVec2f &)endCoord; |
| 330 |
end = (SbVec2f &)startCoord; |
| 331 |
} |
| 332 |
xwidth = WALL_THICKNESS; |
| 333 |
zwidth = 0.0; |
| 334 |
normShift = 0; |
| 335 |
} |
| 336 |
else { |
| 337 |
|
| 338 |
// Wall is vertical |
| 339 |
if (startCoord[1] > endCoord[1]) { |
| 340 |
start = (SbVec2f &)endCoord; |
| 341 |
end = (SbVec2f &)startCoord; |
| 342 |
} |
| 343 |
xwidth = 0.0; |
| 344 |
zwidth = WALL_THICKNESS; |
| 345 |
normShift = -1; |
| 346 |
} |
| 347 |
|
| 348 |
// Generate the Coordinates |
| 349 |
c.setValue(start[0] - xwidth - zwidth, |
| 350 |
0.0, |
| 351 |
start[1] + xwidth -zwidth); |
| 352 |
coords->point.set1Value(cNum++, c); |
| 353 |
c.setValue(end[0] + xwidth - zwidth, |
| 354 |
0.0, |
| 355 |
end[1] + xwidth + zwidth); |
| 356 |
coords->point.set1Value(cNum++, c); |
| 357 |
c.setValue(end[0] + xwidth + zwidth, |
| 358 |
0.0, |
| 359 |
end[1] - xwidth + zwidth); |
| 360 |
coords->point.set1Value(cNum++, c); |
| 361 |
c.setValue(start[0] - xwidth + zwidth, |
| 362 |
0.0, |
| 363 |
start[1] - xwidth - zwidth); |
| 364 |
coords->point.set1Value(cNum++, c); |
| 365 |
c.setValue(start[0] - xwidth - zwidth, |
| 366 |
WALL_HEIGHT, |
| 367 |
start[1] + xwidth - zwidth); |
| 368 |
coords->point.set1Value(cNum++, c); |
| 369 |
c.setValue(end[0] + xwidth - zwidth, |
| 370 |
WALL_HEIGHT, |
| 371 |
end[1] + xwidth + zwidth); |
| 372 |
coords->point.set1Value(cNum++, c); |
| 373 |
c.setValue(end[0] + xwidth + zwidth, |
| 374 |
WALL_HEIGHT, |
| 375 |
end[1] - xwidth + zwidth); |
| 376 |
coords->point.set1Value(cNum++, c); |
| 377 |
c.setValue(start[0] - xwidth + zwidth, |
| 378 |
WALL_HEIGHT, |
| 379 |
start[1] - xwidth - zwidth); |
| 380 |
coords->point.set1Value(cNum++, c); |
| 381 |
|
| 382 |
// Generate the faces |
| 383 |
cIndex = coordStartNum + 3; |
| 384 |
fset->coordIndex.set1Value(fNum++, cIndex); |
| 385 |
cIndex = coordStartNum + 2; |
| 386 |
fset->coordIndex.set1Value(fNum++, cIndex); |
| 387 |
cIndex = coordStartNum + 1; |
| 388 |
fset->coordIndex.set1Value(fNum++, cIndex); |
| 389 |
cIndex = coordStartNum + 0; |
| 390 |
fset->coordIndex.set1Value(fNum++, cIndex); |
| 391 |
fset->coordIndex.set1Value(fNum++, SO_END_FACE_INDEX); |
| 392 |
fset->normalIndex.set1Value(normNum++, 0); |
| 393 |
|
| 394 |
cIndex = coordStartNum + 4; |
| 395 |
fset->coordIndex.set1Value(fNum++, cIndex); |
| 396 |
cIndex = coordStartNum + 5; |
| 397 |
fset->coordIndex.set1Value(fNum++, cIndex); |
| 398 |
cIndex = coordStartNum + 6; |
| 399 |
fset->coordIndex.set1Value(fNum++, cIndex); |
| 400 |
cIndex = coordStartNum + 7; |
| 401 |
fset->coordIndex.set1Value(fNum++, cIndex); |
| 402 |
fset->coordIndex.set1Value(fNum++, SO_END_FACE_INDEX); |
| 403 |
fset->normalIndex.set1Value(normNum++, 1); |
| 404 |
|
| 405 |
cIndex = coordStartNum + 0; |
| 406 |
fset->coordIndex.set1Value(fNum++, cIndex); |
| 407 |
cIndex = coordStartNum + 1; |
| 408 |
fset->coordIndex.set1Value(fNum++, cIndex); |
| 409 |
cIndex = coordStartNum + 5; |
| 410 |
fset->coordIndex.set1Value(fNum++, cIndex); |
| 411 |
cIndex = coordStartNum + 4; |
| 412 |
fset->coordIndex.set1Value(fNum++, cIndex); |
| 413 |
fset->coordIndex.set1Value(fNum++, SO_END_FACE_INDEX); |
| 414 |
fset->normalIndex.set1Value(normNum++, ((2 + normShift) == 1) ? 5 : 2); |
| 415 |
|
| 416 |
cIndex = coordStartNum + 1; |
| 417 |
fset->coordIndex.set1Value(fNum++, cIndex); |
| 418 |
cIndex = coordStartNum + 2; |
| 419 |
fset->coordIndex.set1Value(fNum++, cIndex); |
| 420 |
cIndex = coordStartNum + 6; |
| 421 |
fset->coordIndex.set1Value(fNum++, cIndex); |
| 422 |
cIndex = coordStartNum + 5; |
| 423 |
fset->coordIndex.set1Value(fNum++, cIndex); |
| 424 |
fset->coordIndex.set1Value(fNum++, SO_END_FACE_INDEX); |
| 425 |
fset->normalIndex.set1Value(normNum++, 3 + normShift); |
| 426 |
|
| 427 |
cIndex = coordStartNum + 2; |
| 428 |
fset->coordIndex.set1Value(fNum++, cIndex); |
| 429 |
cIndex = coordStartNum + 3; |
| 430 |
fset->coordIndex.set1Value(fNum++, cIndex); |
| 431 |
cIndex = coordStartNum + 7; |
| 432 |
fset->coordIndex.set1Value(fNum++, cIndex); |
| 433 |
cIndex = coordStartNum + 6; |
| 434 |
fset->coordIndex.set1Value(fNum++, cIndex); |
| 435 |
fset->coordIndex.set1Value(fNum++, SO_END_FACE_INDEX); |
| 436 |
fset->normalIndex.set1Value(normNum++, 4 + normShift); |
| 437 |
|
| 438 |
cIndex = coordStartNum + 3; |
| 439 |
fset->coordIndex.set1Value(fNum++, cIndex); |
| 440 |
cIndex = coordStartNum + 0; |
| 441 |
fset->coordIndex.set1Value(fNum++, cIndex); |
| 442 |
cIndex = coordStartNum + 4; |
| 443 |
fset->coordIndex.set1Value(fNum++, cIndex); |
| 444 |
cIndex = coordStartNum + 7; |
| 445 |
fset->coordIndex.set1Value(fNum++, cIndex); |
| 446 |
fset->coordIndex.set1Value(fNum++, SO_END_FACE_INDEX); |
| 447 |
fset->normalIndex.set1Value(normNum++, 5 + normShift); |
| 448 |
} |
| 449 |
|
| 450 |
//////////////////////////////////////////////////////////////////////// |
| 451 |
// |
| 452 |
// Description: |
| 453 |
// Generates a hole in the maze given location on the floor |
| 454 |
// of the maze. |
| 455 |
// |
| 456 |
// Use: private |
| 457 |
|
| 458 |
static void |
| 459 |
generateHole( |
| 460 |
const SbVec2f &location, |
| 461 |
SoCoordinate3 *coords, |
| 462 |
SoTriangleStripSet *sset ) |
| 463 |
|
| 464 |
// |
| 465 |
//////////////////////////////////////////////////////////////////////// |
| 466 |
{ |
| 467 |
SbVec3f pt(location[0], 0.04, location[1] + HOLE_RADIUS); |
| 468 |
float incr = M_PI / HOLE_SEGMENTS; |
| 469 |
int cNum = coords->point.getNum(); |
| 470 |
|
| 471 |
coords->point.set1Value(cNum++, pt); |
| 472 |
for (int i=1; i<HOLE_SEGMENTS; i++) { |
| 473 |
pt[0] = location[0] + sinf(i*incr) * HOLE_RADIUS; |
| 474 |
pt[2] = location[1] + cosf(i*incr) * HOLE_RADIUS; |
| 475 |
coords->point.set1Value(cNum++, pt); |
| 476 |
pt[0] = location[0] - sinf(i*incr) * HOLE_RADIUS; |
| 477 |
coords->point.set1Value(cNum++, pt); |
| 478 |
} |
| 479 |
pt[0] = location[0] + 0.0; |
| 480 |
pt[2] = location[1] - HOLE_RADIUS; |
| 481 |
coords->point.set1Value(cNum++, pt); |
| 482 |
sset->numVertices.set1Value(sset->numVertices.getNum(), |
| 483 |
(HOLE_SEGMENTS-1)*2+2); |
| 484 |
} |
| 485 |
|
| 486 |
//////////////////////////////////////////////////////////////////////// |
| 487 |
// |
| 488 |
// Description: |
| 489 |
// Builds the maze by generating horizontal and vertical walls |
| 490 |
// from the maze row and column arrays. Generate holes from |
| 491 |
// the array of hole positions. |
| 492 |
// |
| 493 |
// Use: private |
| 494 |
|
| 495 |
static void |
| 496 |
buildMaze( |
| 497 |
SoCoordinate3 *fcoords, |
| 498 |
SoCoordinate3 *scoords, |
| 499 |
SoIndexedFaceSet *fset, |
| 500 |
SoTriangleStripSet *sset ) |
| 501 |
|
| 502 |
// |
| 503 |
//////////////////////////////////////////////////////////////////////// |
| 504 |
{ |
| 505 |
int i, j, k; |
| 506 |
|
| 507 |
// Loop through the rows and columns arrays looking for contiguous |
| 508 |
// stretches of ON bits. Form walls from the bits. |
| 509 |
SbVec2f startCoord, endCoord; |
| 510 |
|
| 511 |
for (i=0; i<EDGE_RESOLUTION; i++) { |
| 512 |
for (j=0; j<EDGE_RESOLUTION; j++) { |
| 513 |
if (mazeRows[i][j]) { |
| 514 |
startCoord.setValue(-GRID_RES2+(float)j, -GRID_RES2+(float)i); |
| 515 |
for (k=j+1; k<EDGE_RESOLUTION; k++) { |
| 516 |
if (mazeRows[i][k] == 0) |
| 517 |
break; |
| 518 |
} |
| 519 |
if (k == EDGE_RESOLUTION) |
| 520 |
k--; |
| 521 |
endCoord.setValue(-GRID_RES2+(float)k, -GRID_RES2+(float)i); |
| 522 |
generateWall(startCoord, endCoord, fcoords, fset); |
| 523 |
j = k; |
| 524 |
} |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
for (i=0; i<EDGE_RESOLUTION; i++) { |
| 529 |
for (j=0; j<EDGE_RESOLUTION; j++) { |
| 530 |
if (mazeColumns[j][i]) { |
| 531 |
startCoord.setValue(-GRID_RES2+(float)i, -GRID_RES2+(float)j); |
| 532 |
for (k=j+1; k<EDGE_RESOLUTION; k++) { |
| 533 |
if (!mazeColumns[k][i]) |
| 534 |
break; |
| 535 |
} |
| 536 |
if (k == EDGE_RESOLUTION) |
| 537 |
k--; |
| 538 |
endCoord.setValue(-GRID_RES2+(float)i, -GRID_RES2+(float)k); |
| 539 |
j = k; |
| 540 |
generateWall(startCoord, endCoord, fcoords, fset); |
| 541 |
} |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
#ifdef NO_HOLES |
| 546 |
// Loop through the array of holes, building holes for the maze |
| 547 |
for (i=0; i<GRID_RESOLUTION; i++) |
| 548 |
for (j=0; j<GRID_RESOLUTION; j++) |
| 549 |
mazeHoles[i][j] = 0; |
| 550 |
#else |
| 551 |
// Loop through the array of holes, building holes for the maze |
| 552 |
SbVec3f st, ft; |
| 553 |
for (i=0; i<GRID_RESOLUTION; i++) { |
| 554 |
for (j=0; j<GRID_RESOLUTION; j++) { |
| 555 |
if (mazeHoles[i][j] == 1) { |
| 556 |
startCoord.setValue( -GRID_RES2 + (float)j + 0.5, |
| 557 |
-GRID_RES2 + (float)i + 0.5 ); |
| 558 |
generateHole( startCoord, scoords, sset ); |
| 559 |
} |
| 560 |
else if (mazeHoles[i][j] == 2) { |
| 561 |
st.setValue(-GRID_RES2 + j + 0.5, -GRID_RES2 + i + 0.5, 0.04 ); |
| 562 |
} |
| 563 |
else if (mazeHoles[i][j] == 3) { |
| 564 |
ft.setValue(-GRID_RES2 + j + 0.5, -GRID_RES2 + i + 0.5, 0.04 ); |
| 565 |
} |
| 566 |
} |
| 567 |
} |
| 568 |
startTrans->translation.setValue(st); |
| 569 |
finishTrans->translation.setValue(ft-st); |
| 570 |
#endif |
| 571 |
} |
| 572 |
|
| 573 |
//////////////////////////////////////////////////////////////////////// |
| 574 |
// |
| 575 |
// Description: |
| 576 |
// Callback routine for receiving mouse button press to tilt the floor. |
| 577 |
// Schedule the timer sensor on mouse down. |
| 578 |
// |
| 579 |
// Use: private |
| 580 |
|
| 581 |
static void |
| 582 |
saveMouseLocation( void *, SoEventCallback *cb ) |
| 583 |
|
| 584 |
// |
| 585 |
//////////////////////////////////////////////////////////////////////// |
| 586 |
{ |
| 587 |
if (SO_MOUSE_PRESS_EVENT(cb->getEvent(), BUTTON1)) { |
| 588 |
|
| 589 |
const SbVec2s &pos = cb->getEvent()->getPosition(); |
| 590 |
mouseLocation.setValue(pos[0], pos[1]); |
| 591 |
|
| 592 |
if (!timer->isScheduled()) { |
| 593 |
animationTime.setValue( SbTime::getTimeOfDay().getValue() ); |
| 594 |
timer->schedule(); |
| 595 |
} |
| 596 |
|
| 597 |
isMouseDown = TRUE; |
| 598 |
cb->setHandled(); |
| 599 |
} |
| 600 |
else if (SO_MOUSE_RELEASE_EVENT(cb->getEvent(), BUTTON1)) { |
| 601 |
|
| 602 |
isMouseDown = FALSE; |
| 603 |
cb->setHandled(); |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
//////////////////////////////////////////////////////////////////////// |
| 608 |
// |
| 609 |
// Description: |
| 610 |
// Callback routine for receiving mouse motion to tilt the floor. |
| 611 |
// |
| 612 |
// Use: private |
| 613 |
|
| 614 |
static void |
| 615 |
computeFloorTilt( void *, SoEventCallback *cb ) |
| 616 |
|
| 617 |
// |
| 618 |
//////////////////////////////////////////////////////////////////////// |
| 619 |
{ |
| 620 |
if (!isMouseDown) |
| 621 |
return; |
| 622 |
|
| 623 |
const SbVec2s &pos = cb->getEvent()->getPosition(); |
| 624 |
SbVec2s mouseMotion; |
| 625 |
short x = mouseLocation[0]; |
| 626 |
short y = mouseLocation[1]; |
| 627 |
|
| 628 |
mouseMotion[0] = x - pos[0]; |
| 629 |
mouseMotion[1] = y - pos[1]; |
| 630 |
mouseLocation.setValue((short)pos[0], (short)pos[1]); |
| 631 |
|
| 632 |
// Compute X and Z rotation angles depending on the mouse motion |
| 633 |
mouseRotation[0] += (float)(mouseMotion[1]) / 1000.0; |
| 634 |
mouseRotation[1] += (float)(mouseMotion[0]) / 1000.0; |
| 635 |
|
| 636 |
// Keep the rotation within bounds |
| 637 |
if (mouseRotation[0] > ROTATION_LIMIT) |
| 638 |
mouseRotation[0] = ROTATION_LIMIT; |
| 639 |
else if (mouseRotation[0] < -ROTATION_LIMIT) |
| 640 |
mouseRotation[0] = -ROTATION_LIMIT; |
| 641 |
if (mouseRotation[1] > ROTATION_LIMIT) |
| 642 |
mouseRotation[1] = ROTATION_LIMIT; |
| 643 |
else if (mouseRotation[1] < -ROTATION_LIMIT) |
| 644 |
mouseRotation[1] = -ROTATION_LIMIT; |
| 645 |
|
| 646 |
rotationX->angle = mouseRotation[0]; |
| 647 |
rotationZ->angle = mouseRotation[1]; |
| 648 |
cb->setHandled(); |
| 649 |
} |
| 650 |
|
| 651 |
//////////////////////////////////////////////////////////////////////// |
| 652 |
// |
| 653 |
// Description: |
| 654 |
// Resets the game parameters to their initial values to start |
| 655 |
// a new game. |
| 656 |
// |
| 657 |
// Use: private |
| 658 |
|
| 659 |
static void |
| 660 |
resetGame() |
| 661 |
|
| 662 |
// |
| 663 |
//////////////////////////////////////////////////////////////////////// |
| 664 |
{ |
| 665 |
mouseLocation.setValue(0, 0); |
| 666 |
mouseRotation.setValue(0.0, 0.0); |
| 667 |
ballVelocity.setValue(0.0, 0.0); |
| 668 |
ballAcceleration.setValue(0.0, 0.0); |
| 669 |
ballPosition[0] = startGridCol + stdDist; |
| 670 |
ballPosition[1] = startGridRow + stdDist; |
| 671 |
lastBallPosition[0] = ballPosition[0]; |
| 672 |
lastBallPosition[1] = ballPosition[1]; |
| 673 |
ballHeight = 0.0; |
| 674 |
currentGridCol = startGridCol; |
| 675 |
currentGridRow = startGridRow; |
| 676 |
ballTranslation->translation.setValue( |
| 677 |
ballPosition[0] - GRID_RES2, BALL_RADIUS, |
| 678 |
ballPosition[1] - GRID_RES2); |
| 679 |
rotationX->angle = mouseRotation[0]; |
| 680 |
rotationZ->angle = mouseRotation[1]; |
| 681 |
isBallFalling = FALSE; |
| 682 |
doneGame = FALSE; |
| 683 |
} |
| 684 |
|
| 685 |
//////////////////////////////////////////////////////////////////////// |
| 686 |
// |
| 687 |
// Description: |
| 688 |
// Callback routine for receiving keyboard events. |
| 689 |
// |
| 690 |
// Use: private |
| 691 |
|
| 692 |
static void |
| 693 |
processKeyEvents( void *, SoEventCallback *cb ) |
| 694 |
|
| 695 |
// |
| 696 |
//////////////////////////////////////////////////////////////////////// |
| 697 |
{ |
| 698 |
if (SO_KEY_PRESS_EVENT(cb->getEvent(), R)) { |
| 699 |
|
| 700 |
resetGame(); |
| 701 |
cb->setHandled(); |
| 702 |
return; |
| 703 |
} |
| 704 |
if (SO_KEY_PRESS_EVENT(cb->getEvent(), NUMBER_1)) { |
| 705 |
|
| 706 |
// Reset the game with standard maze 1 |
| 707 |
mazeCoords->point.deleteValues(0); |
| 708 |
mazeFaces->coordIndex.deleteValues(0); |
| 709 |
mazeFaces->normalIndex.deleteValues(0); |
| 710 |
holeCoords->point.deleteValues(0); |
| 711 |
holeStrips->numVertices.deleteValues(0); |
| 712 |
setStandardMaze(1); |
| 713 |
buildMaze( mazeCoords, holeCoords, mazeFaces, holeStrips ); |
| 714 |
|
| 715 |
resetGame(); |
| 716 |
cb->setHandled(); |
| 717 |
return; |
| 718 |
} |
| 719 |
if (SO_KEY_PRESS_EVENT(cb->getEvent(), NUMBER_2)) { |
| 720 |
|
| 721 |
// Reset the game with standard maze 2 |
| 722 |
mazeCoords->point.deleteValues(0); |
| 723 |
mazeFaces->coordIndex.deleteValues(0); |
| 724 |
mazeFaces->normalIndex.deleteValues(0); |
| 725 |
holeCoords->point.deleteValues(0); |
| 726 |
holeStrips->numVertices.deleteValues(0); |
| 727 |
setStandardMaze(2); |
| 728 |
buildMaze( mazeCoords, holeCoords, mazeFaces, holeStrips ); |
| 729 |
|
| 730 |
resetGame(); |
| 731 |
cb->setHandled(); |
| 732 |
return; |
| 733 |
} |
| 734 |
if (SO_KEY_PRESS_EVENT(cb->getEvent(), NUMBER_3)) { |
| 735 |
|
| 736 |
// Reset the game with standard maze 3 |
| 737 |
mazeCoords->point.deleteValues(0); |
| 738 |
mazeFaces->coordIndex.deleteValues(0); |
| 739 |
mazeFaces->normalIndex.deleteValues(0); |
| 740 |
holeCoords->point.deleteValues(0); |
| 741 |
holeStrips->numVertices.deleteValues(0); |
| 742 |
setStandardMaze(3); |
| 743 |
buildMaze( mazeCoords, holeCoords, mazeFaces, holeStrips ); |
| 744 |
|
| 745 |
resetGame(); |
| 746 |
cb->setHandled(); |
| 747 |
return; |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
//////////////////////////////////////////////////////////////////////// |
| 752 |
// |
| 753 |
// Description: |
| 754 |
// Drop the ball into the hole. |
| 755 |
// |
| 756 |
// Use: private |
| 757 |
|
| 758 |
void |
| 759 |
dropBall( float et ) |
| 760 |
|
| 761 |
// |
| 762 |
//////////////////////////////////////////////////////////////////////// |
| 763 |
{ |
| 764 |
// When the ball has dropped far enough we don't need to drop the |
| 765 |
// ball any further. We can also unschedule the timer sensor. |
| 766 |
if (ballHeight <= -2.0) { |
| 767 |
if (!isMouseDown) |
| 768 |
timer->unschedule(); |
| 769 |
|
| 770 |
// Schedule a timer to wait for a bit then reset the game back to |
| 771 |
// its initial position |
| 772 |
resetTimer->setTimeFromNow(SbTime(2.0)); |
| 773 |
resetTimer->schedule(); |
| 774 |
return; |
| 775 |
} |
| 776 |
|
| 777 |
// Calculate a new position for the ball |
| 778 |
SbVec2f newPosition; |
| 779 |
|
| 780 |
newPosition[0] = et * (ballVelocity[0] + |
| 781 |
et * GRAVITATIONAL_CONSTANT * sinf(-rotationZ->angle.getValue())); |
| 782 |
newPosition[1] = et * (ballVelocity[1] + |
| 783 |
et * GRAVITATIONAL_CONSTANT * sinf(rotationX->angle.getValue())); |
| 784 |
ballHeight += et * (dropVelocity - et * GRAVITATIONAL_CONSTANT); |
| 785 |
dropVelocity = ballHeight / et; |
| 786 |
ballPosition += newPosition; |
| 787 |
ballVelocity[0] = newPosition[0] / et; |
| 788 |
ballVelocity[1] = newPosition[1] / et; |
| 789 |
|
| 790 |
if (ballPosition[0] < (GRID_RES2-5.75+BALL_RADIUS)) |
| 791 |
ballPosition[0] = GRID_RES2-5.75+BALL_RADIUS; |
| 792 |
if (ballPosition[0] > (GRID_RES2+5.75-BALL_RADIUS)) |
| 793 |
ballPosition[0] = GRID_RES2+5.75-BALL_RADIUS; |
| 794 |
if (ballPosition[1] < (GRID_RES2-5.75+BALL_RADIUS)) |
| 795 |
ballPosition[1] = GRID_RES2-5.75+BALL_RADIUS; |
| 796 |
if (ballPosition[1] > (GRID_RES2+5.75-BALL_RADIUS)) |
| 797 |
ballPosition[1] = GRID_RES2+5.75-BALL_RADIUS; |
| 798 |
|
| 799 |
ballTranslation->translation.setValue( |
| 800 |
ballPosition[0] - GRID_RES2, |
| 801 |
BALL_RADIUS + ballHeight, |
| 802 |
ballPosition[1] - GRID_RES2); |
| 803 |
|
| 804 |
#ifndef NO_AUDIO |
| 805 |
// play the drop sound |
| 806 |
if (ballHeight <=-2.5) { |
| 807 |
sginap(20); |
| 808 |
ballSound->start(); |
| 809 |
} |
| 810 |
#endif |
| 811 |
} |
| 812 |
|
| 813 |
//////////////////////////////////////////////////////////////////////// |
| 814 |
// |
| 815 |
// Description: |
| 816 |
// Callback routine for resetting the game back to its initial state |
| 817 |
// after the ball has fallen into a hole. |
| 818 |
// |
| 819 |
// Use: private |
| 820 |
|
| 821 |
static void |
| 822 |
resetCB( void *, SoSensor * ) |
| 823 |
|
| 824 |
// |
| 825 |
//////////////////////////////////////////////////////////////////////// |
| 826 |
{ |
| 827 |
resetGame(); |
| 828 |
resetTimer->unschedule(); |
| 829 |
} |
| 830 |
|
| 831 |
//////////////////////////////////////////////////////////////////////// |
| 832 |
// |
| 833 |
// Description: |
| 834 |
// Callback routine for animating the ball through a timer sensor. |
| 835 |
// |
| 836 |
// Use: private |
| 837 |
|
| 838 |
static void |
| 839 |
animateBall( void *, SoSensor * ) |
| 840 |
|
| 841 |
// |
| 842 |
//////////////////////////////////////////////////////////////////////// |
| 843 |
{ |
| 844 |
// Calculate the elapsed time since the last timer firing |
| 845 |
SbTime newTime; |
| 846 |
SbTime elapsedTime; |
| 847 |
|
| 848 |
newTime.setValue( SbTime::getTimeOfDay().getValue() ); |
| 849 |
elapsedTime.setValue( (newTime - animationTime).getValue() ); |
| 850 |
animationTime.setValue( (newTime).getValue() ); |
| 851 |
float et = elapsedTime.getValue(); |
| 852 |
|
| 853 |
// Check to see if elapsed time is too long due to a slow machine |
| 854 |
if (et > MAX_ELAPSED_TIME) |
| 855 |
et = MAX_ELAPSED_TIME; |
| 856 |
|
| 857 |
// If the ball is falling, perform its animation |
| 858 |
if (isBallFalling) { |
| 859 |
dropBall(et); |
| 860 |
return; |
| 861 |
} |
| 862 |
|
| 863 |
// Calculate a new position for the ball |
| 864 |
SbVec2f newPosition, tmpPosition; |
| 865 |
|
| 866 |
newPosition[0] = et * (ballVelocity[0] + |
| 867 |
et * GRAVITATIONAL_CONSTANT * sinf(-rotationZ->angle.getValue())); |
| 868 |
newPosition[1] = et * (ballVelocity[1] + |
| 869 |
et * GRAVITATIONAL_CONSTANT * sinf(rotationX->angle.getValue())); |
| 870 |
|
| 871 |
ballVelocity[0] = newPosition[0] / et; |
| 872 |
ballVelocity[1] = newPosition[1] / et; |
| 873 |
|
| 874 |
tmpPosition[0] = 1.0 + ballPosition[0] + newPosition[0]; |
| 875 |
tmpPosition[1] = 1.0 + ballPosition[1] + newPosition[1]; |
| 876 |
|
| 877 |
// Check the edges of the current grid cell to see if there are any |
| 878 |
// walls present. For each present wall, test the ball for intersection |
| 879 |
// and modify the ball position accordingly. |
| 880 |
|
| 881 |
int row, col; |
| 882 |
|
| 883 |
row = currentGridRow; |
| 884 |
col = currentGridCol; |
| 885 |
|
| 886 |
// Check for intersections against left and right grid boundaries |
| 887 |
if (((currentGridCol+1) != (int)(tmpPosition[0]-stdDist)) && |
| 888 |
(ballVelocity[0] < 0.0)) { |
| 889 |
if (mazeColumns[row][col]) { |
| 890 |
tmpPosition[0] = 1.0 + col + stdDist; |
| 891 |
ballVelocity[0] /= -WALL_DAMPENING; |
| 892 |
} |
| 893 |
} else if (((currentGridCol+1) != |
| 894 |
(int)(tmpPosition[0]+stdDist)) && |
| 895 |
(ballVelocity[0] > 0.0)) { |
| 896 |
if (mazeColumns[row][col+1]) { |
| 897 |
tmpPosition[0] = 1.0 + col + 1 - stdDist; |
| 898 |
ballVelocity[0] /= -WALL_DAMPENING; |
| 899 |
} |
| 900 |
} |
| 901 |
|
| 902 |
// Check for intersections against top and bottom grid boundaries |
| 903 |
if (((currentGridRow+1) != (int)(tmpPosition[1]-stdDist)) && |
| 904 |
(ballVelocity[1] < 0.0)) { |
| 905 |
if (mazeRows[row][col]) { |
| 906 |
tmpPosition[1] = 1.0 + row + stdDist; |
| 907 |
ballVelocity[1] /= -WALL_DAMPENING; |
| 908 |
} |
| 909 |
} else if (((currentGridRow+1) != |
| 910 |
(int)(tmpPosition[1]+stdDist)) && |
| 911 |
(ballVelocity[1] > 0.0)) { |
| 912 |
if (mazeRows[row+1][col]) { |
| 913 |
tmpPosition[1] = 1.0 + row + 1 - stdDist; |
| 914 |
ballVelocity[1] /= -WALL_DAMPENING; |
| 915 |
} |
| 916 |
} |
| 917 |
|
| 918 |
// Check for intersection against ends of other walls |
| 919 |
SbVec2f cornerPoint; |
| 920 |
SbVec2f cornerVector; |
| 921 |
|
| 922 |
// Check the top right corner of the grid cell for corners |
| 923 |
if ((mazeRows[currentGridRow][currentGridCol+1] || |
| 924 |
mazeColumns[currentGridRow-1][currentGridCol+1]) && |
| 925 |
!mazeRows[currentGridRow][currentGridCol] && |
| 926 |
!mazeColumns[currentGridRow][currentGridCol+1]) { |
| 927 |
|
| 928 |
// Check the corner. |
| 929 |
SbVec2f center(currentGridCol+0.5, currentGridRow+0.5); |
| 930 |
cornerPoint[0] = currentGridCol + 1.0 - WALL_THICKNESS; |
| 931 |
cornerPoint[1] = currentGridRow + WALL_THICKNESS; |
| 932 |
cornerVector = tmpPosition - oneVector - cornerPoint; |
| 933 |
|
| 934 |
// Check to see if the intersection is on the edge |
| 935 |
// of the wall rather than the corner |
| 936 |
if ((cornerVector[1] <= 0.0) && |
| 937 |
(cornerVector[0] >= -BALL_RADIUS)) { |
| 938 |
tmpPosition[0] = 1.0 + currentGridCol + 1 - stdDist; |
| 939 |
ballVelocity[0] /= -WALL_DAMPENING; |
| 940 |
} |
| 941 |
else if ((cornerVector[0] >= 0.0) && (cornerVector[1] <= BALL_RADIUS)){ |
| 942 |
tmpPosition[1] = 1.0 + currentGridRow + stdDist; |
| 943 |
ballVelocity[1] /= -WALL_DAMPENING; |
| 944 |
} |
| 945 |
else { |
| 946 |
float distFromCorner = fabs(cornerVector.normalize()); |
| 947 |
float reflectionMagnitude; |
| 948 |
if (distFromCorner <= BALL_RADIUS) { |
| 949 |
reflectionMagnitude = -1.1 * cornerVector.dot(ballVelocity); |
| 950 |
ballVelocity += reflectionMagnitude * cornerVector; |
| 951 |
tmpPosition = oneVector+cornerPoint + BALL_RADIUS*cornerVector; |
| 952 |
} |
| 953 |
} |
| 954 |
} |
| 955 |
|
| 956 |
// Check the top left corner of the grid cell for corners |
| 957 |
if ((mazeRows[currentGridRow][currentGridCol-1] || |
| 958 |
mazeColumns[currentGridRow-1][currentGridCol]) && |
| 959 |
!mazeRows[currentGridRow][currentGridCol] && |
| 960 |
!mazeColumns[currentGridRow][currentGridCol]) { |
| 961 |
|
| 962 |
// Check the corner. |
| 963 |
SbVec2f center(currentGridCol+0.5, currentGridRow+0.5); |
| 964 |
cornerPoint[0] = currentGridCol + WALL_THICKNESS; |
| 965 |
cornerPoint[1] = currentGridRow + WALL_THICKNESS; |
| 966 |
cornerVector = tmpPosition - oneVector - cornerPoint; |
| 967 |
|
| 968 |
// Check to see if the intersection is on the edge |
| 969 |
// of the wall rather than the corner |
| 970 |
if ((cornerVector[1] <= 0.0) && |
| 971 |
(cornerVector[0] <= BALL_RADIUS)) { |
| 972 |
tmpPosition[0] = 1.0 + currentGridCol + stdDist; |
| 973 |
ballVelocity[0] /= -WALL_DAMPENING; |
| 974 |
} |
| 975 |
else if ((cornerVector[0] <= 0.0) && (cornerVector[1] <= BALL_RADIUS)){ |
| 976 |
tmpPosition[1] = 1.0 + currentGridRow + stdDist; |
| 977 |
ballVelocity[1] /= -WALL_DAMPENING; |
| 978 |
} |
| 979 |
else { |
| 980 |
float distFromCorner = fabs(cornerVector.normalize()); |
| 981 |
float reflectionMagnitude; |
| 982 |
if (distFromCorner <= BALL_RADIUS) { |
| 983 |
reflectionMagnitude = -1.1 * cornerVector.dot(ballVelocity); |
| 984 |
ballVelocity += reflectionMagnitude * cornerVector; |
| 985 |
tmpPosition = oneVector+cornerPoint + BALL_RADIUS*cornerVector; |
| 986 |
} |
| 987 |
} |
| 988 |
} |
| 989 |
|
| 990 |
// Check the bottom right corner of the grid cell for corners |
| 991 |
if ((mazeRows[currentGridRow+1][currentGridCol+1] || |
| 992 |
mazeColumns[currentGridRow+1][currentGridCol+1]) && |
| 993 |
!mazeRows[currentGridRow+1][currentGridCol] && |
| 994 |
!mazeColumns[currentGridRow][currentGridCol+1]) { |
| 995 |
|
| 996 |
// Check the corner. |
| 997 |
SbVec2f center(currentGridCol+0.5, currentGridRow+0.5); |
| 998 |
cornerPoint[0] = currentGridCol + 1.0 - WALL_THICKNESS; |
| 999 |
cornerPoint[1] = currentGridRow + 1.0 - WALL_THICKNESS; |
| 1000 |
cornerVector = tmpPosition - oneVector - cornerPoint; |
| 1001 |
|
| 1002 |
// Check to see if the intersection is on the edge |
| 1003 |
// of the wall rather than the corner |
| 1004 |
if ((cornerVector[1] >= 0.0) && |
| 1005 |
(cornerVector[0] >= -BALL_RADIUS)) { |
| 1006 |
tmpPosition[0] = 1.0 + currentGridCol + 1 - stdDist; |
| 1007 |
ballVelocity[0] /= -WALL_DAMPENING; |
| 1008 |
} |
| 1009 |
else if ((cornerVector[0] >= 0.0) && (cornerVector[1] >= -BALL_RADIUS)){ |
| 1010 |
tmpPosition[1] = 1.0 + currentGridRow + 1 - stdDist; |
| 1011 |
ballVelocity[1] /= -WALL_DAMPENING; |
| 1012 |
} |
| 1013 |
else { |
| 1014 |
float distFromCorner = fabs(cornerVector.normalize()); |
| 1015 |
float reflectionMagnitude; |
| 1016 |
if (distFromCorner <= BALL_RADIUS) { |
| 1017 |
reflectionMagnitude = -1.1 * cornerVector.dot(ballVelocity); |
| 1018 |
ballVelocity += reflectionMagnitude * cornerVector; |
| 1019 |
tmpPosition = oneVector+cornerPoint + BALL_RADIUS*cornerVector; |
| 1020 |
} |
| 1021 |
} |
| 1022 |
} |
| 1023 |
|
| 1024 |
// Check the bottom left corner of the grid cell for corners |
| 1025 |
if ((mazeRows[currentGridRow+1][currentGridCol-1] || |
| 1026 |
mazeColumns[currentGridRow+1][currentGridCol]) && |
| 1027 |
!mazeRows[currentGridRow+1][currentGridCol] && |
| 1028 |
!mazeColumns[currentGridRow][currentGridCol]) { |
| 1029 |
|
| 1030 |
// Check the corner. |
| 1031 |
SbVec2f center(currentGridCol+0.5, currentGridRow+0.5); |
| 1032 |
cornerPoint[0] = currentGridCol + WALL_THICKNESS; |
| 1033 |
cornerPoint[1] = currentGridRow + 1.0 - WALL_THICKNESS; |
| 1034 |
cornerVector = tmpPosition - oneVector - cornerPoint; |
| 1035 |
|
| 1036 |
// Check to see if the intersection is on the edge |
| 1037 |
// of the wall rather than the corner |
| 1038 |
if ((cornerVector[1] >= 0.0) && |
| 1039 |
(cornerVector[0] <= BALL_RADIUS)) { |
| 1040 |
tmpPosition[0] = 1.0 + currentGridCol + stdDist; |
| 1041 |
ballVelocity[0] /= -WALL_DAMPENING; |
| 1042 |
} |
| 1043 |
else if ((cornerVector[0] <= 0.0) && (cornerVector[1] >= -BALL_RADIUS)){ |
| 1044 |
tmpPosition[1] = 1.0 + currentGridRow + 1 - stdDist; |
| 1045 |
ballVelocity[1] /= -WALL_DAMPENING; |
| 1046 |
} |
| 1047 |
else { |
| 1048 |
float distFromCorner = fabs(cornerVector.normalize()); |
| 1049 |
float reflectionMagnitude; |
| 1050 |
if (distFromCorner <= BALL_RADIUS) { |
| 1051 |
reflectionMagnitude = -1.1 * cornerVector.dot(ballVelocity); |
| 1052 |
ballVelocity += reflectionMagnitude * cornerVector; |
| 1053 |
tmpPosition = oneVector+cornerPoint + BALL_RADIUS*cornerVector; |
| 1054 |
} |
| 1055 |
} |
| 1056 |
} |
| 1057 |
|
| 1058 |
ballPosition[0] = tmpPosition[0] - 1.0; |
| 1059 |
ballPosition[1] = tmpPosition[1] - 1.0; |
| 1060 |
|
| 1061 |
// Check to see if the ball has moved since last time. |
| 1062 |
// If it's still in the same place and the mouse button is up |
| 1063 |
// then we can unschedule the timer sensor. |
| 1064 |
if (ballPosition == lastBallPosition && !isMouseDown) { |
| 1065 |
timer->unschedule(); |
| 1066 |
} |
| 1067 |
|
| 1068 |
currentGridRow = (int)(ballPosition[1]); |
| 1069 |
currentGridCol = (int)(ballPosition[0]); |
| 1070 |
|
| 1071 |
if (!doneGame) |
| 1072 |
if ((currentGridRow == finishLocation[1]) && |
| 1073 |
(currentGridCol == finishLocation[0])) { |
| 1074 |
|
| 1075 |
// Play the Victory Audio |
| 1076 |
#ifndef NO_AUDIO |
| 1077 |
victory->start(); |
| 1078 |
#endif |
| 1079 |
doneGame = TRUE; |
| 1080 |
} |
| 1081 |
|
| 1082 |
// Check to see if the ball has run into a hole |
| 1083 |
if (mazeHoles[currentGridRow][currentGridCol] != 0) { |
| 1084 |
|
| 1085 |
SbVec2f center(currentGridCol+0.5, |
| 1086 |
currentGridRow+0.5); |
| 1087 |
SbVec2f fallVector = center - ballPosition; |
| 1088 |
float distFromHoleCenter = fabs(fallVector.length()); |
| 1089 |
if (distFromHoleCenter <= HOLE_RADIUS) { |
| 1090 |
isBallFalling = TRUE; |
| 1091 |
|
| 1092 |
// Adjust the ball velocity to have it start dropping |
| 1093 |
SbVec2f changeVelocity = (fallVector - ballVelocity); |
| 1094 |
ballHeight = - sinf(HOLE_RADIUS-distFromHoleCenter); |
| 1095 |
dropVelocity = ballHeight / et; |
| 1096 |
ballVelocity += changeVelocity * 0.5; |
| 1097 |
} |
| 1098 |
} |
| 1099 |
|
| 1100 |
ballTranslation->translation.setValue( |
| 1101 |
ballPosition[0] - GRID_RES2, |
| 1102 |
.35 + ballHeight, |
| 1103 |
ballPosition[1] - GRID_RES2); |
| 1104 |
|
| 1105 |
lastBallPosition[0] = ballPosition[0]; |
| 1106 |
lastBallPosition[1] = ballPosition[1]; |
| 1107 |
} |
| 1108 |
|
| 1109 |
|
| 1110 |
//////////////////////////////////////////////////////////////////////// |
| 1111 |
// |
| 1112 |
// Set the Inventor logo on the screen. |
| 1113 |
// |
| 1114 |
//////////////////////////////////////////////////////////////////////// |
| 1115 |
|
| 1116 |
static void |
| 1117 |
logoCB(void *, SoAction *action) |
| 1118 |
{ |
| 1119 |
if (action->isOfType(SoGLRenderAction::getClassTypeId())) { |
| 1120 |
glViewport(0, 0, 80, 80); |
| 1121 |
} |
| 1122 |
} |
| 1123 |
|
| 1124 |
static void |
| 1125 |
setOverlayLogo(SoXtRenderArea *ra) |
| 1126 |
{ |
| 1127 |
static SoSeparator *logo = NULL; |
| 1128 |
|
| 1129 |
if (logo == NULL) { |
| 1130 |
SoInput in; |
| 1131 |
in.setBuffer((void *)ivLogo, ivLogoSize); |
| 1132 |
logo = SoDB::readAll(&in); |
| 1133 |
logo->ref(); |
| 1134 |
|
| 1135 |
// Add a callback node which will set the viewport |
| 1136 |
SoCallback *cb = new SoCallback; |
| 1137 |
cb->setCallback(logoCB); |
| 1138 |
logo->insertChild(cb, 0); |
| 1139 |
} |
| 1140 |
|
| 1141 |
SbColor col(1, 1, 1); |
| 1142 |
ra->setOverlayColorMap(1, 1, &col); |
| 1143 |
ra->setOverlaySceneGraph(logo); |
| 1144 |
} |
| 1145 |
|
| 1146 |
int |
| 1147 |
main(int argc, char *argv[]) |
| 1148 |
{ |
| 1149 |
Widget mainWindow; |
| 1150 |
|
| 1151 |
// Initialize Inventor |
| 1152 |
mainWindow = SoXt::init("Inventor Maze"); |
| 1153 |
SoDB::setDelaySensorTimeout(SbTime(0.0)); |
| 1154 |
|
| 1155 |
#ifndef NO_AUDIO |
| 1156 |
// Initialize the audio for the victory sound |
| 1157 |
victory = new PlayClass("Victory"); |
| 1158 |
victory->setFilename(myVictorySound); |
| 1159 |
ballSound = new PlayClass("BallSound"); |
| 1160 |
ballSound->setFilename(myBallSound); |
| 1161 |
#endif |
| 1162 |
|
| 1163 |
oneVector.setValue(1.0, 1.0); |
| 1164 |
|
| 1165 |
// Setup the scene graph for the rolling ball |
| 1166 |
SoSeparator *ballRoot = new SoSeparator; |
| 1167 |
SoSeparator *ballCache = new SoSeparator; |
| 1168 |
SoMaterial *ballMaterial = new SoMaterial; |
| 1169 |
SoSphere *ball = new SoSphere; |
| 1170 |
|
| 1171 |
ballTranslation = new SoTranslation; |
| 1172 |
ball->radius = BALL_RADIUS; |
| 1173 |
ballMaterial->shininess = .8; |
| 1174 |
ballMaterial->ambientColor.setValue(SbColor(.4, .4, .4)); |
| 1175 |
ballMaterial->diffuseColor.setValue(SbColor(0.4, 0.4, 0.4)); |
| 1176 |
ballMaterial->specularColor.setValue(SbColor(1, 1, 1)); |
| 1177 |
ballCache->renderCaching = SoSeparator::ON; |
| 1178 |
ballCache->addChild(ballMaterial); |
| 1179 |
ballCache->addChild(ball); |
| 1180 |
ballRoot->addChild(ballTranslation); |
| 1181 |
ballRoot->addChild(ballCache); |
| 1182 |
|
| 1183 |
// Setup a timer sensor to animate the ball |
| 1184 |
timer = new SoTimerSensor(animateBall, NULL); |
| 1185 |
timer->setInterval(SbTime(0.03)); |
| 1186 |
resetTimer = new SoAlarmSensor(resetCB, NULL); |
| 1187 |
|
| 1188 |
// Generate a scene graph representing the maze specified by the array |
| 1189 |
SoSeparator *mazeRoot = new SoSeparator; |
| 1190 |
SoSeparator *mazeCache = new SoSeparator; |
| 1191 |
SoSeparator *holeCache = new SoSeparator; |
| 1192 |
SoMaterial *floorMaterial = new SoMaterial; |
| 1193 |
SoCube *floor = new SoCube; |
| 1194 |
SoMaterial *mazeMaterial = new SoMaterial; |
| 1195 |
SoMaterialBinding *mazeMtlBind = new SoMaterialBinding; |
| 1196 |
SoNormal *mazeNorms = new SoNormal; |
| 1197 |
SoNormalBinding *mazeNormBind = new SoNormalBinding; |
| 1198 |
SoLightModel *holeModel = new SoLightModel; |
| 1199 |
SoBaseColor *holeColor = new SoBaseColor; |
| 1200 |
SoFont *textFont = new SoFont; |
| 1201 |
SoText3 *startText = new SoText3; |
| 1202 |
SoText3 *finishText = new SoText3; |
| 1203 |
SoRotationXYZ *textRotation = new SoRotationXYZ; |
| 1204 |
SoBaseColor *textColor = new SoBaseColor; |
| 1205 |
|
| 1206 |
mazeCoords = new SoCoordinate3; |
| 1207 |
mazeFaces = new SoIndexedFaceSet; |
| 1208 |
holeCoords = new SoCoordinate3; |
| 1209 |
holeStrips = new SoTriangleStripSet; |
| 1210 |
startTrans = new SoTranslation; |
| 1211 |
finishTrans = new SoTranslation; |
| 1212 |
finishLocation.setValue(10, 10); |
| 1213 |
|
| 1214 |
// Create walls for the boundary of the maze |
| 1215 |
mazeNorms->vector.set1Value(0, SbVec3f( 0.0, -1.0, 0.0)); |
| 1216 |
mazeNorms->vector.set1Value(1, SbVec3f( 0.0, 1.0, 0.0)); |
| 1217 |
mazeNorms->vector.set1Value(2, SbVec3f( 0.0, 0.0, 1.0)); |
| 1218 |
mazeNorms->vector.set1Value(3, SbVec3f( 1.0, 0.0, 0.0)); |
| 1219 |
mazeNorms->vector.set1Value(4, SbVec3f( 0.0, 0.0, -1.0)); |
| 1220 |
mazeNorms->vector.set1Value(5, SbVec3f(-1.0, 0.0, 0.0)); |
| 1221 |
mazeMaterial->diffuseColor.setValue(SbColor(.8, .6, .1)); |
| 1222 |
mazeMtlBind->value = SoMaterialBinding::OVERALL; |
| 1223 |
mazeNormBind->value = SoNormalBinding::PER_FACE_INDEXED; |
| 1224 |
mazeCoords->point.deleteValues(0); |
| 1225 |
mazeFaces->coordIndex.deleteValues(0); |
| 1226 |
mazeFaces->normalIndex.deleteValues(0); |
| 1227 |
|
| 1228 |
#ifndef NO_HOLES |
| 1229 |
// Create holes in the maze floor |
| 1230 |
holeModel->model = SoLightModel::BASE_COLOR; |
| 1231 |
holeColor->rgb.setValue(SbColor(0.0, 0.0, 0.0)); |
| 1232 |
holeModel->model = SoLightModel::BASE_COLOR; |
| 1233 |
holeCoords->point.deleteValues(0); |
| 1234 |
holeStrips->numVertices.deleteValues(0); |
| 1235 |
holeCache->addChild(holeModel); |
| 1236 |
holeCache->addChild(holeColor); |
| 1237 |
holeCache->addChild(holeCoords); |
| 1238 |
holeCache->addChild(holeStrips); |
| 1239 |
#endif |
| 1240 |
|
| 1241 |
// Parse the command line to find the maze filename |
| 1242 |
if (argc < 2) |
| 1243 |
setStandardMaze(1); |
| 1244 |
else |
| 1245 |
readMazeFile(argv[1]); |
| 1246 |
|
| 1247 |
// Build the initial maze |
| 1248 |
buildMaze( mazeCoords, holeCoords, mazeFaces, holeStrips ); |
| 1249 |
|
| 1250 |
rotationX = new SoRotationXYZ; |
| 1251 |
rotationZ = new SoRotationXYZ; |
| 1252 |
|
| 1253 |
// Read the geometry for the game box and add it to the maze |
| 1254 |
SoInput in; |
| 1255 |
SoNode *n = NULL; |
| 1256 |
SoGroup *gameBox = NULL; |
| 1257 |
in.setBuffer( (void *)box, 1032 ); |
| 1258 |
SoDB::read(&in, n); |
| 1259 |
gameBox = (SoGroup *) n; |
| 1260 |
|
| 1261 |
floorMaterial->diffuseColor.setValue(SbColor(.95, .8, .1)); |
| 1262 |
floorMaterial->shininess = 0.2; |
| 1263 |
floor->width = GAME_WIDTH; |
| 1264 |
floor->depth = GAME_WIDTH; |
| 1265 |
floor->height = 0.05; |
| 1266 |
rotationX->axis = SoRotationXYZ::X; |
| 1267 |
rotationZ->axis = SoRotationXYZ::Z; |
| 1268 |
holeCache->renderCaching = SoSeparator::ON; |
| 1269 |
mazeCache->renderCaching = SoSeparator::ON; |
| 1270 |
mazeCache->addChild(floorMaterial); |
| 1271 |
mazeCache->addChild(floor); |
| 1272 |
mazeCache->addChild(mazeMaterial); |
| 1273 |
mazeCache->addChild(mazeCoords); |
| 1274 |
mazeCache->addChild(mazeNorms); |
| 1275 |
mazeCache->addChild(mazeMtlBind); |
| 1276 |
mazeCache->addChild(mazeNormBind); |
| 1277 |
mazeCache->addChild(mazeFaces); |
| 1278 |
mazeRoot->addChild(gameBox); |
| 1279 |
mazeRoot->addChild(rotationX); |
| 1280 |
mazeRoot->addChild(rotationZ); |
| 1281 |
mazeRoot->addChild(ballRoot); |
| 1282 |
mazeRoot->addChild(mazeCache); |
| 1283 |
mazeRoot->addChild(holeCache); |
| 1284 |
|
| 1285 |
// Create a viewer and begin the game |
| 1286 |
SoSeparator *mainRoot = new SoSeparator; |
| 1287 |
SoEventCallback *eventCB = new SoEventCallback; |
| 1288 |
SoXtExaminerViewer *vwr = new SoXtExaminerViewer(mainWindow); |
| 1289 |
SoPerspectiveCamera *cam = new SoPerspectiveCamera; |
| 1290 |
|
| 1291 |
// Setup event callbacks |
| 1292 |
eventCB->addEventCallback(SoMouseButtonEvent::getClassTypeId(), |
| 1293 |
saveMouseLocation, (void *)&mouseLocation); |
| 1294 |
eventCB->addEventCallback(SoLocation2Event::getClassTypeId(), |
| 1295 |
computeFloorTilt, (void *)&mouseLocation); |
| 1296 |
eventCB->addEventCallback(SoKeyboardEvent::getClassTypeId(), |
| 1297 |
processKeyEvents, NULL); |
| 1298 |
|
| 1299 |
// Setup the camera so the game is comfortably viewed |
| 1300 |
SbVec3f pos(0.0, 15.0, 9.0); |
| 1301 |
SbVec3f lookto(0.0, -1.7, 0.0); |
| 1302 |
cam->position.setValue(pos); |
| 1303 |
cam->orientation.setValue(SbRotation(SbVec3f(0.0, 0.0, -1.0), |
| 1304 |
lookto-pos)); |
| 1305 |
cam->focalDistance = (lookto-pos).length(); |
| 1306 |
|
| 1307 |
mainRoot->ref(); |
| 1308 |
mainRoot->addChild(eventCB); |
| 1309 |
mainRoot->addChild(cam); |
| 1310 |
mainRoot->addChild(mazeRoot); |
| 1311 |
|
| 1312 |
mouseLocation.setValue(0, 0); |
| 1313 |
mouseRotation.setValue(0.0, 0.0); |
| 1314 |
ballVelocity.setValue(0.0, 0.0); |
| 1315 |
ballAcceleration.setValue(0.0, 0.0); |
| 1316 |
ballPosition[0] = (float)currentGridCol + stdDist; |
| 1317 |
ballPosition[1] = (float)currentGridRow + stdDist; |
| 1318 |
lastBallPosition[0] = ballPosition[0]; |
| 1319 |
lastBallPosition[1] = ballPosition[1]; |
| 1320 |
ballTranslation->translation.setValue( |
| 1321 |
ballPosition[0] - GRID_RES2, .35, ballPosition[1] - GRID_RES2); |
| 1322 |
|
| 1323 |
vwr->setSize(SbVec2s(900, 700)); |
| 1324 |
vwr->setSceneGraph(mainRoot); |
| 1325 |
vwr->setTitle("Inventor Maze"); |
| 1326 |
vwr->setViewing(FALSE); |
| 1327 |
vwr->setDecoration(FALSE); |
| 1328 |
vwr->show(); |
| 1329 |
|
| 1330 |
// Set the overlay graph - Inventor logo |
| 1331 |
setOverlayLogo(vwr); |
| 1332 |
|
| 1333 |
SoXt::show(mainWindow); |
| 1334 |
SoXt::mainLoop(); |
| 1335 |
return 0; |
| 1336 |
} |
| 1337 |
|