首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Raylib中跟踪带有像素的路径?

如何在Raylib中跟踪带有像素的路径?
EN

Stack Overflow用户
提问于 2022-09-08 03:58:54
回答 1查看 60关注 0票数 3

我正在用Raylib制作一个双摆模拟器,在深入了解它的工作原理之前,我想完成它的图形创建。

到目前为止,我创建了一个窗口,制作了一个双摆,可以任意旋转摆,但在研究物理之前,我最不需要做的事情是跟踪它的路径,我使用RenderTexture2D变量使用纹理来实现它。

然而,当这样做时,它确实跟踪路径,但只跟踪整个窗口的1/4,我不知道为什么。

它很可能与rltranslatef有关,但我不知道如何修复它。我有一个pendulum.h & .cpp文件,但它们与为此绘制图形无关。任何帮助都是非常感谢的。

摆:

代码语言:javascript
复制
#ifndef DOUBLE_PENDULUM_SIM_PENDULUM_H
#define DOUBLE_PENDULUM_SIM_PENDULUM_H


class pendulum {
public:

    float pLength{};
    float pMass{};
    float x{};
    float y{};
    float pAngle{};

public:

    [[nodiscard]] float getX() const ;
    [[nodiscard]] float getY() const ;

    [[nodiscard]] float getAngle() const ;

    void setX(float length, float angle);
    void setY(float length, float angle);

    void setLength(float length);
    void setMass(float mass);
    void setAngle(float angle);

    pendulum();

    pendulum(float length, float mass, float angle) : pLength(length), pMass(mass), pAngle(angle) {}

    ~pendulum();


};

#endif //DOUBLE_PENDULUM_SIM_PENDULUM_H

pendulum.cpp:

代码语言:javascript
复制
#include "includes/pendulum.h"
#include <cmath>
#include <iostream>
#include <raylib.h>

void pendulum::setX(float length, float angle) {
    x = length * sin(angle);
}

void pendulum::setY(float length, float angle) {
    y = length * cos(angle);
}

float pendulum::getX() const {
    return x;
}

float pendulum::getY() const {
    return y;
}

void pendulum::setLength(float length) {
    pLength = length;
}

void pendulum::setMass(float mass) {
    pMass = mass;
}

float pendulum::getAngle() const {
    return pAngle;
}

pendulum::~pendulum() {
    std::cout << "\nPendulum destroyed" << std::endl;
}

void pendulum::setAngle(float angle) {
    pAngle = angle * DEG2RAD;
}

//Default constructor
pendulum::pendulum() = default;

绘制主要图形的main.cpp:

代码语言:javascript
复制
#include <iostream>
#include "raylib.h"
#include "includes/pendulum.h"
#include <rlgl.h>
#include <cmath>

void _testPendulum();

pendulum pen1;
pendulum pen2;


int main() {


    //Prompt to make the initial values themselves
    float uLength1, uLength2, uMass1, uMass2, uAngle1, uAngle2;

    try {

        std::cout << "Please choose the length of each pendulum, starting with Pendulum 1, then Pendulum 2. Each value provided can be up to 7 decimal digits, " << "\n" << "length MUST BE greater than 50 and less than 200" << "\n";
        std::cin >> uLength1 >> uLength2;
        std::cout << "Please choose the mass of each pendulum, starting with Pendulum 1, then Pendulum 2. Each value provided can be up to 7 decimal digits, " << "\n" << "mass MUST BE greater than 20 and less than 100" << "\n";
        std::cin >> uMass1 >> uMass2;
        std::cout << "Please choose the starting angle of each pendulum, starting with Pendulum 1, then Pendulum 2. Each value provided can be up to 7 decimal digits" << "\n";
        std::cin >> uAngle1 >> uAngle2;

    } catch (const std::exception & e) {
        std::cout << e.what();
    }

    //Pendulum 1 settings

    pen1.setMass(uMass1);
    pen1.setLength(uLength1);
    pen1.setAngle(uAngle1);
    pen1.setX(pen1.pLength,pen1.getAngle());
    pen1.setY(pen1.pLength, pen1.getAngle());

    std::cout << "X coord: " << pen1.getX() << " Y coord: " << pen1.getY() << std::endl;

    //Pendulum 2 settings

    pen2.setMass(uMass2);
    pen2.setLength(uLength2);
    pen2.setAngle(uAngle2); //Can only set this once and cant anywhere else, why?
    pen2.setX( pen2.pLength,pen2.getAngle());
    pen2.setY( pen2.pLength,pen2.getAngle());
    pen2.x = pen1.getX() + pen2.getX();
    pen2.y = pen1.getY() + pen2.getY();

    std::cout << "X coord: " << pen2.getX() << " Y coord: " << pen2.getY() << std::endl;

    Vector2 origin{0,0};

    const double screenWidth = 1440;
    const double screenHeight = 1080;


    InitWindow((int) screenWidth, (int) screenHeight, "Double-Pendulum-Sim");

    int frameCounter = 0;

    SetTargetFPS(60);

    float px1 = pen1.getX();
    float py1 = pen1.getY();

    float px2 = pen2.getX();
    float py2 = pen2.getY();

    RenderTexture2D target = LoadRenderTexture((int) screenWidth, (int) screenHeight);

    BeginTextureMode(target);
    ClearBackground(RAYWHITE);
    EndTextureMode();

    while (!WindowShouldClose()) {


        Vector2 rod1{px1,py1};
        Vector2 rod2 {px2, py2};

        /**------------------Update------------------*/

            frameCounter++;
            uAngle1 += 1.0f;
            pen1.setAngle(uAngle1); //Can only set this once and cant anywhere else, why?
            pen1.setX(pen1.pLength,pen1.getAngle());
            pen1.setY(pen1.pLength, pen1.getAngle());

            px1 = pen1.getX();
            py1 = pen1.getY();

            uAngle2 -= 1.0f;
            pen2.setAngle(uAngle2); //Can only set this once and cant anywhere else, why?
            pen2.setX( pen2.pLength,pen2.getAngle());
            pen2.setY( pen2.pLength,pen2.getAngle());
            pen2.x = pen1.getX() + pen2.getX();
            pen2.y = pen1.getY() + pen2.getY();

            px2 = pen2.getX();
            py2 = pen2.getY();

            std::cout << frameCounter << std::endl;


    /**---------------------------------Draw-Pendulums & Path---------------------------------- */
    //TODO: Get pathing to work

    BeginDrawing();


            BeginTextureMode(target);
            DrawPixelV(rod2, RED);
            EndTextureMode();

            rlTranslatef((float) screenWidth/2,(float) screenHeight/4,0);

            DrawTextureRec(target.texture, (Rectangle){0,0, (float) target.texture.width, (float) -target.texture.height}, (Vector2){0,0}, WHITE);

            ClearBackground(RAYWHITE);

            DrawFPS(-350, -200);


            DrawLineEx(origin, rod1, 5.0f, BLACK);
            DrawCircle( px1,py1,pen1.pMass,BLACK);

            DrawLineEx(rod1, rod2, 5.0f, BLACK);
            DrawCircle(px2,py2,pen2.pMass,BLACK);


            std::cout << "Pendulum 1 X & Y: " << pen1.getX() << " " << pen1.getY() << std::endl;
            std::cout << "Pendulum 2 X & Y: " << pen2.getX() << " " << pen2.getY() << std::endl;

    EndDrawing();

    }

    CloseWindow();

    return 0;
}

//Test function
void _testPendulum() {

    try {
        pen1.setMass(20.0f);
        pen1.setLength(150.0f);
        pen1.setAngle(0.0f);
        pen1.setX(pen1.pLength,pen1.getAngle());
        pen1.setY(pen1.pLength, pen1.getAngle());

        std::cout << "X coord: " << pen1.getX() << " Y coord: " << pen1.getY() << std::endl;

        pen2.setMass(50.0f);
        pen2.setLength(150.0f);
        pen2.setAngle(0.0f);
        pen2.setX( pen2.pLength,pen2.getAngle());
        pen2.setY( pen2.pLength,pen2.getAngle());
        pen2.x = pen1.getX() + pen2.getX();
        pen2.y = pen1.getY() + pen2.getY();


        std::cout << "X coord: " << pen2.getX() << " Y coord: " << pen2.getY() << std::endl;

    } catch (const std::exception & e) {
        std::cout << e.what();
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-08 07:32:27

您的问题来自于DrawPixelV(rod2, RED);,它无法写出位置为负值的位置(尝试不使用rlTranslatef来查看这个想法)。

解决方案:

  • 不使用以屏幕中心为中心的originpx1py1px2py2 (不是0,0)。

main.cpp中,我更改了这些行,问题就消失了。

代码语言:javascript
复制
    Vector2 origin{700,200}; //  was origin{0,0};
...

    float px1 = pen1.getX() + 700; // +700 was not here
    float py1 = pen1.getY() + 200; // +200 was not here
    float px2 = pen2.getX() + 700;
    float py2 = pen2.getY() + 200;

...
    px1 = pen1.getX() + 700;
    py1 = pen1.getY() + 200;
...
    px2 = pen2.getX() + 700;
    py2 = pen2.getY() + 200;
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73643579

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档