Skip to content

Commit 2a7b35c

Browse files
committed
feat: update starting scene and enhance camera controls
1 parent d34b246 commit 2a7b35c

4 files changed

Lines changed: 41 additions & 110 deletions

File tree

config/app.jsonc

Lines changed: 18 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"start-scene": "menu",
2+
"start-scene": "play",
33
"window":
44
{
55
"title":"Default Game Window",
@@ -129,13 +129,23 @@
129129
},
130130
"world":[
131131
{
132-
"position": [0, 0, 10],
132+
"position": [0, 1.7, 0],
133133
"components": [
134134
{
135-
"type": "Camera"
135+
"type": "Camera",
136+
"fovY": 60,
137+
"near": 0.1,
138+
"far": 500
136139
},
137140
{
138-
"type": "Free Camera Controller"
141+
"type": "Free Camera Controller",
142+
"positionSensitivity": [5.0, 5.0, 5.0],
143+
"speedupFactor": 3.0,
144+
"groundLevel": 0.0,
145+
"playerHeight": 1.7
146+
},
147+
{
148+
"type": "Player"
139149
}
140150
],
141151
"children": [
@@ -153,108 +163,19 @@
153163
}
154164
]
155165
},
166+
167+
// Ground plane
156168
{
157-
"rotation": [-45, 0, 0],
158-
"components": [
159-
{
160-
"type": "Mesh Renderer",
161-
"mesh": "monkey",
162-
"material": "monkey"
163-
},
164-
{
165-
"type": "Movement",
166-
"linearVelocity": [0, 0.1, 0],
167-
"angularVelocity": [0, 45, 0]
168-
}
169-
]
170-
},
171-
{
172-
"position": [0, -1, 0],
169+
"position": [0, 0, 0],
173170
"rotation": [-90, 0, 0],
174-
"scale": [10, 10, 1],
171+
"scale": [100, 100, 1],
175172
"components": [
176173
{
177174
"type": "Mesh Renderer",
178175
"mesh": "plane",
179176
"material": "grass"
180177
}
181178
]
182-
},
183-
{
184-
"position": [0, 1, 2],
185-
"rotation": [0, 0, 0],
186-
"scale": [2, 2, 2],
187-
"components": [
188-
{
189-
"type": "Mesh Renderer",
190-
"mesh": "plane",
191-
"material": "glass"
192-
}
193-
]
194-
},
195-
{
196-
"position": [0, 1, -2],
197-
"rotation": [0, 0, 0],
198-
"scale": [2, 2, 2],
199-
"components": [
200-
{
201-
"type": "Mesh Renderer",
202-
"mesh": "plane",
203-
"material": "glass"
204-
}
205-
]
206-
},
207-
{
208-
"position": [2, 1, 0],
209-
"rotation": [0, 90, 0],
210-
"scale": [2, 2, 2],
211-
"components": [
212-
{
213-
"type": "Mesh Renderer",
214-
"mesh": "plane",
215-
"material": "glass"
216-
}
217-
]
218-
},
219-
{
220-
"position": [-2, 1, 0],
221-
"rotation": [0, 90, 0],
222-
"scale": [2, 2, 2],
223-
"components": [
224-
{
225-
"type": "Mesh Renderer",
226-
"mesh": "plane",
227-
"material": "glass"
228-
}
229-
]
230-
},
231-
{
232-
"position": [0, 3, 0],
233-
"rotation": [90, 0, 0],
234-
"scale": [2, 2, 2],
235-
"components": [
236-
{
237-
"type": "Mesh Renderer",
238-
"mesh": "plane",
239-
"material": "glass"
240-
}
241-
]
242-
},
243-
{
244-
"position": [0, 10, 0],
245-
"rotation": [45, 45, 0],
246-
"scale": [5, 5, 5],
247-
"components": [
248-
{
249-
"type": "Mesh Renderer",
250-
"mesh": "sphere",
251-
"material": "moon"
252-
},
253-
{
254-
"type": "Movement",
255-
"angularVelocity": [0, 90, 0]
256-
}
257-
]
258179
}
259180
]
260181
}

src/common/components/free-camera-controller.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ namespace our {
1111
fovSensitivity = data.value("fovSensitivity", fovSensitivity);
1212
positionSensitivity = data.value("positionSensitivity", positionSensitivity);
1313
speedupFactor = data.value("speedupFactor", speedupFactor);
14+
groundLevel = data.value("groundLevel", groundLevel);
15+
playerHeight = data.value("playerHeight", playerHeight);
1416
}
1517
} // namespace our

src/common/components/free-camera-controller.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ namespace our {
2020
3.0f}; // The unity per second of camera movement if WASD is pressed
2121
float speedupFactor = 5.0f; // A multiplier for the positionSensitivity if "Left Shift" is held.
2222

23+
// Ground collision
24+
float groundLevel = 0.0f;
25+
float playerHeight = 1.7f;
26+
2327
// The ID of this component type is "Free Camera Controller"
2428
static std::string getID() {
2529
return "Free Camera Controller";

src/common/systems/free-camera-controller.hpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,26 +70,30 @@ namespace our {
7070
glm::pi<float>() * 0.99f); // We keep the fov in the range 0.01*PI to 0.99*PI
7171
camera->fovY = fov;
7272

73-
// We get the camera model matrix (relative to its parent) to compute the front, up and right directions
73+
// We get the camera model matrix (relative to its parent) to compute the front and right directions
7474
glm::mat4 matrix = entity->localTransform.toMat4();
7575

76-
glm::vec3 front = glm::vec3(matrix * glm::vec4(0, 0, -1, 0)),
77-
up = glm::vec3(matrix * glm::vec4(0, 1, 0, 0)), right = glm::vec3(matrix * glm::vec4(1, 0, 0, 0));
76+
glm::vec3 front = glm::vec3(matrix * glm::vec4(0, 0, -1, 0));
77+
glm::vec3 right = glm::vec3(matrix * glm::vec4(1, 0, 0, 0));
7878

7979
glm::vec3 current_sensitivity = controller->positionSensitivity;
8080
// If the LEFT SHIFT key is pressed, we multiply the position sensitivity by the speed up factor
8181
if (app->getKeyboard().isPressed(GLFW_KEY_LEFT_SHIFT)) current_sensitivity *= controller->speedupFactor;
8282

83-
// We change the camera position based on the keys WASD/QE
84-
// S & W moves the player back and forth
85-
if (app->getKeyboard().isPressed(GLFW_KEY_W)) position += front * (deltaTime * current_sensitivity.z);
86-
if (app->getKeyboard().isPressed(GLFW_KEY_S)) position -= front * (deltaTime * current_sensitivity.z);
87-
// Q & E moves the player up and down
88-
if (app->getKeyboard().isPressed(GLFW_KEY_Q)) position += up * (deltaTime * current_sensitivity.y);
89-
if (app->getKeyboard().isPressed(GLFW_KEY_E)) position -= up * (deltaTime * current_sensitivity.y);
90-
// A & D moves the player left or right
91-
if (app->getKeyboard().isPressed(GLFW_KEY_D)) position += right * (deltaTime * current_sensitivity.x);
92-
if (app->getKeyboard().isPressed(GLFW_KEY_A)) position -= right * (deltaTime * current_sensitivity.x);
83+
// We change the camera position based on the keys WASD
84+
glm::vec3 frontXZ = glm::normalize(glm::vec3(front.x, 0, front.z));
85+
glm::vec3 rightXZ = glm::normalize(glm::vec3(right.x, 0, right.z));
86+
87+
// S & W moves the player back and forth along the ground plane
88+
if (app->getKeyboard().isPressed(GLFW_KEY_W)) position += frontXZ * (deltaTime * current_sensitivity.z);
89+
if (app->getKeyboard().isPressed(GLFW_KEY_S)) position -= frontXZ * (deltaTime * current_sensitivity.z);
90+
// A & D moves the player left or right along the ground plane
91+
if (app->getKeyboard().isPressed(GLFW_KEY_D)) position += rightXZ * (deltaTime * current_sensitivity.x);
92+
if (app->getKeyboard().isPressed(GLFW_KEY_A)) position -= rightXZ * (deltaTime * current_sensitivity.x);
93+
94+
// stop the player from going below the ground plane
95+
float minY = controller->groundLevel + controller->playerHeight;
96+
if (position.y < minY) position.y = minY;
9397
}
9498

9599
// When the state exits, it should call this function to ensure the mouse is unlocked

0 commit comments

Comments
 (0)