-
Notifications
You must be signed in to change notification settings - Fork 1
/
MovableObject.pde
151 lines (117 loc) · 4.4 KB
/
MovableObject.pde
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
/*
* SCHUA: The SCHooling User-interactive Aquarium
* - Created for Paul Fishwick's CAP5805 "Simulation Concepts" course at the
* University of Florida in November 2005
* Copyright (c) 2005 James C. Jones <jcjones AT ufl DOT edu>
*
* 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.
*/
public class MovableObject
{
public Vec3D Position, Velocity, Heading, Side;
public float Mass, AxisRotation, BoundRadius, MaxVelocity, MaxTurnRate, MaxForce;
public SteeringBehaviour Behaviour;
public int Species;
public boolean Carnivorous;
public boolean Live;
public int NumberOfThingsEaten;
public int serialNumber;
protected final float DILATION = 75.0f;
public MovableObject(Vec3D p) {
Position = p;
Velocity = new Vec3D();
Heading = RandomVec(); // avoid fully zero headings which will cause problems.
Heading.NormalizeS();
Side = Heading.Perp();
AxisRotation = 180.0f;
BoundRadius = 1.0f;
Behaviour = new SteeringBehaviour(this);
MaxVelocity = 6.0f;
MaxTurnRate = 25.0f;
MaxForce = 100.0f;
Mass = 1.0;
serialNumber = globalSerialNumber++;
NumberOfThingsEaten = 0;
Species = 0;
Carnivorous = false;
Live = true;
}
public void Draw() {
}
public final void DrawAxes()
{
if (!drawAxes)
return;
final float len = 5000.0f;
gl.glLineWidth (5);
setMaterialDiffuseColor (1.0f, 0.0f, 0.0f, 1.0f);
gl.glBegin(GL.GL_LINES);
gl.glVertex3f(0.0f, 0.0f, 0.0f);
gl.glVertex3f(len, 0.0f, 0.0f);
gl.glEnd();
setMaterialDiffuseColor (0.0f, 1.0f, 0.0f, 1.0f);
gl.glBegin(GL.GL_LINES);
gl.glVertex3f(0.0f, 0.0f, 0.0f);
gl.glVertex3f(0.0f, len, 0.0f);
gl.glEnd();
setMaterialDiffuseColor (0.0f, 0.0f, 1.0f, 1.0f);
gl.glBegin(GL.GL_LINES);
gl.glVertex3f(0.0f, 0.0f, 0.0f);
gl.glVertex3f(0.0f, 0.0f, len);
gl.glEnd();
}
public final String Name() {
return Name + " " + serialNumber + ":" + Species;
}
public String Name = "Generic object";
public final String toString()
{
return Name() + " at pos " + Position + " hdg " + Heading + " vel " + Velocity;
}
protected void enforceWalls() {
float posMag = Position.Magnitude2();
if (posMag < OuterWallRadius*OuterWallRadius)
return;
// We're beyond the walls! Shit! Slam on the brakes and stop us
Velocity.ScaleS(0.025f);
// Now scale back our position so that we end up within the walls again
Position.ScaleS( (OuterWallRadius*OuterWallRadius) / posMag );
debug(0, Name() + " says 'OUCH!'");
}
public void Update(float deltaTime) {
Vec3D SteeringForce = Behaviour.Calculate(deltaTime);
debug(4, this.Name() + " Steering Force: " + SteeringForce);
/* F = m*a -> a = F/m */
Vec3D Acceleration = SteeringForce.DividedBy(Mass);
/* Increase velocity */
Velocity.AddS(Acceleration.Scale(deltaTime));
// Velocity = Behaviour.Velocity();
/* Clip values */
Velocity.ClipS(MaxVelocity);
/* Adjust position */
Position.AddS(Velocity.Scale(deltaTime).Scale(DILATION));
/* Enforce Walls */
enforceWalls();
/* Update heading if the vehicle has a nonzero velocity */
if (Velocity.Magnitude2() > 0.0000001)
{
Heading = Velocity.Normalize();
Side = Heading.Perp();
}
}
}