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