/
logic.c
141 lines (114 loc) · 3.25 KB
/
logic.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
/**
* @file
* Hier ist die Datenhaltung und Programmlogik
*
* @author Tilman Nedele, Maurice Tollmien
*/
/* ---- System Header einbinden ---- */
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <math.h>
/* ---- Eigene Header einbinden ---- */
#include "logic.h"
#include "mtVector.h"
#include "types.h"
/** Mausbewegung zoom/move/none */
MouseInterpretType G_Mouse;
/** Kameraposition */
MTVec3D G_CameraPosition;
/** Position der Maus */
Movement G_MouseMove = {0,0,0};
/** Startpunkt der Mausbewegung */
MTVec3D G_LastMouseCenter;
/* ------- GETTER / SETTER ------- */
/**
* Set-Function für den Status der Maus
* @param Status der Maus
*/
void setMouseEvent(MouseInterpretType state,int x, int y) {
G_MouseMove[0] = 0.0;
G_MouseMove[2] = 0.0;
G_LastMouseCenter.x = x;
G_LastMouseCenter.z = y;
G_Mouse = state;
}
/**
* Mouse Status.
*/
void setMouseState(MouseInterpretType state) {
G_Mouse = state;
}
void setMouseCoord(int x, int y) {
G_LastMouseCenter.x = x;
G_LastMouseCenter.z = y;
}
/**
* Get-Function für den Status der Maus
* @return Status der Maus
*/
MouseInterpretType getMouseEvent() {
return G_Mouse;
}
/**
* Gibt die Kamerakoordinate zurück.
*/
double getCameraPosition (int axis)
{
switch (axis) {
case 0: return G_CameraPosition.x;
case 1: return G_CameraPosition.y;
case 2: return G_CameraPosition.z;
}
return 0.0;
}
/**
* Bewegt die Kamera auf einem Kugelradius um den Szenenmittelpunkt.
*/
void setCameraMovement(int x,int y)
{
MTVec3D tmp = mtToVector3D(G_CameraPosition.x, G_CameraPosition.y,G_CameraPosition.z);
double factor, radius = mtVectorLength3D(G_CameraPosition);
G_MouseMove[0] = x-G_LastMouseCenter.x;
G_MouseMove[2] = y-G_LastMouseCenter.z;
G_LastMouseCenter.x = x;
G_LastMouseCenter.z = y;
/* Bewegung um Y-Achse: */
G_CameraPosition.x = cos(-G_MouseMove[0]*PI/180.0/CAMERA_MOVEMENT_SPEED)*tmp.x + sin(-G_MouseMove[0]*PI/180.0/CAMERA_MOVEMENT_SPEED)*tmp.z;
G_CameraPosition.z = -sin(-G_MouseMove[0]*PI/180.0/CAMERA_MOVEMENT_SPEED)*tmp.x + cos(-G_MouseMove[0]*PI/180.0/CAMERA_MOVEMENT_SPEED)*tmp.z;
/* Bewegung oben/unten */
G_CameraPosition.y += G_MouseMove[2]/(CAMERA_MOVEMENT_SPEED/2)*(mtVectorLength3D(G_CameraPosition)/100.0);
factor = 1.0 / (mtVectorLength3D(G_CameraPosition) / radius);
G_CameraPosition = mtMultiplyVectorScalar (G_CameraPosition, factor);
}
/**
* Verlängert/verkürzt den Vektor zur Kamera.
*/
void setCameraZoom(int x,int y)
{
double factor = 1.0 + (CAMERA_ZOOM_SPEED / 1000.0) * ((G_MouseMove[2] < 0.0) ? -1.0 : 1.0);
G_MouseMove[0] = x-G_LastMouseCenter.x;
G_MouseMove[2] = y-G_LastMouseCenter.z;
G_LastMouseCenter.x = x;
G_LastMouseCenter.z = y;
G_CameraPosition = mtMultiplyVectorScalar(G_CameraPosition, factor);
}
/* ------- INIT ------- */
/**
* Initialisiert die Kamera.
*/
void initCameraPosition ()
{
G_CameraPosition.x = CAMERA_X;
G_CameraPosition.y = CAMERA_Y;
G_CameraPosition.z = CAMERA_Z;
}
/**
* Hier findet die komplette Initialisierung des kompletten SPIEeles statt.
* Inklusive der Datenhaltung und des SPIEelfeldes.
*/
void initGame ()
{
G_LastMouseCenter = mtToVector3D(0,0,0);
initCameraPosition();
}