#include <iostream>
#include <conio.h> // For _kbhit() and _getch()
// Constants for movement speed
const float MOVE_SPEED = 0.5f;
// Function to print the current position of the object
void printPosition(float x, float y, float z) {
std::cout << "Object Position - X: " << x << ", Y: " << y << ", Z: " << z << std::endl;
}
// Function to handle user input and move the object
void handleInput(float& x, float& y, float& z) {
if (_kbhit()) {
char ch = _getch(); // Get the user input
switch (ch) {
case 'w': // Move forward
z -= MOVE_SPEED;
break;
case 's': // Move backward
z += MOVE_SPEED;
break;
case 'a': // Move left
x -= MOVE_SPEED;
break;
case 'd': // Move right
x += MOVE_SPEED;
break;
case 'q': // Move up
y += MOVE_SPEED;
break;
case 'e': // Move down
y -= MOVE_SPEED;
break;
case 'x': // Exit the simulation
std::exit(0);
break;
}
}
}
int main() {
// Initial position of the virtual object
float x = 0.0f;
float y = 0.0f;
float z = 0.0f;
std::cout << "Virtual Reality Simulation\n";
std::cout << "Controls:\n";
std::cout << "W: Move Forward\n";
std::cout << "S: Move Backward\n";
std::cout << "A: Move Left\n";
std::cout << "D: Move Right\n";
std::cout << "Q: Move Up\n";
std::cout << "E: Move Down\n";
std::cout << "X: Exit Simulation\n";
while (true) {
printPosition(x, y, z);
handleInput(x, y, z);
}
return 0;
}