source: trunk/src/testing/app/atlantis-fbo/swim.c @ 4

Revision 4, 5.5 KB checked in by ajaworski, 13 years ago (diff)

Added modified SAGE sources

Line 
1/**
2 * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
3 * ALL RIGHTS RESERVED
4 * Permission to use, copy, modify, and distribute this software for
5 * any purpose and without fee is hereby granted, provided that the above
6 * copyright notice appear in all copies and that both the copyright notice
7 * and this permission notice appear in supporting documentation, and that
8 * the name of Silicon Graphics, Inc. not be used in advertising
9 * or publicity pertaining to distribution of the software without specific,
10 * written prior permission.
11 *
12 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
13 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
14 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
15 * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
16 * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
17 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
18 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
19 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
20 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
21 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
22 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
23 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
24 *
25 * US Government Users Restricted Rights
26 * Use, duplication, or disclosure by the Government is subject to
27 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
28 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
29 * clause at DFARS 252.227-7013 and/or in similar or successor
30 * clauses in the FAR or the DOD or NASA FAR Supplement.
31 * Unpublished-- rights reserved under the copyright laws of the
32 * United States.  Contractor/manufacturer is Silicon Graphics,
33 * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
34 *
35 * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
36 */
37#include <math.h>
38#include <stdlib.h>
39#if defined(__APPLE__)
40#include <GLUT/glut.h>
41#include <OpenGL/glu.h>
42#else
43#include <GL/glut.h>
44#include <GL/glu.h>
45#endif
46
47#include "atlantis.h"
48
49void
50FishTransform(fishRec * fish)
51{
52        glTranslatef(fish->y, fish->z, -fish->x);
53        glRotatef(-fish->psi, 0.0, 1.0, 0.0);
54    glRotatef(fish->theta, 1.0, 0.0, 0.0);
55    glRotatef(-fish->phi, 0.0, 0.0, 1.0);
56}
57
58void
59WhalePilot(fishRec * fish)
60{
61
62    fish->phi = -20.0;
63    fish->theta = 0.0;
64    fish->psi -= 0.5;
65
66    fish->x += WHALESPEED * fish->v * cos(fish->psi / RAD) * cos(fish->theta / RAD);
67    fish->y += WHALESPEED * fish->v * sin(fish->psi / RAD) * cos(fish->theta / RAD);
68    fish->z += WHALESPEED * fish->v * sin(fish->theta / RAD);
69}
70
71void
72SharkPilot(fishRec * fish)
73{
74    static int sign = 1;
75    float X, Y, Z, tpsi, ttheta, thetal;
76
77    fish->xt = 60000.0;
78    fish->yt = 0.0;
79    fish->zt = 0.0;
80
81    X = fish->xt - fish->x;
82    Y = fish->yt - fish->y;
83    Z = fish->zt - fish->z;
84
85    thetal = fish->theta;
86
87    ttheta = RAD * atan(Z / (sqrt(X * X + Y * Y)));
88
89    if (ttheta > fish->theta + 0.25) {
90        fish->theta += 0.5;
91    } else if (ttheta < fish->theta - 0.25) {
92        fish->theta -= 0.5;
93    }
94    if (fish->theta > 90.0) {
95        fish->theta = 90.0;
96    }
97    if (fish->theta < -90.0) {
98        fish->theta = -90.0;
99    }
100    fish->dtheta = fish->theta - thetal;
101
102    tpsi = RAD * atan2(Y, X);
103
104    fish->attack = 0;
105
106    if (fabs(tpsi - fish->psi) < 10.0) {
107        fish->attack = 1;
108    } else if (fabs(tpsi - fish->psi) < 45.0) {
109        if (fish->psi > tpsi) {
110            fish->psi -= 0.5;
111            if (fish->psi < -180.0) {
112                fish->psi += 360.0;
113            }
114        } else if (fish->psi < tpsi) {
115            fish->psi += 0.5;
116            if (fish->psi > 180.0) {
117                fish->psi -= 360.0;
118            }
119        }
120    } else {
121        if (rand() % 100 > 98) {
122            sign = 1 - sign;
123        }
124        fish->psi += sign;
125        if (fish->psi > 180.0) {
126            fish->psi -= 360.0;
127        }
128        if (fish->psi < -180.0) {
129            fish->psi += 360.0;
130        }
131    }
132
133    if (fish->attack) {
134        if (fish->v < 1.1) {
135            fish->spurt = 1;
136        }
137        if (fish->spurt) {
138            fish->v += 0.2;
139        }
140        if (fish->v > 5.0) {
141            fish->spurt = 0;
142        }
143        if ((fish->v > 1.0) && (!fish->spurt)) {
144            fish->v -= 0.2;
145        }
146    } else {
147        if (!(rand() % 400) && (!fish->spurt)) {
148            fish->spurt = 1;
149        }
150        if (fish->spurt) {
151            fish->v += 0.05;
152        }
153        if (fish->v > 3.0) {
154            fish->spurt = 0;
155        }
156        if ((fish->v > 1.0) && (!fish->spurt)) {
157            fish->v -= 0.05;
158        }
159    }
160
161    fish->x += SHARKSPEED * fish->v * cos(fish->psi / RAD) * cos(fish->theta / RAD);
162    fish->y += SHARKSPEED * fish->v * sin(fish->psi / RAD) * cos(fish->theta / RAD);
163    fish->z += SHARKSPEED * fish->v * sin(fish->theta / RAD);
164}
165
166void
167SharkMiss(int i)
168{
169    int j;
170    float avoid, thetal;
171    float X, Y, Z, R;
172
173    for (j = 0; j < NUM_SHARKS; j++) {
174        if (j != i) {
175            X = sharks[j].x - sharks[i].x;
176            Y = sharks[j].y - sharks[i].y;
177            Z = sharks[j].z - sharks[i].z;
178
179            R = sqrt(X * X + Y * Y + Z * Z);
180
181            avoid = 1.0;
182            thetal = sharks[i].theta;
183
184            if (R < SHARKSIZE) {
185                if (Z > 0.0) {
186                    sharks[i].theta -= avoid;
187                } else {
188                    sharks[i].theta += avoid;
189                }
190            }
191            sharks[i].dtheta += (sharks[i].theta - thetal);
192        }
193    }
194}
Note: See TracBrowser for help on using the repository browser.