/
Elevator.s3d
342 lines (275 loc) · 9.57 KB
/
Elevator.s3d
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
* Class that represents a room, with an optional front wall (so that it can be used for portals' rooms, that don't have the front wall so the camera
* dosn't get obstructed when the room is rendered). It gives more flexibility than simply creating a box, and it's easier to maintain
*/
class Elevator
{
var mElevatorDoorLeftMesh;
var mElevatorDoorRightMesh;
var mElevatorDoorLeftObj;
var mElevatorDoorRightObj;
// The CVmObj that contains the room
var mElevatorRoom;
var mDistanceBetweenClosedDoors;
// Corners of the room (these are local coordinates)
var mLowerCornerPosition;
var mUpperCornerPosition;
var mLowerCornerWorld;
var mUpperCornerWorld;
var mLeftDoorClosedPosition;
var mRightDoorClosedPosition;
var mLeftDoorOpenPosition;
var mRightDoorOpenPosition;
var mIsVisible;
// Public functions
Draw();
IsElevatorOpen();
GetLeftDoor();
GetRightDoor();
GetLeftDoorClosedPosition();
GetRightDoorClosedPosition();
GetLeftDoorOpenPosition();
GetRightDoorOpenPosition();
IsPointInElevator(point);
AddObject(object, x, y, z);
RemoveObject(object);
GetObj();
GetRoomWidth();
GetRoomDepth();
GetRoomHeight();
GetScale();
SetScale(x, y, z);
SetLightsRange(min, max);
GetLightsRange();
LocalToWorld(point);
SetPosition(position);
GetPosition();
Rotate(angle, x, y, z);
Translate(x, y, z);
Hide();
Unhide();
IsVisible();
CheckIntersection(rayOrigin, rayEnd, contactPoint, normal);
};
/*
* Constructor
*
* farWallWidth: (float) width of the front and back wall
* height: (float) height of the room
* hasFront: (boolean) true if there is a front wall, false otherwise
* centerInOrigin: (boolean) if true, the room is moved to the center of the coordinate system
* lightNumber: (integer) the number of the opengl light that has to be used in the cel shader to render the room
*/
function Elevator::Elevator(elevatorWidth, sideWallWidth, height, lightsRangeMin, lightsRangeMax)
{
mElevatorRoom = Room(elevatorWidth, sideWallWidth, height, true, lightsRangeMin, lightsRangeMax, false, true);
var halfHeight = height/2.0;
mElevatorDoorLeftMesh = CVmNewMesh(VRP_PLANE, 1, [-elevatorWidth/4, -halfHeight, 0], [elevatorWidth/4, halfHeight, 0], 2, 2);
mElevatorDoorRightMesh = CVmNewMesh(VRP_PLANE, 1, [-elevatorWidth/4, -halfHeight, 0], [elevatorWidth/4, halfHeight, 0], 2, 2);
// Adding the cel shader to the doors; the material is the same as for every wall
AddToonShaderToMesh(mElevatorDoorLeftMesh, [1, 1, 1], [0.8, 0.8, 0.8], [0.1, 0.1, 0.1], 20, lightsRangeMin, lightsRangeMax, SCALE_FACTOR, 1.0);
AddToonShaderToMesh(mElevatorDoorRightMesh, [1, 1, 1], [0.8, 0.8, 0.8], [0.1, 0.1, 0.1], 20, lightsRangeMin, lightsRangeMax, SCALE_FACTOR, 1.0);
mElevatorDoorLeftObj = CVmObj(mElevatorDoorLeftMesh);
mElevatorDoorRightObj = CVmObj(mElevatorDoorRightMesh);
AddSilhouetteLinesToPlanarObject(mElevatorDoorLeftObj, 0.02 * SCALE_FACTOR, elevatorWidth / 2.0, height);
AddSilhouetteLinesToPlanarObject(mElevatorDoorRightObj, 0.02 * SCALE_FACTOR, elevatorWidth / 2.0, height);
mLeftDoorClosedPosition = [-elevatorWidth/4 - 0 * SCALE_FACTOR, 0, sideWallWidth + 0.005 * SCALE_FACTOR];
mRightDoorClosedPosition = [elevatorWidth/4 - 0 * SCALE_FACTOR, 0, sideWallWidth + 0.005 * SCALE_FACTOR];
mLeftDoorOpenPosition = mLeftDoorClosedPosition - [GetRoomWidth()/3.0 + 0.1 * SCALE_FACTOR, 0, 0];
mRightDoorOpenPosition = mRightDoorClosedPosition + [GetRoomWidth()/3.0 + 0.1 * SCALE_FACTOR, 0, 0];
// Adding the doors to the room
mElevatorRoom.AddObject(mElevatorDoorLeftObj, mLeftDoorClosedPosition.x, mLeftDoorClosedPosition.y, mLeftDoorClosedPosition.z);
mElevatorRoom.AddObject(mElevatorDoorRightObj, mRightDoorClosedPosition.x, mRightDoorClosedPosition.y, mRightDoorClosedPosition.z);
mDistanceBetweenClosedDoors = Modulus(mElevatorDoorLeftObj.GetPosition() - mElevatorDoorRightObj.GetPosition());
mLowerCornerPosition = [-elevatorWidth/2.0, -height/2.0, sideWallWidth];
mUpperCornerPosition = [elevatorWidth/2.0, height/2.0, 0];
// Computing the world coordinates of the bounding box
mLowerCornerWorld = mElevatorRoom.LocalToWorld(mLowerCornerPosition);
mUpperCornerWorld = mElevatorRoom.LocalToWorld(mUpperCornerPosition);
mIsVisible = true;
}
function Elevator::Draw()
{
mElevatorRoom.Draw();
}
function Elevator::IsElevatorOpen()
{
var distanceBetweenDoors = Modulus(mElevatorDoorLeftObj.GetPosition() - mElevatorDoorRightObj.GetPosition());
return distanceBetweenDoors > mDistanceBetweenClosedDoors;
}
function Elevator::GetLeftDoor()
{
return mElevatorDoorLeftObj;
}
function Elevator::GetRightDoor()
{
return mElevatorDoorRightObj;
}
function Elevator::GetLeftDoorClosedPosition()
{
return mLeftDoorClosedPosition;
}
function Elevator::GetRightDoorClosedPosition()
{
return mRightDoorClosedPosition;
}
function Elevator::GetLeftDoorOpenPosition()
{
return mLeftDoorOpenPosition;
}
function Elevator::GetRightDoorOpenPosition()
{
return mRightDoorOpenPosition;
}
/*
* This method checks whether the point passed as parameter is in the room or not
*
* point: (vector) the point, in world coordinates
*/
function Elevator::IsPointInElevator(point)
{
return mElevatorRoom.IsPointInRoom(point);
}
/*
* Method that adds an object fo the room. Mind that the children are only useful for drawing, meaning that the Room object will
* only make sure to draw its children using the silhouette shader plus any shader they might have, nothing more
*/
function Elevator::AddObject(object, x, y, z)
{
mElevatorRoom.AddObject(object, x, y, z);
}
/*
* This method checks where the given ray intersects the world, and returns true if that position is valid for the player
*
* rayOrigin: (vector) starting point of the ray
* rayEnd: (vector) starting point of the ray
* contactPoint: (vector) if the ray intersects something, this parameter will be filled up with the information (needs to be passed as reference, with &)
* normal: (vector) if the ray intersects something, this parameter will be filled up with the normal to the intersected plane (needs to be passed as reference, with &)
*
* The method returns:
* -2 -> if it intercepts the floor, but the door of the elevator are closed so it's not a good position
* -1 -> if no interception was found
* 0 -> if it intercepts the floor
* 1 -> if it intercepts the back wall
* 2 -> if it intercepts the roof
* 3 -> if it intercepts the left wall
* 4 -> if it intercepts the right wall
* 5 -> if it intercepts the elevator's doors
*/
function Elevator::CheckIntersection(rayOrigin, rayEnd, contactPoint, normal)
{
// At first I state that no intersection was found
var intersectedWall = -1;
// If the elevator is not open, I check if the ray intercepts the doors
if(!IsElevatorOpen())
{
// I only consider the left door, since they're both in the same plane; I subtract -1 to make sure that the plane is inside the elevator
var planePoint = mElevatorDoorLeftObj.GetPosition() + GetPosition() + [0, 0, -1 * SCALE_FACTOR];
normal = ComputeNormalOfPlanarObject(mElevatorDoorLeftObj);
contactPoint = FindIntersectionInPlane(rayOrigin, rayEnd, planePoint, normal);
if(contactPoint != Void && mElevatorRoom.IsPointInRoom(contactPoint))
{
contactPoint -= [0, 0, -1 * SCALE_FACTOR];
intersectedWall = 5;
}
}
var contactPointTmp;
var normalTmp;
// I check the rest of the elevator
var intersectedWallTmp = mElevatorRoom.CheckIntersection(rayOrigin, rayEnd, &contactPointTmp, &normalTmp);
// If there was an intersection before and I found one in the rest of the elevator, I need to check which one "wins", i.e. which one
// is closest to the player (in this case the origin of the ray)
if(intersectedWall != -1 && intersectedWallTmp != -1)
{
var previousDistance = Modulus(contactPoint - rayOrigin);
var currentDistance = Modulus(contactPointTmp - rayOrigin);
// If the new distance is lesser, it wins
if(currentDistance < previousDistance)
{
intersectedWall = intersectedWallTmp;
contactPoint = contactPointTmp;
normal = normalTmp;
}
}
// If before there was no intersection, I update the final values
else
{
intersectedWall = intersectedWallTmp;
contactPoint = contactPointTmp;
normal = normalTmp;
}
// If the elevator is closed and the floor was found, it's not a good position
if(!IsElevatorOpen() && intersectedWall == 0)
intersectedWall = -2;
return intersectedWall;
}
function Elevator::RemoveObject(object)
{
mElevatorRoom.RemoveObject(object);
}
function Elevator::GetObj()
{
return mElevatorRoom.GetObj();
}
function Elevator::GetRoomWidth()
{
return mElevatorRoom.GetRoomWidth();
}
function Elevator::GetRoomDepth()
{
return mElevatorRoom.GetRoomDepth();
}
function Elevator::GetRoomHeight()
{
return mElevatorRoom.GetRoomHeight();
}
function Elevator::GetScale()
{
return mElevatorRoom.GetScale();
}
function Elevator::SetScale(x, y, z)
{
mElevatorRoom.SetScale(x, y, z);
}
function Elevator::SetLightsRange(min ,max)
{
mElevatorRoom.SetLightsRange(min, max);
}
function Elevator::GetLightsRange()
{
return mElevatorRoom.GetLightsRange();
}
function Elevator::SetPosition(position)
{
mElevatorRoom.SetPosition(position);
}
function Elevator::Translate(x, y, z)
{
mElevatorRoom.Translate(x, y, z);
}
function Elevator::Rotate(angle, x, y, z)
{
mElevatorRoom.Rotate(angle, x, y, z);
}
function Elevator::GetPosition()
{
return mElevatorRoom.GetPosition();
}
function Elevator::LocalToWorld(point)
{
return mElevatorRoom.LocalToWorld(point);
}
function Elevator::Hide()
{
mIsVisible = false;
mElevatorRoom.Hide();
}
function Elevator::Unhide()
{
mIsVisible = true;
mElevatorRoom.Unhide();
}
function Elevator::IsVisible()
{
return mIsVisible;
}