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:
CodePudding user response:
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
).