Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - noct27

Pages: [1]
1
That was it, thanks!


2
Graphics / Quad and mouse click collisions are consistently 3 tiles off.
« on: December 23, 2022, 08:13:18 am »
Hello, i'm having problems clicking on individual tiles in a VertexArray populated with quads.

I followed this [tilemap](https://www.sfml-dev.org/tutorials/2.5/graphics-vertex-array.php) to build my tilemaps.

Here is the code I use to click on a tile.

 sf::Vector2f p = win->mapPixelToCoords(sf::Vector2i(sf::Mouse::getPosition().x, sf::Mouse::getPosition().y));
 int x_pos = p.x / 32;
 int y_pos = p.y / 32;
 if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)){
        tilemap->m_vertices[( (x_pos-3) + (y_pos-3) * 16) * 4].color = sf::Color::Green;
}

Where my tile size is `32x32` and the tilemap is `16` tiles long in the x and y direction. Without doing `x_pos-3` and `y_pos-3` the the wrong tile gets highlighted. Wondering why it's off by 3 tiles?

3
General / Collision detection, makes sprite move in opposite direction.
« on: August 28, 2022, 10:06:45 pm »
Hello,


I'm working on collision detection when a sprite hits a wall.


In my Zone.cpp class, where tilemaps are handled I have this block in the update function:


Code: [Select]

    for (auto& tileMap : renderAfterPlayerTM) {
        tileMap->tileMap->update(player, &movement, &this->frameItem, &zoneInfo, deltaTime);
        playerUpdate = movement.ghostMove(deltaTime, currentPlayerPosition, *tileMap->tileMap->getTilesGroupVector());
    }
    player->update(deltaTime, playerUpdate.newPosition, playerUpdate.playerPosition);
    for (auto& tileMap : renderBeforePlayerTM) {
        tileMap->tileMap->update(player, &movement, &this->frameItem, &zoneInfo, deltaTime);
        playerUpdate = movement.ghostMove(deltaTime, currentPlayerPosition, *tileMap->tileMap->getTilesGroupVector());


    }
 player->update(deltaTime, playerUpdate.newPosition, playerUpdate.playerPosition);


I have multiple tilemaps that render different type of objects, like things that should render/update before and after the player is.
The ghostMove function is where the sprites new position is formed:


Code: [Select]

MovePlayerResponse Movement::ghostMove(sf::Time deltaTime, sf::Vector2f playerPosition, std::vector<TileGroup*> tileGroupsVector) {
    MovePlayerResponse mpr;
    AnimatedSprite ghost;
    ghost.setPosition(playerPosition);
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) {
        ghost.move(sf::Vector2f((-1 * speed), 0.0f));
        LL::MapUpdateResponseObject collideResponse = LL::Collision::collide(ghost.getPosition(), tileGroupsVector);
        mpr.playerPosition = LL::PlayerPosition::Left;
        if (collideResponse.collided) {
            mpr.newPosition = sf::Vector2f(speed, 0.0f);
            mpr.tileNumber = collideResponse.tileNumber;
            return mpr;
        }
        mpr.newPosition = sf::Vector2f((-1 * speed), 0.0f);
    }
    else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) {
        ghost.move(sf::Vector2f((speed), 0.0f));
        LL::MapUpdateResponseObject collideResponse = LL::Collision::collide(sf::Vector2f(ghost.getPosition().x, ghost.getPosition().y), tileGroupsVector);
        mpr.playerPosition = LL::PlayerPosition::Right;
        if (collideResponse.collided) {
            mpr.newPosition = sf::Vector2f(-1 * (speed), 0.0f);
            mpr.tileNumber = collideResponse.tileNumber;
            return mpr;
        }
      mpr.newPosition = sf::Vector2f((speed), 0.0f);
    }
    mpr.tileNumber = -1;
    return mpr;
}


On every movement, I move the position to the desired position and if a collision occurs I move it back. The problem occurs when I hold down S (which goes down on the screen) and the sprite continues to collide with  the wall, and if I were to press D the sprite would move as if i'm pressing A because it's stuck in that collide block or the sprite would just not move at all, or continuously bounce off and on the wall.


Image for an idea of when this occurs, collision on a wall:


Any ideas on why this behavior occurs, or if I have some fundamental flaw in my design?


Pages: [1]
anything