1

So I'm new to raylib and, basically, I'm trying to make a sandbox game and I am trying to make it so that when the player places a square or material when that material hits the edge of the screen it stops. Currently, my square when it falls it goes to the edge of the screen and it stops. but there's noticeable space between the screen, and the squares flicker and the squares don't stop on the same X level.

This Code is called when the user clicks on the screen. This DrawMat function is called when the user clicks and from there the square falls to the bottom of the screen.

Heres My Code

struct Mat
{
    
    float X, Y;
    float SpeedX, SpeedY;
    float Force;
    float Gravity;
    Vector2 MousePos;
    Vector2 size;
    Color color;
    void DrawMat() {
        
        DrawRectangle(MousePos.x, MousePos.y, size.x, size.y, color);
        MousePos.y += 9.81f;

        if (MousePos.x < 0 || MousePos.x > GetScreenWidth()) {
            MousePos.x *= -1;

        }

        if (MousePos.y < 0 || MousePos.y > GetScreenHeight()) {
            MousePos.y *= -1;

        }
    }

    
};
int main() {

InitWindow(800, 600, "Speed Z Presends to you... Satisfiying, Amazing, Niffty, Dreamy. SIMULATOR");

SetWindowState(FLAG_VSYNC_HINT);



Mat Sand;
    
    Sand.MousePos = { -100, -100 };
    Sand.size = { 5, 5 };
    Sand.color = YELLOW;
    Sand.X = Sand.MousePos.x;
    Sand.Y = Sand.MousePos.y;
    Sand.SpeedX = 300;
    Sand.SpeedY = 500;


Vector2 Mousepos = {-100, -100};
    bool Mouseclicked = false;
    Vector2 RectSize = { 2, 2 };
    int Numof = 0;
    std::vector<Mat> positions = {};

while (!WindowShouldClose())
    {
        
        
        
        
        BeginDrawing();



        ClearBackground(BLACK);
    
        for (size_t i = 0; i < positions.size(); i++)
        {
            positions[i].DrawMat();
        }




            if (IsKeyPressed(KEY_ONE)) {
                Numof = 1;
                
            }

            if (Numof == 1)
            {
                if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
                {
                    Mouseclicked = true;
                    Mousepos = GetMousePosition();
                    Sand.MousePos = GetMousePosition();
                    positions.push_back(Sand);

                }
                
            }

Here is an image of What I mean:

1

GreenDev
  • 13
  • 7

1 Answers1

0

I don't really understand how this code would work at all.

But if what you mean is that the Y coordinate should be as close as possible to the edge, your if statement should be like so:

    if (MousePos.y < 0) {
        MousePos.y *= -1;
    }
    else if (MousePos.y > GetScreenHeight()) {
        MousePos.y = GetScreenHeight();
    }

Adjust the last expression as required. Maybe you need to subtract the height of the object so it doesn't disappear:

    else if (MousePos.y > GetScreenHeight() - size.y) {
        MousePos.y = GetScreenHeight() - size.y;
    }

Why is that necessary?

I would imagine that the rendering you make uses MousePos.x as the left position and MousePos.y as the top position like so:

              /---- this corner is (MousePos.x, MousePos.y)
              |
|             v                                  |
|             +----------------+----             |
|             |                |  ^              |<- screen bottom edge
|             |                |  | size.y       |
+-------------|----------------|--|--------------+
              |                |  v
              +----------------+----

So to keep the sand grain on screen, you must make sure that the corner is at a location that makes it visible. As we see on the ASCII picture, for Y it means the maximum is GetScreenHeight() - size.y.

Note that you certainly have the same issue with the X coordinate (i.e. you may want the maximum to be GetScreenWidth() - size.x).

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
  • Hey so I used your code and it doesn't work since when I place a square and it falls to the bottom of the screen and doesn't stop it just keeps going. Are you confused on the mousePos part bc in that drawmat function we just update the mouse pos = to 9.81 bc that's what makes the squares fall when they are placed basically that 9.81 is gravity and were updating it.The MousePos.x and the mousepos.y are the mousepos .Would you like to see my entire Code for more clarity? – GreenDev Jun 20 '22 at 17:04
  • @GreenDev Yes. In general, the more you can share, the more likely we can understand what you are trying to do and find a solution for you. – Alexis Wilke Jun 20 '22 at 17:19
  • Hey so i edited my question with more of the code can you understand it more now? – GreenDev Jun 20 '22 at 17:22
  • Okay, so my code is close. Are you sure that it _keeps failing_? because it looks to me that it could be right under the bottom, hence the `GetScreenHeight() - size.y;` instead of using `GetScreenHeight()`. You could also change the limit. I've updated my answer accordingly. – Alexis Wilke Jun 20 '22 at 17:31
  • 1
    Hey, thanks for the help it works now!! I've been stuck on this problem for days and now it works.I would have never thought of that! But can you pls explain the - size.y part in the else if statement, please? Thanks Again! – GreenDev Jun 20 '22 at 17:36