Witam,
Staram się stworzyć prostą platformówke 2d w SFML coś jak Mario. Klasa Player do kolizji używa sf::RectangleShape, stworzyłem Tilemap wszystko się wyświetla jak powinno, Player colliduje z podlożem(1) i nie wylatuje poza mape. Moim problemem jest kolizja z Tilem - 2 czyli jezeli Playerem wejde w "2" z lewej lub prawej strony to sie zatrzymuje jezeli "wskocze" to na nim "stoje" :D coś takiego.
Wrzucam kod do Tilemap.h i Tilem.cpp
#pragma once
#include <SFML/Graphics.hpp>
#include <iostream>
#include "Player.h"
#include "Enemy.h"
using namespace std;
using namespace sf;
class newTiled{
public:
newTiled(void);
~newTiled(void);
void draw(RenderWindow &);
void Update(Player &player);
private:
int map_width = 42;
int map_height = 12;
Texture tileset_texture;
Sprite tileset_sprite;
Texture bg_txt;
Sprite bg_sprite;
int level[42 * 12] ={
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};
};
#include "newTiled.h"
#include <cmath>
using namespace sf;
using namespace std;
newTiled::newTiled(){
if(!tileset_texture.loadFromFile("assets/images/tajlmap.png"))
return;
if(!bg_txt.loadFromFile("assets/images/bg2.png"))
return;
tileset_sprite.setTexture(tileset_texture);
bg_sprite.setTexture(bg_txt);
}
newTiled::~newTiled(){
}
void newTiled::draw(RenderWindow &window){
window.draw(bg_sprite);
for(int y = 0; y < map_height; ++y){
for(int x = 0; x < map_width; ++x){
int tilecount = x + y * map_width;
tileset_sprite.setPosition(x * 64, y * 64);
tileset_sprite.setTextureRect(IntRect(level[tilecount] * 64, 0, 64, 64));
window.draw(tileset_sprite);
}
}
}
void newTiled::Update(Player &player){
int playerTileX = floor((int)(player.getRect().getPosition().x / 64));
int playerTileY = floor((int)(player.getRect().getPosition().y / 64));
int tilecount = playerTileX + playerTileY * map_width;
if (level[tilecount] == 1){
player.m_onGround = true;
}else {
player.m_onGround = false;
}
if (level[tilecount] == 2){
//kolizja .....
}
}