This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcamera.c
11441 lines (10085 loc) · 388 KB
/
camera.c
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <ultra64.h>
#define INCLUDED_FROM_CAMERA_C
#define ANALOG_AMOUNT 262144 / 360
// Analog camera movement by Pathétique (github.com/vrmiguel), y0shin and Mors
// Contribute or communicate bugs at github.com/vrmiguel/sm64-analog-camera
#include <stdio.h>
#include "sm64.h"
#include "camera.h"
#include "seq_ids.h"
#include "dialog_ids.h"
#include "audio/external.h"
#include "mario_misc.h"
#include "game.h"
#include "hud.h"
#include "engine/math_util.h"
#include "area.h"
#include "engine/surface_collision.h"
#include "engine/behavior_script.h"
#include "level_update.h"
#include "ingame_menu.h"
#include "mario_actions_cutscene.h"
#include "save_file.h"
#include "room.h"
#include "object_helpers.h"
#include "object_helpers2.h"
#include "print.h"
#include "spawn_sound.h"
#include "behavior_actions.h"
#include "behavior_data.h"
#include "object_list_processor.h"
#include "paintings.h"
#include "prevent_bss_reordering.h"
#include "engine/graph_node.h"
#include "level_table.h"
#define CBUTTON_MASK (U_CBUTTONS | D_CBUTTONS | L_CBUTTONS | R_CBUTTONS)
/**
* @file camera.c
* Implements the camera system, including C-button input, camera modes, camera triggers, and cutscenes.
*
* When working with the camera, you should be familiar with sm64's coordinate system.
* Relative to the camera, the coordinate system follows the right hand rule:
* +X points right.
* +Y points up.
* +Z points out of the screen.
*
* You should also be familiar with Euler angles: 'pitch', 'yaw', and 'roll'.
* pitch: rotation about the X-axis, measured from +Y.
* Unlike yaw and roll, pitch is bounded in +-0x4000 (90 degrees).
* Pitch is 0 when the camera points parallel to the xz-plane (+Y points straight up).
*
* yaw: rotation about the Y-axis, measured from (absolute) +Z.
* Positive yaw rotates clockwise, towards +X.
*
* roll: rotation about the Z-axis, measured from the camera's right direction.
* Unfortunately, it's weird: For some reason, roll is flipped. Positive roll makes the camera
* rotate counterclockwise, which means the WORLD rotates clockwise. Luckily roll is rarely
* used.
*
* Remember the right hand rule: make a thumbs-up with your right hand, stick your thumb in the
* +direction (except for roll), and the angle follows the rotation of your curled fingers.
*
* Illustrations:
* Following the right hand rule, each hidden axis's positive direction points out of the screen.
*
* YZ-Plane (pitch) XZ-Plane (yaw) XY-Plane (roll -- Note flipped)
* +Y -Z +Y
* ^ ^ (into the ^
* --|-- | screen) |<-
* +pitch / | \ -pitch | | \ -roll
* v | v | | |
* +Z <------O------> -Z -X <------O------> +X -X <------O------> +X
* | ^ | ^ | |
* | \ | / | / +roll
* | -yaw --|-- +yaw |<-
* v v v
* -Y +Z -Y
*
*/
// BSS
/**
* Global array of PlayerCameraState.
* L is real.
*/
struct PlayerCameraState gPlayerCameraState[2];
/**
* Stores lakitu's position from the last frame, used for transitioning in next_lakitu_state()
*/
Vec3f sOldPosition;
/**
* Stores lakitu's focus from the last frame, used for transitioning in next_lakitu_state()
*/
Vec3f sOldFocus;
/**
* Direction controlled by player 2, moves the focus during the credits.
*/
Vec3f sPlayer2FocusOffset;
/**
* The pitch used for the credits easter egg.
*/
s16 sCreditsPlayer2Pitch;
/**
* The yaw used for the credits easter egg.
*/
s16 sCreditsPlayer2Yaw;
/**
* Used to decide when to zoom out in the pause menu.
*/
u8 sFramesPaused;
struct CameraFOVStatus sFOVState;
struct TransitionInfo sModeTransition;
struct PlayerGeometry sMarioGeometry;
s16 unusedFreeRoamWallYaw;
s16 sAvoidYawVel;
s16 sCameraYawAfterDoorCutscene;
s16 unusedSplinePitch;
s16 unusedSplineYaw;
// Shaky Hand-held Camera effect variables
struct HandheldShakePoint sHandheldShakeSpline[4];
s16 sHandheldShakeMag;
f32 sHandheldShakeTimer;
f32 sHandheldShakeInc;
s16 sHandheldShakePitch;
s16 sHandheldShakeYaw;
s16 sHandheldShakeRoll;
u32 unused8033B30C;
u32 unused8033B310;
/**
* Determines which R-Trigger mode is selected in the pause menu.
*/
s16 sSelectionFlags;
s16 unused8033B316;
/**
* Flags that determine whether the player has already rotated left or right. Used in radial mode to
* determine whether to rotate all the way, or just to 60 degrees.
*/
s16 s2ndRotateFlags;
s16 unused8033B31A;
/**
* Flags that control buzzes and sounds that play, mostly for C-button input.
*/
s16 sCameraSoundFlags;
/**
* Stores what C-Buttons are pressed this frame.
*/
u16 sCButtonsPressed;
/**
* A copy of gDialogID, the dialog displayed during the cutscene.
*/
s16 sCutsceneDialogID;
/**
* Lakitu's position and focus.
* @see LakituState
*/
struct LakituState gLakituState;
s16 unused8033B3E8;
/**
* The angle of the direction vector from the area's center to mario's position.
*/
s16 sAreaYaw;
/**
* How much sAreaYaw changed when mario moved.
*/
s16 sAreaYawChange;
/**
* Lakitu's distance from mario in C-Down mode
*/
s16 sLakituDist;
/**
* How much lakitu looks down in C-Down mode
*/
s16 sLakituPitch;
/**
* The amount of distance left to zoom out
*/
f32 sZoomAmount;
s16 sCSideButtonYaw;
/**
* Sound timer used to space out sounds in behind mario mode
*/
s16 sBehindMarioSoundTimer;
/**
* Virtually unused aside being set to 0 and compared with gCameraZoomDist (which is never < 0)
*/
f32 sZeroZoomDist;
/**
* The camera's pitch in C-Up mode. Mainly controls mario's head rotation.
*/
s16 sCUpCameraPitch;
/**
* The current mode's yaw, which gets added to the camera's yaw.
*/
s16 sModeOffsetYaw;
/**
* Stores mario's yaw around the stairs, relative to the camera's position.
*
* Used in update_spiral_stairs_camera()
*/
s16 sSpiralStairsYawOffset;
/**
* The constant offset to 8-direction mode's yaw.
*/
s16 s8DirModeBaseYaw;
/**
* Player-controlled yaw offset in 8-direction mode, a multiple of 45 degrees.
*/
s16 s8DirModeYawOffset;
/**
* The distance that the camera will look ahead of mario in the direction mario is facing.
*/
f32 sPanDistance;
/**
* When mario gets in the cannon, it is pointing straight up and rotates down.
* This is used to make the camera start up and rotate down, like the cannon.
*/
f32 sCannonYOffset;
struct ModeTransitionInfo sModeInfo;
/**
* Offset added to sFixedModeBasePosition when mario is inside, near the castle lobby entrance
*/
Vec3f sCastleEntranceOffset;
/**
* The index into the current parallel tracking path
*/
u32 sParTrackIndex;
/**
* The current list of ParallelTrackingPoints used in update_parallel_tracking_camera()
*/
struct ParallelTrackingPoint *sParTrackPath;
/**
* On the first frame after the camera changes to a different parallel tracking path, this stores the
* displacement between the camera's calculated new position and its previous positions
*
* This transition offset is then used to smoothly interpolate the camera's position between the two
* paths
*/
struct CameraStoredInfo sParTrackTransOff;
/**
* The information stored when C-Up is active, used to update lakitu's rotation when exiting C-Up
*/
struct CameraStoredInfo sCameraStoreCUp;
/**
* The information stored during cutscenes
*/
struct CameraStoredInfo sCameraStoreCutscene;
/**
* Flags that determine what movements the camera should start / do this frame.
*/
s16 gCameraMovementFlags;
/**
* Flags that change how modes operate and how lakitu moves.
* The most commonly used flag is CAM_FLAG_SMOOTH_MOVEMENT, which makes lakitu fly to the next position,
* instead of warping.
*/
s16 sStatusFlags;
/**
* The current spline that controls the camera's position during the credits.
*/
struct CutsceneSplinePoint sCurCreditsSplinePos[32];
/**
* The current spline that controls the camera's focus during the credits.
*/
struct CutsceneSplinePoint sCurCreditsSplineFocus[32];
/**
* The current segment of the CutsceneSplinePoint[] being used.
*/
s16 sCutsceneSplineSegment;
/**
* The progress (from 0 to 1) through the current spline segment.
* When it becomes >= 1, 1.0 is subtracted from it and sCutsceneSplineSegment is increased.
*/
f32 sCutsceneSplineSegmentProgress;
s16 unused8033B6E8;
/**
* The currently playing shot in the cutscene.
*/
s16 sCutsceneShot;
/**
* The current frame of the cutscene shot.
*/
s16 gCutsceneTimer;
/**
* These structs are used by the cutscenes. Most of the fields are unused, and some (all?) of the used
* ones have multiple uses.
* Check the cutscene_start functions for documentation on the cvars used by a specific cutscene.
*/
struct CutsceneVariable sCutsceneVars[10];
/**
* Controls when an object-based cutscene should end. It's only used in the star spawn cutscenes, but
* yoshi also toggles this.
*/
s32 gObjCutsceneDone;
/**
* Controls which object to spawn in the intro and ending cutscenes.
*/
u32 gCutsceneObjSpawn;
struct Camera *gCamera;
// first iteration of data
u32 unused8032CFC0 = 0;
struct Object *gCutsceneFocus = NULL;
// other camera focuses?
u32 unused8032CFC8 = 0;
u32 unused8032CFCC = 0;
struct Object *gSecondCameraFocus = NULL;
/**
* How fast the camera's yaw should approach the next yaw.
*/
s16 sYawSpeed = 0x400;
s32 gCurrLevelArea = 0;
u32 gPrevLevel = 0;
f32 unused8032CFE0 = 1000.0f;
f32 unused8032CFE4 = 800.0f;
u32 unused8032CFE8 = 0;
f32 gCameraZoomDist = 800.0f;
/**
* A cutscene that plays when the player interacts with an object
*/
u8 sObjectCutscene = 0;
/**
* The ID of the cutscene that ended. It's set to 0 if no cutscene ended less than 8 frames ago.
*
* It is only used to prevent the same cutscene from playing twice before 8 frames have passed.
*/
u8 gRecentCutscene = 0;
/**
* A timer that increments for 8 frames when a cutscene ends.
* When it reaches 8, it sets gRecentCutscene to 0.
*/
u8 sFramesSinceCutsceneEnded = 0;
/**
* Mario's response to a dialog.
* 0 = No response yet
* 1 = Yes
* 2 = No
* 3 = Dialog doesn't have a response
*/
u8 sCutsceneDialogResponse = 0;
struct PlayerCameraState *sMarioCamState = &gPlayerCameraState[0];
struct PlayerCameraState *sLuigiCamState = &gPlayerCameraState[1];
u32 unused8032D008 = 0;
Vec3f sFixedModeBasePosition = { 646.0f, 143.0f, -1513.0f };
Vec3f sUnusedModeBasePosition_2 = { 646.0f, 143.0f, -1513.0f };
Vec3f sUnusedModeBasePosition_3 = { 646.0f, 143.0f, -1513.0f };
Vec3f sUnusedModeBasePosition_4 = { 646.0f, 143.0f, -1513.0f };
Vec3f sUnusedModeBasePosition_5 = { 646.0f, 143.0f, -1513.0f };
s32 update_radial_camera(struct Camera *c, Vec3f, Vec3f);
s32 update_outward_radial_camera(struct Camera *c, Vec3f, Vec3f);
s32 update_behind_mario_camera(struct Camera *c, Vec3f, Vec3f);
s32 update_mario_camera(struct Camera *c, Vec3f, Vec3f);
s32 unused_update_mode_5_camera(struct Camera *c, Vec3f, Vec3f);
s32 update_c_up(struct Camera *c, Vec3f, Vec3f);
s32 nop_update_water_camera(struct Camera *c, Vec3f, Vec3f);
s32 update_slide_or_0f_camera(struct Camera *c, Vec3f, Vec3f);
s32 update_in_cannon(struct Camera *c, Vec3f, Vec3f);
s32 update_boss_fight_camera(struct Camera *c, Vec3f, Vec3f);
s32 update_parallel_tracking_camera(struct Camera *c, Vec3f, Vec3f);
s32 update_fixed_camera(struct Camera *c, Vec3f, Vec3f);
s32 update_8_directions_camera(struct Camera *c, Vec3f, Vec3f);
s32 update_slide_or_0f_camera(struct Camera *c, Vec3f, Vec3f);
s32 update_spiral_stairs_camera(struct Camera *c, Vec3f, Vec3f);
typedef s32 (*CameraTransition)(struct Camera *c, Vec3f, Vec3f);
CameraTransition sModeTransitions[] = {
NULL,
update_radial_camera,
update_outward_radial_camera,
update_behind_mario_camera,
update_mario_camera,
unused_update_mode_5_camera,
update_c_up,
update_mario_camera,
nop_update_water_camera,
update_slide_or_0f_camera,
update_in_cannon,
update_boss_fight_camera,
update_parallel_tracking_camera,
update_fixed_camera,
update_8_directions_camera,
update_slide_or_0f_camera,
update_mario_camera,
update_spiral_stairs_camera
};
// Move these two tables to another include file?
extern u8 sDanceCutsceneIndexTable[][4];
extern u8 sZoomOutAreaMasks[];
/**
* Starts a camera shake triggered by an interaction
*/
void set_camera_shake_from_hit(s16 shake) {
switch (shake) {
// Makes the camera stop for a bit
case SHAKE_ATTACK:
gLakituState.focHSpeed = 0;
gLakituState.posHSpeed = 0;
break;
case SHAKE_FALL_DAMAGE:
set_camera_pitch_shake(0x60, 0x3, 0x8000);
set_camera_roll_shake(0x60, 0x3, 0x8000);
break;
case SHAKE_GROUND_POUND:
set_camera_pitch_shake(0x60, 0xC, 0x8000);
break;
case SHAKE_SMALL_DAMAGE:
if (sMarioCamState->action & (ACT_FLAG_SWIMMING | ACT_FLAG_METAL_WATER)) {
set_camera_yaw_shake(0x200, 0x10, 0x1000);
set_camera_roll_shake(0x400, 0x20, 0x1000);
set_fov_shake(0x100, 0x30, 0x8000);
} else {
set_camera_yaw_shake(0x80, 0x8, 0x4000);
set_camera_roll_shake(0x80, 0x8, 0x4000);
set_fov_shake(0x100, 0x30, 0x8000);
}
gLakituState.focHSpeed = 0;
gLakituState.posHSpeed = 0;
break;
case SHAKE_MED_DAMAGE:
if (sMarioCamState->action & (ACT_FLAG_SWIMMING | ACT_FLAG_METAL_WATER)) {
set_camera_yaw_shake(0x400, 0x20, 0x1000);
set_camera_roll_shake(0x600, 0x30, 0x1000);
set_fov_shake(0x180, 0x40, 0x8000);
} else {
set_camera_yaw_shake(0x100, 0x10, 0x4000);
set_camera_roll_shake(0x100, 0x10, 0x4000);
set_fov_shake(0x180, 0x40, 0x8000);
}
gLakituState.focHSpeed = 0;
gLakituState.posHSpeed = 0;
break;
case SHAKE_LARGE_DAMAGE:
if (sMarioCamState->action & (ACT_FLAG_SWIMMING | ACT_FLAG_METAL_WATER)) {
set_camera_yaw_shake(0x600, 0x30, 0x1000);
set_camera_roll_shake(0x800, 0x40, 0x1000);
set_fov_shake(0x200, 0x50, 0x8000);
} else {
set_camera_yaw_shake(0x180, 0x20, 0x4000);
set_camera_roll_shake(0x200, 0x20, 0x4000);
set_fov_shake(0x200, 0x50, 0x8000);
}
gLakituState.focHSpeed = 0;
gLakituState.posHSpeed = 0;
break;
case SHAKE_HIT_FROM_BELOW:
gLakituState.focHSpeed = 0.07;
gLakituState.posHSpeed = 0.07;
break;
case SHAKE_SHOCK:
set_camera_pitch_shake(RandomFloat() * 64.f, 0x8, 0x8000);
set_camera_yaw_shake(RandomFloat() * 64.f, 0x8, 0x8000);
break;
}
}
/**
* Start a shake from the environment
*/
void set_environmental_camera_shake(s16 shake) {
switch (shake) {
case SHAKE_ENV_EXPLOSION:
set_camera_pitch_shake(0x60, 0x8, 0x4000);
break;
case SHAKE_ENV_BOWSER_THROW_BOUNCE:
set_camera_pitch_shake(0xC0, 0x8, 0x4000);
break;
case SHAKE_ENV_BOWSER_JUMP:
set_camera_pitch_shake(0x100, 0x8, 0x3000);
break;
case SHAKE_ENV_UNUSED_6:
set_camera_roll_shake(0x80, 0x10, 0x3000);
break;
case SHAKE_ENV_UNUSED_7:
set_camera_pitch_shake(0x20, 0x8, 0x8000);
break;
case SHAKE_ENV_PYRAMID_EXPLODE:
set_camera_pitch_shake(0x40, 0x8, 0x8000);
break;
case SHAKE_ENV_JRB_SHIP_DRAIN:
set_camera_pitch_shake(0x20, 0x8, 0x8000);
set_camera_roll_shake(0x400, 0x10, 0x100);
break;
case SHAKE_ENV_FALLING_BITS_PLAT:
set_camera_pitch_shake(0x40, 0x2, 0x8000);
break;
case SHAKE_ENV_UNUSED_5:
set_camera_yaw_shake(-0x200, 0x80, 0x200);
break;
}
}
/**
* Starts a camera shake, but scales the amplitude by the point's distance from the camera
*/
void set_camera_shake_from_point(s16 shake, f32 posX, f32 posY, f32 posZ) {
switch (shake) {
case SHAKE_POS_BOWLING_BALL:
set_pitch_shake_from_point(0x28, 0x8, 0x4000, 2000.f, posX, posY, posZ);
break;
case SHAKE_POS_SMALL:
set_pitch_shake_from_point(0x80, 0x8, 0x4000, 4000.f, posX, posY, posZ);
set_fov_shake_from_point_preset(SHAKE_FOV_SMALL, posX, posY, posZ);
break;
case SHAKE_POS_MEDIUM:
set_pitch_shake_from_point(0xC0, 0x8, 0x4000, 6000.f, posX, posY, posZ);
set_fov_shake_from_point_preset(SHAKE_FOV_MEDIUM, posX, posY, posZ);
break;
case SHAKE_POS_LARGE:
set_pitch_shake_from_point(0x100, 0x8, 0x3000, 8000.f, posX, posY, posZ);
set_fov_shake_from_point_preset(SHAKE_FOV_LARGE, posX, posY, posZ);
break;
}
}
/**
* Start a camera shake from an environmental source, but only shake the camera's pitch.
*/
void unused_set_camera_pitch_shake_env(s16 shake) {
switch (shake) {
case SHAKE_ENV_EXPLOSION:
set_camera_pitch_shake(0x60, 0x8, 0x4000);
break;
case SHAKE_ENV_BOWSER_THROW_BOUNCE:
set_camera_pitch_shake(0xC0, 0x8, 0x4000);
break;
case SHAKE_ENV_BOWSER_JUMP:
set_camera_pitch_shake(0x100, 0x8, 0x3000);
break;
}
}
/**
* Calculates mario's distance to the floor, or the water level if it is above the floor. Then:
* `posOff` is set to the distance multiplied by posMul and bounded to [-posBound, posBound]
* `focOff` is set to the distance multiplied by focMul and bounded to [-focBound, focBound]
*
* Notes:
* posMul is always 1.0f, focMul is always 0.9f
* both ranges are always 200.f
* Since focMul is 0.9, `focOff` is closer to the floor than `posOff`
* posOff and focOff are sometimes the same address, which just ignores the pos calculation
*/
void calc_y_to_curr_floor(f32 *posOff, f32 posMul, f32 posBound, f32 *focOff, f32 focMul, f32 focBound) {
f32 floorHeight = sMarioGeometry.currFloorHeight;
f32 waterHeight;
UNUSED s32 filler;
if (!(sMarioCamState->action & ACT_FLAG_METAL_WATER)) {
//! @bug this should use sMarioGeometry.waterHeight
if (floorHeight < (waterHeight = find_water_level(sMarioCamState->pos[0], sMarioCamState->pos[2]))) {
floorHeight = waterHeight;
}
}
if (sMarioCamState->action & ACT_FLAG_ON_POLE) {
if (sMarioGeometry.currFloorHeight >= gMarioStates[0].usedObj->oPosY && sMarioCamState->pos[1]
< 0.7f * gMarioStates[0].usedObj->hitboxHeight + gMarioStates[0].usedObj->oPosY) {
posBound = 1200;
}
}
*posOff = (floorHeight - sMarioCamState->pos[1]) * posMul;
if (*posOff > posBound) {
*posOff = posBound;
}
if (*posOff < -posBound) {
*posOff = -posBound;
}
*focOff = (floorHeight - sMarioCamState->pos[1]) * focMul;
if (*focOff > focBound) {
*focOff = focBound;
}
if (*focOff < -focBound) {
*focOff = -focBound;
}
}
void focus_on_mario(Vec3f focus, Vec3f pos, f32 posYOff, f32 focYOff, f32 dist, s16 pitch, s16 yaw) {
Vec3f marioPos;
marioPos[0] = sMarioCamState->pos[0];
marioPos[1] = sMarioCamState->pos[1] + posYOff;
marioPos[2] = sMarioCamState->pos[2];
vec3f_set_dist_and_angle(marioPos, pos, dist, pitch + sLakituPitch, yaw);
focus[0] = sMarioCamState->pos[0];
focus[1] = sMarioCamState->pos[1] + focYOff;
focus[2] = sMarioCamState->pos[2];
}
static UNUSED void set_pos_to_mario(Vec3f foc, Vec3f pos, f32 yOff, f32 focYOff, f32 dist, s16 pitch, s16 yaw) {
Vec3f marioPos;
f32 posDist;
f32 focDist;
s16 posPitch;
s16 posYaw;
s16 focPitch;
s16 focYaw;
vec3f_copy(marioPos, sMarioCamState->pos);
marioPos[1] += yOff;
vec3f_set_dist_and_angle(marioPos, pos, dist, pitch + sLakituPitch, yaw);
vec3f_get_dist_and_angle(pos, sMarioCamState->pos, &posDist, &posPitch, &posYaw);
//! Useless get and set
vec3f_get_dist_and_angle(pos, foc, &focDist, &focPitch, &focYaw);
vec3f_set_dist_and_angle(pos, foc, focDist, focPitch, focYaw);
foc[1] = sMarioCamState->pos[1] + focYOff;
}
/**
* Set the camera's y coordinate to goalHeight, respecting floors and ceilings in the way
*/
void set_camera_height(struct Camera *c, f32 goalHeight) {
struct Surface *surface;
f32 marioFloorHeight;
f32 marioCeilHeight;
f32 camFloorHeight;
UNUSED u8 filler[8];
UNUSED s16 action = sMarioCamState->action;
f32 baseOff = 125.f;
f32 camCeilHeight = find_ceil(c->pos[0], gLakituState.goalPos[1] - 50.f, c->pos[2], &surface);
if (sMarioCamState->action & ACT_FLAG_HANGING) {
marioCeilHeight = sMarioGeometry.currCeilHeight;
marioFloorHeight = sMarioGeometry.currFloorHeight;
if (marioFloorHeight < marioCeilHeight - 400.f) {
marioFloorHeight = marioCeilHeight - 400.f;
}
goalHeight = marioFloorHeight + (marioCeilHeight - marioFloorHeight) * 0.4f;
if (sMarioCamState->pos[1] - 400 > goalHeight) {
goalHeight = sMarioCamState->pos[1] - 400;
}
approach_camera_height(c, goalHeight, 5.f);
} else {
camFloorHeight = find_floor(c->pos[0], c->pos[1] + 100.f, c->pos[2], &surface) + baseOff;
marioFloorHeight = baseOff + sMarioGeometry.currFloorHeight;
if (camFloorHeight < marioFloorHeight) {
camFloorHeight = marioFloorHeight;
}
if (goalHeight < camFloorHeight) {
goalHeight = camFloorHeight;
c->pos[1] = goalHeight;
}
// Warp camera to goalHeight if further than 1000 and mario is stuck in the ground
if (sMarioCamState->action == ACT_BUTT_STUCK_IN_GROUND ||
sMarioCamState->action == ACT_HEAD_STUCK_IN_GROUND ||
sMarioCamState->action == ACT_FEET_STUCK_IN_GROUND) {
if (ABS(c->pos[1] - goalHeight) > 1000.f) {
c->pos[1] = goalHeight;
}
}
approach_camera_height(c, goalHeight, 20.f);
if (camCeilHeight != 20000.f) {
camCeilHeight -= baseOff;
if ((c->pos[1] > camCeilHeight && sMarioGeometry.currFloorHeight + baseOff < camCeilHeight)
|| (sMarioGeometry.currCeilHeight != 20000.f
&& sMarioGeometry.currCeilHeight > camCeilHeight && c->pos[1] > camCeilHeight)) {
c->pos[1] = camCeilHeight;
}
}
}
}
/**
* Pitch the camera down when the camera is facing down a slope
*/
s16 look_down_slopes(s16 camYaw) {
struct Surface *floor;
f32 floorDY;
// Default pitch
s16 pitch = 0x05B0;
// x and z offsets towards the camera
f32 xOff = sMarioCamState->pos[0] + sins(camYaw) * 40.f;
f32 zOff = sMarioCamState->pos[2] + coss(camYaw) * 40.f;
floorDY = find_floor(xOff, sMarioCamState->pos[1], zOff, &floor) - sMarioCamState->pos[1];
if (floor != NULL) {
if (floor->type != SURFACE_WALL_MISC && floorDY > 0) {
if (floor->normal.z == 0.f && floorDY < 100.f) {
pitch = 0x05B0;
} else {
// Add the slope's angle of declination to the pitch
pitch += atan2s(40.f, floorDY);
}
}
}
return pitch;
}
/**
* Look ahead to the left or right in the direction the player is facing
* The calculation for pan[0] could be simplified to:
* yaw = -yaw;
* pan[0] = sins(sMarioCamState->faceAngle[1] + yaw) * sins(0xC00) * dist;
* Perhaps, early in development, the pan used to be calculated for both the x and z directions
*
* Since this function only affects the camera's focus, mario's movement direction isn't affected.
*/
void pan_ahead_of_player(struct Camera *c) {
f32 dist;
s16 pitch;
s16 yaw;
Vec3f pan = { 0, 0, 0 };
// Get distance and angle from camera to mario.
vec3f_get_dist_and_angle(c->pos, sMarioCamState->pos, &dist, &pitch, &yaw);
// The camera will pan ahead up to about 30% of the camera's distance to mario.
pan[2] = sins(0xC00) * dist;
rotate_in_xz(pan, pan, sMarioCamState->faceAngle[1]);
// rotate in the opposite direction
yaw = -yaw;
rotate_in_xz(pan, pan, yaw);
// Only pan left or right
pan[2] = 0.f;
// If mario is long jumping, or on a flag pole (but not at the top), then pan in the opposite direction
if (sMarioCamState->action == ACT_LONG_JUMP ||
(sMarioCamState->action != ACT_TOP_OF_POLE && (sMarioCamState->action & ACT_FLAG_ON_POLE))) {
pan[0] = -pan[0];
}
// Slowly make the actual pan, sPanDistance, approach the calculated pan
// If mario is sleeping, then don't pan
if (sStatusFlags & CAM_FLAG_SLEEPING) {
approach_f32_asymptotic_bool(&sPanDistance, 0.f, 0.025f);
} else {
approach_f32_asymptotic_bool(&sPanDistance, pan[0], 0.025f);
}
// Now apply the pan. It's a dir vector to the left or right, rotated by the camera's yaw to mario
pan[0] = sPanDistance;
yaw = -yaw;
rotate_in_xz(pan, pan, yaw);
vec3f_add(c->focus, pan);
}
s16 find_in_bounds_yaw_wdw_bob_thi(Vec3f pos, Vec3f origin, s16 yaw) {
switch (gCurrLevelArea) {
case AREA_WDW_MAIN:
yaw = clamp_positions_and_find_yaw(pos, origin, 4508.f, -3739.f, 4508.f, -3739.f);
break;
case AREA_BOB:
yaw = clamp_positions_and_find_yaw(pos, origin, 8000.f, -8000.f, 7050.f, -8000.f);
break;
case AREA_THI_HUGE:
yaw = clamp_positions_and_find_yaw(pos, origin, 8192.f, -8192.f, 8192.f, -8192.f);
break;
case AREA_THI_TINY:
yaw = clamp_positions_and_find_yaw(pos, origin, 2458.f, -2458.f, 2458.f, -2458.f);
break;
}
return yaw;
}
/**
* Rotates the camera around the area's center point.
*/
s32 update_radial_camera(struct Camera *c, Vec3f focus, Vec3f pos) {
f32 cenDistX = sMarioCamState->pos[0] - c->areaCenX;
f32 cenDistZ = sMarioCamState->pos[2] - c->areaCenZ;
s16 camYaw = atan2s(cenDistZ, cenDistX) + sModeOffsetYaw;
s16 pitch = look_down_slopes(camYaw);
UNUSED f32 unused1;
f32 posY;
f32 focusY;
UNUSED f32 unused2;
UNUSED f32 unused3;
f32 yOff = 125.f;
f32 baseDist = 1000.f;
sAreaYaw = camYaw - sModeOffsetYaw;
calc_y_to_curr_floor(&posY, 1.f, 200.f, &focusY, 0.9f, 200.f);
focus_on_mario(focus, pos, posY + yOff, focusY + yOff, sLakituDist + baseDist, pitch, camYaw);
camYaw = find_in_bounds_yaw_wdw_bob_thi(pos, focus, camYaw);
return camYaw;
}
/**
* Update the camera during 8 directional mode
*/
s32 update_8_directions_camera(struct Camera *c, Vec3f focus, Vec3f pos) {
UNUSED f32 cenDistX = sMarioCamState->pos[0] - c->areaCenX;
UNUSED f32 cenDistZ = sMarioCamState->pos[2] - c->areaCenZ;
s16 camYaw = s8DirModeBaseYaw + s8DirModeYawOffset;
s16 pitch = look_down_slopes(camYaw);
f32 posY;
f32 focusY;
UNUSED f32 unused1;
UNUSED f32 unused2;
UNUSED f32 unused3;
f32 yOff = 125.f;
f32 baseDist = 1000.f;
sAreaYaw = camYaw;
calc_y_to_curr_floor(&posY, 1.f, 200.f, &focusY, 0.9f, 200.f);
focus_on_mario(focus, pos, posY + yOff, focusY + yOff, sLakituDist + baseDist, pitch, camYaw);
pan_ahead_of_player(c);
if (gCurrLevelArea == AREA_DDD_SUB) {
camYaw = clamp_positions_and_find_yaw(pos, focus, 6839.f, 995.f, 5994.f, -3945.f);
}
return camYaw;
}
/**
* Moves the camera for the radial and outward radial camera modes.
*
* If sModeOffsetYaw is 0, the camera points directly at the area center point.
*/
void radial_camera_move(struct Camera *c) {
s16 maxAreaYaw = DEGREES(60);
s16 minAreaYaw = DEGREES(-60);
s16 rotateSpeed = 0x1000;
s16 avoidYaw;
s32 avoidStatus;
UNUSED s16 unused1 = 0;
UNUSED s32 unused2 = 0;
f32 areaDistX = sMarioCamState->pos[0] - c->areaCenX;
f32 areaDistZ = sMarioCamState->pos[2] - c->areaCenZ;
UNUSED s32 filler;
// How much the camera's yaw changed
s16 yawOffset = calculate_yaw(sMarioCamState->pos, c->pos) - atan2s(areaDistZ, areaDistX);
if (yawOffset > maxAreaYaw) {
yawOffset = maxAreaYaw;
}
if (yawOffset < minAreaYaw) {
yawOffset = minAreaYaw;
}
// Check if mario stepped on a surface that rotates the camera. For example, when mario enters the
// gate in BoB, the camera turns right to face up the hill path
if (!(gCameraMovementFlags & CAM_MOVE_ROTATE)) {
// ANALOG ADDITION
if (gPlayer2Controller->stickX != 0)
{
printf("gPlayer2Controller->stickX: %f\n", gPlayer2Controller->stickX);
if (gPlayer2Controller->stickX > 0)
{
gCameraMovementFlags &= ~(CAM_MOVE_ROTATE_RIGHT | CAM_MOVE_ENTERED_ROTATE_SURFACE);
sModeOffsetYaw += ANALOG_AMOUNT * ((gPlayer2Controller->stickX / 64.0f) * (gPlayer2Controller->stickX / 64.0f)) * -1.0f;
}
else
{
gCameraMovementFlags &= ~(CAM_MOVE_ROTATE_LEFT | CAM_MOVE_ENTERED_ROTATE_SURFACE);
sModeOffsetYaw += ANALOG_AMOUNT * ((gPlayer2Controller->stickX / 64.0f) * (gPlayer2Controller->stickX / 64.0f)) * 1.0f;
}
}
if (sMarioGeometry.currFloorType == SURFACE_CAMERA_MIDDLE
&& sMarioGeometry.prevFloorType != SURFACE_CAMERA_MIDDLE) {
gCameraMovementFlags |= (CAM_MOVE_RETURN_TO_MIDDLE | CAM_MOVE_ENTERED_ROTATE_SURFACE);
}
if (sMarioGeometry.currFloorType == SURFACE_CAMERA_ROTATE_RIGHT
&& sMarioGeometry.prevFloorType != SURFACE_CAMERA_ROTATE_RIGHT) {
gCameraMovementFlags |= (CAM_MOVE_ROTATE_RIGHT | CAM_MOVE_ENTERED_ROTATE_SURFACE);
}
if (sMarioGeometry.currFloorType == SURFACE_CAMERA_ROTATE_LEFT
&& sMarioGeometry.prevFloorType != SURFACE_CAMERA_ROTATE_LEFT) {
gCameraMovementFlags |= (CAM_MOVE_ROTATE_LEFT | CAM_MOVE_ENTERED_ROTATE_SURFACE);
}
}
if (gCameraMovementFlags & CAM_MOVE_ENTERED_ROTATE_SURFACE) {
rotateSpeed = 0x200;
}
if (c->mode == CAMERA_MODE_OUTWARD_RADIAL) {
areaDistX = -areaDistX;
areaDistZ = -areaDistZ;
}
// Avoid obstructing walls
avoidStatus = rotate_camera_around_walls(c, c->pos, &avoidYaw, 0x400);
if (avoidStatus == 3) {
if (avoidYaw - atan2s(areaDistZ, areaDistX) + DEGREES(90) < 0) {
avoidYaw += DEGREES(180);
}
// We want to change sModeOffsetYaw so that the player is no longer obstructed by the wall.
// So, we make avoidYaw relative to the yaw around the area center
avoidYaw -= atan2s(areaDistZ, areaDistX);
// Bound avoid yaw to radial mode constraints
if (avoidYaw > DEGREES(105)) {
avoidYaw = DEGREES(105);
}
if (avoidYaw < DEGREES(-105)) {
avoidYaw = DEGREES(-105);
}
}
if (gCameraMovementFlags & CAM_MOVE_RETURN_TO_MIDDLE) {
if (camera_approach_s16_symmetric_bool(&sModeOffsetYaw, 0, rotateSpeed) == 0) {
gCameraMovementFlags &= ~CAM_MOVE_RETURN_TO_MIDDLE;
}
} else {
// Prevent the player from rotating into obstructing walls
if ((gCameraMovementFlags & CAM_MOVE_ROTATE_RIGHT) && avoidStatus == 3
&& avoidYaw + 0x10 < sModeOffsetYaw) {
sModeOffsetYaw = avoidYaw;
gCameraMovementFlags &= ~(CAM_MOVE_ROTATE_RIGHT | CAM_MOVE_ENTERED_ROTATE_SURFACE);
}
if ((gCameraMovementFlags & CAM_MOVE_ROTATE_LEFT) && avoidStatus == 3
&& avoidYaw - 0x10 > sModeOffsetYaw) {
sModeOffsetYaw = avoidYaw;
gCameraMovementFlags &= ~(CAM_MOVE_ROTATE_LEFT | CAM_MOVE_ENTERED_ROTATE_SURFACE);
}
// If it's the first time rotating, just rotate to +-60 degrees
if (!(s2ndRotateFlags & CAM_MOVE_ROTATE_RIGHT) && (gCameraMovementFlags & CAM_MOVE_ROTATE_RIGHT)
&& camera_approach_s16_symmetric_bool(&sModeOffsetYaw, maxAreaYaw, rotateSpeed) == 0) {
gCameraMovementFlags &= ~(CAM_MOVE_ROTATE_RIGHT | CAM_MOVE_ENTERED_ROTATE_SURFACE);
}
if (!(s2ndRotateFlags & CAM_MOVE_ROTATE_LEFT) && (gCameraMovementFlags & CAM_MOVE_ROTATE_LEFT)
&& camera_approach_s16_symmetric_bool(&sModeOffsetYaw, minAreaYaw, rotateSpeed) == 0) {