friction2d / friction

Friction Graphics
https://friction.graphics
GNU General Public License v3.0
293 stars 14 forks source link

[Feature Request] Implement Camera System for Friction 2D #273

Closed Rodoxdexts closed 3 weeks ago

Rodoxdexts commented 3 weeks ago

Hello, I'm using ai it so I can be useful in some way. Being quite honest, i only know how to program in gdscript, so thanks for making this fork a good program! I asked AI to analyze this fork and write a solution, because it hasn't been implemented since https://github.com/friction2d/friction/issues/66 And also thanks for the consideration! I like your work.


Description:

I would like to propose the implementation of a camera system within the Friction 2D engine. This feature would enhance the capabilities of the engine, allowing users to create more dynamic and flexible 2D scenes through effective camera manipulation.


Proposed Implementation Steps

  1. Clone the Friction 2D Repository:

    First, clone the repository to your local machine:

    git clone https://github.com/friction2d/friction.git
    cd friction
  2. Understand the Project Structure:

    Before implementing the camera system, it’s essential to understand the existing architecture, particularly:

    • Rendering System: Identify files related to rendering and the rendering loop, such as Renderer.h and Renderer.cpp.
    • Scene Management: Locate files that handle scene objects and transformations, likely within the Scene or Entity directories.
    • Transformations: Review how objects are currently manipulated, focusing on translation, scaling, and rotation.
  3. Create the Camera Class:

    Introduce a Camera class to manage the following functionalities:

    • Position: Control where the camera is positioned in the 2D space.
    • Zoom: Allow zooming in and out of the scene.
    • Panning: Enable horizontal and vertical movement.
    • Rotation: Provide an optional feature to rotate the camera view.

    Here’s a basic implementation of the Camera class in C++:

    class Camera {
    public:
       glm::vec2 position; // Camera position
       float zoom;         // Zoom level
       float rotation;     // Rotation angle in degrees
    
       Camera() : position(0.0f, 0.0f), zoom(1.0f), rotation(0.0f) {}
    
       void setPosition(float x, float y) {
           position.x = x;
           position.y = y;
       }
    
       void move(float dx, float dy) {
           position.x += dx;
           position.y += dy;
       }
    
       void setZoom(float z) {
           zoom = z;
       }
    
       void zoomIn(float factor) {
           zoom *= factor;
       }
    
       void zoomOut(float factor) {
           zoom /= factor;
       }
    
       void setRotation(float angle) {
           rotation = angle;
       }
    };
  4. Integrate with the Rendering Pipeline:

    Modify the existing rendering code to utilize the Camera class. Adjust the transformation matrices in the rendering pipeline to consider the camera’s position, zoom, and rotation. For example:

    glm::mat4 view = glm::mat4(1.0f);
    
    // Apply camera transformations
    view = glm::translate(view, glm::vec3(-camera.position.x, -camera.position.y, 0.0f));
    view = glm::scale(view, glm::vec3(camera.zoom, camera.zoom, 1.0f));
    view = glm::rotate(view, glm::radians(camera.rotation), glm::vec3(0.0f, 0.0f, 1.0f));
    
    // Pass the view matrix to the shader or renderer
    shader.setMat4("view", view);
  5. Handle Input for Camera Control:

    Implement input handling to allow users to control the camera. This can be integrated into the existing input system of Friction 2D:

    void handleInput(Camera& camera) {
       // Pan with arrow keys
       if (isKeyPressed(KEY_RIGHT)) {
           camera.move(10.0f, 0.0f);
       }
       if (isKeyPressed(KEY_LEFT)) {
           camera.move(-10.0f, 0.0f);
       }
       if (isKeyPressed(KEY_UP)) {
           camera.move(0.0f, 10.0f);
       }
       if (isKeyPressed(KEY_DOWN)) {
           camera.move(0.0f, -10.0f);
       }
    
       // Zoom with +/- keys
       if (isKeyPressed(KEY_PLUS)) {
           camera.zoomIn(1.1f);
       }
       if (isKeyPressed(KEY_MINUS)) {
           camera.zoomOut(1.1f);
       }
    }
  6. Compile and Test:

    After integrating the camera functionality, compile the project and run tests to ensure that the camera behaves as expected:

    make
    ./friction2d
  7. Optional: Implement GUI Controls for Camera:

    For enhanced usability, consider implementing GUI controls using a library like ImGui to allow real-time camera adjustments through sliders or buttons:

    ImGui::Begin("Camera Controls");
    ImGui::SliderFloat2("Position", &camera.position.x, -1000.0f, 1000.0f);
    ImGui::SliderFloat("Zoom", &camera.zoom, 0.1f, 10.0f);
    ImGui::SliderFloat("Rotation", &camera.rotation, -360.0f, 360.0f);
    ImGui::End();
  8. Documentation:

    Provide comprehensive documentation on how to utilize the camera system within the engine, including setup instructions and example usage scenarios.


Distinction Between Core Feature and Plugin


Benefits of Implementing a Camera System:


Thank you for considering this feature request! I believe adding a camera system will significantly enhance the Friction 2D engine's capabilities and provide developers with powerful tools for their projects.


Feel free to modify any details to better suit your tone or any specific aspects you want to emphasize!

kaixoo commented 3 weeks ago

I'ts amazing the power of chatgpt 😮 All the code is basically wrong, but it gives an idea of what the code should look like xD. I believe camera support is currently out-of-scope, see #261

Rodoxdexts commented 3 weeks ago

Yes, unfortunately, so I propose a very cool suggestion: should I advertise this project so that people who see it will give financial support to this guy so he can improve this software?

rodlie commented 3 weeks ago

Duplicate of https://github.com/friction2d/friction/issues/261

Also, do not use LLM for math or code, it's trash. I not accept generated/stolen code.

btw, everything related to transforms etc uses skia.