Yes hello all. I’m having a problems with my collisions in my demo I’m making. Sometimes the player collides with objects and sometimes they pass right through also they get stuck on side of objects, or reverses it’s movement.I hope someone can put code in ide and tell me what’s going on. I’m a beginner and I’ve tried all I know. I don’t know if it’s because I’m using different sizes for the tile map and players, or what’s going on. I want player to not move when they walks into gray box any help is appreciated. Thanks.
player
#include<iostream>
#include<raylib.h>
struct AnimData
{
Rectangle rec;
Vector2 pos;
int frame;
float updateTime;
float runningTime;
};
const int sWidth = 10;
const int sHeigth = 7;
int main()
{
InitWindow(800, 400, "My first game");
Rectangle rec[sWidth][sHeigth];
int type[sWidth][sHeigth]
{
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,1,0,0,1,0},
{1,0,0,0,0,0,0,0,0,0},
{1,0,0,0,0,0,0,0,0,0},
{1,0,0,0,0,0,0,0,0,0},
{1,0,0,0,0,0,0,0,0,0},
{1,0,0,0,0,0,0,0,0,0}
};
for (int i = 0; i < sHeigth; i++)
{
for (int j = 0; j < sWidth; j++)
{
Rectangle floor{ i * 32, j * 32, 32, 32 };
rec[i][j] = floor;
}
}
int playervec = 128;
bool UP = false, DOWN = false, LEFT = false, RIGHT = false, ISMOVINGu = false, ISMOVINGd = false,
ISMOVINGl = false, ISMOVINGr = false;
Image nebula = LoadImage("Cat 01-1.png");
Texture2D tex = LoadTextureFromImage(nebula);
AnimData nebData{
{200.0, 125.0, nebula.width / 3, nebula.height / 4}, // Rectangle rec
{200, 125}, // Vector2 pos
0, // int frame
1.0f / 9.0f, // float updateTime
0 // float runningTime
};
bool isShoot = false;
Rectangle rec2{ nebData.pos.x ,nebData.pos.y,4.f,4.f };
Vector2 pos{ nebData.pos.x + 0.0f, nebData.pos.y + 0.0f };
Vector2 off{ 800 / 2.0f, 400 / 2.0f };
Camera2D camera = { 0 };
camera.target = pos;
camera.offset = off;
camera.rotation = 0.0f;
camera.zoom = 1.0f;
SetTargetFPS(60);
while (!WindowShouldClose())
{
const float dt = GetFrameTime();
BeginDrawing();
if (IsKeyDown(KEY_W))
{
UP = true;
ISMOVINGu = true;
}
if (IsKeyUp(KEY_W))
ISMOVINGu = false;
if (IsKeyDown(KEY_S))
{
DOWN = true;
ISMOVINGd = true;
}
if (IsKeyUp(KEY_S))
{
ISMOVINGd = false;
}
if (IsKeyDown(KEY_A))
{
LEFT = true;
ISMOVINGl = true;
}
if (IsKeyUp(KEY_A))
{
ISMOVINGl = false;
}
if (IsKeyDown(KEY_D))
{
RIGHT = true;
ISMOVINGr = true;
}
if (IsKeyUp(KEY_D))
ISMOVINGr = false;
if (!ISMOVINGd && !ISMOVINGu && !ISMOVINGl && !ISMOVINGr)
nebData.rec.x = 32;
if (UP)
{
nebData.pos.y -= playervec * dt;
nebData.rec.y = 96;
Rectangle rec1{ nebData.pos.x, nebData.pos.y,nebula.width/3,nebula.height/4};
bool coll = false;
for (int i = 0; i < sWidth; i++)
{
for (int j = 0; j < sHeigth; j++)
if (CheckCollisionRecs(rec1, rec[i][j]))
{
if (type[i][j] == 1)
{
coll = true;
break;
}
}
}
if(coll)
nebData.pos.y += playervec * dt;
UP = false;
camera.target = { nebData.pos.x+20, nebData.pos.y+20 };
}
if (DOWN)
{
nebData.pos.y += playervec * dt;
nebData.rec.y = 0;
Rectangle rec1{ nebData.pos.x , nebData.pos.y,nebula.width / 3,nebula.height / 4};
bool coll = false;
for (int i = 0; i < sWidth; i++)
for (int j = 0; j < sHeigth; j++)
if (CheckCollisionRecs(rec1, rec[i][j]))
{
if (type[i][j] == 1)
{
coll = true;
break;
}
}
if(coll)
nebData.pos.y -= playervec * dt;
DOWN = false;
camera.target = { nebData.pos.x + 20.0f, nebData.pos.y + 20.0f };
}
if (LEFT)
{
nebData.rec.y = 32;
Rectangle rec1{ nebData.pos.x, nebData.pos.y,nebula.width / 3,nebula.height / 4 };
nebData.pos.x -= playervec * dt;
bool coll = false;
for (int i = 0; i < sWidth; i++)
for (int j = 0; j < sHeigth; j++)
if (CheckCollisionRecs(rec1, rec[i][j]))
{
if (type[i][j] == 1)
{
coll = true;
break;
}
}
if(coll)
nebData.pos.x += playervec * dt;
LEFT = false;
camera.target = { nebData.pos.x + 20.0f, nebData.pos.y + 20.0f };
}
if (RIGHT)
{
nebData.pos.x += playervec * dt;
nebData.rec.y = 64;
Rectangle rec1{ nebData.pos.x, nebData.pos.y,nebula.width / 3,nebula.height / 4 };
bool is = false;
for (int i = 0; i < sWidth; i++) {
for (int j = 0; j < sHeigth; j++)
if (CheckCollisionRecs(rec1, rec[i][j]))
{
if (type[i][j] == 1)
{
is = true;
break;
}
}
}
if(is)
nebData.pos.x -= playervec * dt;
RIGHT = false;
camera.target = { nebData.pos.x + 20.0f, nebData.pos.y + 20.0f };
}
if (IsKeyPressed(KEY_SPACE))
{
isShoot = true;
}
if (IsKeyReleased(KEY_SPACE))
{
//isShoot = false;
}
if (isShoot && rec2.x < 800)
rec2.x += 2.f;
else
{
isShoot = false;
}
nebData.runningTime += dt;
if (nebData.runningTime >= nebData.updateTime && (ISMOVINGu || ISMOVINGd || ISMOVINGl || ISMOVINGr))
{
nebData.runningTime = 0.0;
nebData.rec.x = nebData.frame * nebData.rec.width;
nebData.frame++;
if (nebData.frame > 4)
{
nebData.frame = 0;
}
}
BeginMode2D(camera);
ClearBackground(BLACK);
for (int i = 0; i< sWidth; i++)
for (int j = 0; j < sHeigth; j++)
{
if (type[i][j] == 1)
DrawRectangle((int)rec[i][j].x, (int)rec[i][j].y, (int)rec[i][j].width, (int)rec[i][j].height, GRAY);
else DrawRectangle((int)rec[i][j].x, (int)rec[i][j].y, (int)rec[i][j].width, (int)rec[i][j].height, DARKGREEN);
}
DrawTextureRec(tex, nebData.rec, nebData.pos, WHITE);
DrawRectangleLines(nebData.pos.x, nebData.pos.y, nebula.width / 3, nebula.height / 4, RED);
//DrawRectangle();
DrawText(TextFormat("x %f , y %f", nebData.pos.x, nebData.pos.y), nebData.pos.x -40, nebData.pos.y -40, 32,WHITE);
if (isShoot)
DrawRectangle(rec2.x, rec2.y, 4.f, 4.f, BLUE);
EndDrawing();
EndMode2D();
}
CloseWindow();
//std::cout<<"Hello, Worldn";
return 0;
}
I’ve tried to change player’s vel variable I’ve tried to add rectangle to player to see if collisions were correct.
Chr2she is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.