user controlled added variable pushForce

User can user 1, 2, 3, 4 or 5 key to change the force at which the walker is pushed away from the mouse pointer when the mouse button is pressed.
This commit is contained in:
2025-01-31 17:31:40 -05:00
parent 9b06da8ee1
commit f204dc5898

View File

@@ -1,6 +1,6 @@
// ReWalker
// A random walker that moves around the screen with a fading trail and mouse interaction.
// Now with Spacebar reset functionality!
// Now with real-time adjustable push force using number keys 1-5!
// Written using Redacted ReWindow https://git.redacted.cc/Redacted/ReWindow
// Written by Rich
// With help from (and thanks to) from Maxine, Josh and Bill
@@ -22,9 +22,6 @@
// Define fading speed (higher = faster fading, range: 0.001 - 0.2)
#define FADE_SPEED 0.05f
// Define how strong the push effect is when the mouse is clicked
#define PUSH_FORCE 3.0f
// Enable debug mode (set to 0 to disable debug output)
#define DEBUG 0
@@ -61,8 +58,9 @@ struct Walker {
* @brief Pushes the walker away from a given (mouse) position.
* @param mouseX X coordinate of the mouse.
* @param mouseY Y coordinate of the mouse.
* @param force The force applied to push the walker away.
*/
void pushAway(float mouseX, float mouseY) {
void pushAway(float mouseX, float mouseY, float force) {
float adjustedMouseY = HEIGHT - mouseY; // Adjust for coordinate system
float deltaX = x - mouseX;
@@ -75,9 +73,9 @@ struct Walker {
float unitX = deltaX / distance;
float unitY = deltaY / distance;
// Apply push force
x = std::clamp(x + unitX * PUSH_FORCE, 0.0f, static_cast<float>(WIDTH - 1));
y = std::clamp(y + unitY * PUSH_FORCE, 0.0f, static_cast<float>(HEIGHT - 1));
// Apply dynamic push force
x = std::clamp(x + unitX * force, 0.0f, static_cast<float>(WIDTH - 1));
y = std::clamp(y + unitY * force, 0.0f, static_cast<float>(HEIGHT - 1));
}
}
@@ -104,7 +102,8 @@ public:
RandomWalkerWindow(const std::string& title, int width, int height)
: ReWindow::RWindow(title, width, height),
walker(std::make_unique<Walker>()),
trailBuffer(WIDTH * HEIGHT, 1.0f) // Initialize trail buffer to white (1.0)
trailBuffer(WIDTH * HEIGHT, 1.0f), // Initialize trail buffer to white (1.0)
pushForce(3.0f) // Default push force
{}
/**
@@ -135,7 +134,7 @@ public:
// Move the walker
walker->step();
// Update the trail buffer with the walker's current position
// Update the trail buffer with the walkers current position
plotWalker();
// Render the updated trail
@@ -148,6 +147,7 @@ public:
private:
std::unique_ptr<Walker> walker;
std::vector<float> trailBuffer; // Stores grayscale intensity of pixels (1.0 = white, 0.0 = black)
float pushForce; // Variable push force that can be adjusted in real time
/**
* @brief Handles mouse input to push the walker away.
@@ -156,22 +156,28 @@ private:
auto mousePos = GetMouseCoordinates();
if (IsMouseButtonDown(MouseButtons::Left)) {
walker->pushAway(mousePos.x, mousePos.y);
walker->pushAway(mousePos.x, mousePos.y, pushForce);
}
}
/**
* @brief Handles keyboard input to reset the walker.
* @brief Handles keyboard input to reset the walker and adjust push force.
*/
void handleKeyboardInput() {
if (IsKeyDown(Keys::Space)) {
walker->resetPosition();
}
// Close the window and quit if Escape is pressed
if (IsKeyDown(Keys::Escape)) {
Close(); // This will break the main event loop in main()
Close();
}
// Adjust push force using number keys 1-5
if (IsKeyDown(Keys::One)) pushForce = 1.0f;
if (IsKeyDown(Keys::Two)) pushForce = 2.0f;
if (IsKeyDown(Keys::Three)) pushForce = 3.0f;
if (IsKeyDown(Keys::Four)) pushForce = 4.0f;
if (IsKeyDown(Keys::Five)) pushForce = 5.0f;
}
/**
@@ -197,15 +203,12 @@ private:
int py = centerY + dy;
if (px >= 0 && px < WIDTH && py >= 0 && py < HEIGHT) {
trailBuffer[py * WIDTH + px] = 0.0f; // Set to black (no fade)
trailBuffer[py * WIDTH + px] = 0.0f;
}
}
}
}
/**
* @brief Renders the trail buffer to the screen using OpenGL.
*/
void drawTrail() {
std::vector<uint8_t> grayscale(WIDTH * HEIGHT);
for (size_t i = 0; i < trailBuffer.size(); ++i) {
@@ -215,9 +218,6 @@ private:
}
};
/**
* @brief Main function that creates and runs the ReWindow application.
*/
int main() {
auto window = std::make_unique<RandomWalkerWindow>("Random Walker with ReWindow", WIDTH, HEIGHT);
window->SetRenderer(RenderingAPI::OPENGL);