Hi, I’m making a simple platformer for learning purposes and I’m currently trying to add a sprite to my player which is currently a square. I’ve managed to get the sprite part working and animated (it’s an idle animation that’s 11 frames), however, I noticed that when the player collides with a wall or something above them they don’t go flush with the surface they stop a few pixels away instead. I’m assuming it’s because the sprite is a bit smaller than the square which is what’s used for collision so that’s what’s causing the problem. Here’s a video to demonstrate what I mean https://imgur.com/a/UekynVT
Lastly here’s here’s some code related to the player sprite animation
Player::Player(SDL_Renderer* renderer) {
printf("Player Ctor\n");
position = Vector2(100, 100);
velocity = Vector2(0, 0);
size = Size(32 * scale, 32 * scale);
SDL_Surface* temp = IMG_Load("assets/Spritesheet/Ninja Frog/Idle (32x32).png"); // Load the sprite sheet.
if (temp == nullptr) {
std::cerr << "Failed to load sprite sheet: " << IMG_GetError() << std::endl;
exit(1);
}
spriteSheet = SDL_CreateTextureFromSurface(renderer, temp);
SDL_FreeSurface(temp);
}
void Player::draw(SDL_Renderer* renderer) {
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_Rect rect = { position.getX(), position.getY(), size.getWidth(), size.getHeight() };
SDL_RenderFillRect(renderer, &rect);
SDL_Rect srcRect = { currentFrame * frameWidth, 0, frameWidth, frameHeight };
SDL_Rect dstRect = { static_cast<int>(position.getX()), static_cast<int>(position.getY()), frameWidth * scale, frameHeight * scale };
SDL_RenderCopy(renderer, spriteSheet, &srcRect, &dstRect);
if (SDL_GetTicks() - lastFrameTime >= frameDelay) {
currentFrame++;
if (currentFrame >= totalFrames) {
currentFrame = 0;
}
lastFrameTime = SDL_GetTicks();
}
}