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 - Nortski

Pages: [1]
1
Graphics / Re: How to correctly draw a triangle fan?
« on: May 19, 2024, 12:42:34 pm »
If the triangle solution works, you must check your formation of the triangle strip to see if it's correct, including infinitely thin or 'backwards' triangles (breaking the triangle strip shape).
It might be worth making the calculations using doubles - for more accuracy - and then only converting to floats when creating the graphics (triangle strip).

Thank you for taking the time to help me with this.

I actually fixed the problem. As it turned out it was an off by one problem with my loop. A triangle fan needs to be 2 elements larger than the number of vertex points in my vector, 1 for the central position and 1 to close the gap between the last vertex and the first vertex.

// Make polygon 2 larger than vertex container
sf::VertexArray visibilityPolygon(sf::TriangleFan, vectorAngleContainer.size() + 2);
visibilityPolygon[0] = ray[0].position;
visibilityPolygon[0].color = sf::Color::Red;

for (int i = 1; i <= vectorAngleContainer.size(); i++)
{
    visibilityPolygon[i] = sf::Vertex(sf::Vector2f(vectorAngleContainer[i - 1].position.x, vectorAngleContainer[i - 1].position.y), sf::Color::Red);
}

// Set the last point == to the first vertex [1] (not [0] as that is the central point)
visibilityPolygon[vectorAngleContainer.size() + 1] = visibilityPolygon[1].position;
visibilityPolygon[vectorAngleContainer.size() + 1].color = sf::Color::Red;

2
Graphics / Re: How to correctly draw a triangle fan?
« on: May 19, 2024, 11:38:23 am »
Ok, I will try your suggestions.
It's weird though as it only happens on the right side of that shape and nowhere else.

3
Graphics / Re: How to correctly draw a triangle fan?
« on: May 18, 2024, 11:55:50 am »
The safest bet, I would say, is to use separate triangles. The formation of the triangles does not matter so if they overlap or 'go backwards', it's fine since each triangle doesn't affect any other triangle.

I may have to try this just to see if it works. However; I will need to find a solution using a triangle fan as this was specifically instructed in the course I'm following.

4
Graphics / [SOLVED] How to correctly draw a triangle fan?
« on: May 17, 2024, 07:49:40 pm »
So so close to getting my visibility polygon working. I'm using an SFML triangle fan with all the vertex points sorted by angle size with position 0 set to the mouse coords and the last element set to element 2, I needed to do this to fill in the gap for the last triangle.
However; a triangle appears to be missing at certain locations, see video.
I suspect that I'm having an out by one issue with my loop. I've tried rearranging a few numbers but this is the closest I can get.
https://streamable.com/d46yih

sf::VertexArray visibilityPolygon(sf::TriangleFan, vectorAngleContainer.size() + 1);
visibilityPolygon[0] = ray[0].position;

for (int i = 1; i <= vectorAngleContainer.size(); i++)
{
    visibilityPolygon[i] = sf::Vertex(sf::Vector2f(vectorAngleContainer[i-1].position.x, vectorAngleContainer[i-1].position.y), sf::Color::Red);
}
visibilityPolygon[vectorAngleContainer.size()] = visibilityPolygon[1].position;

vectorAngleContainer() is a vector of structs that holds a point and and angle.

5
General / Re: AABB collision resolution not working as expected
« on: April 29, 2024, 06:16:31 pm »
Oh my word! Thank you internet stranger for giving my a kick up the backside to use the debugger for once in my life. Turns out I was only ever updating the players previous position whereas all other enitites had defaulted to 0 as per the constructor.

What an idiot #face palm#

6
General / Re: AABB collision resolution not working as expected
« on: April 29, 2024, 06:02:29 pm »
I recommend getting out the debugger and stepping through the collision resolution code, so see what values you're working on and where the values aren't as expected.

It appears that the prevPos.x and PrevPos.y components of the entity objects are returning 0.0. I don't know why as they update correctly in the movement system.

7
General / AABB collision resolution not working as expected
« on: April 28, 2024, 04:19:39 pm »
I am a beginner trying to implement some AABB collision detection. I'm following along with the excellent COMP4300 Game Programming lecture series on Youtube by Professor Dave Churchill but I'm now stuck.

After watching his lecture on collision detection and resolution I believe I have implemented everything as instructed. The collision detection works fine but the resolution of detecting how much overlap there is and pushing back the sprite by the amount of overlap is not working as expected.

As you can see in the linked video, the resolution only works against the tiles at the very top of the screen. Note that I'm only testing for horizontal collision.

According to the lecture I had to create two helper functions: getOverlap and getPreviousOverlap, which is just a copy and paste of getOverlap but uses the entity's previous frame's position.

If getOverlap x and y are both greater than 0 then there's a collision. This part works.

To see if it was horizontal collision I use the formula if(prevOverlap.y > 0) {}

However; apart from the tiles at the top, this condition is not being met.

Link to demo video https://streamable.com/mj9o61

// Collision system in Scene_Play.cpp
void Scene_Play::sCollision()
{
    auto& playerTransform = m_player->getComponent<CTransform>();
    auto& playerBoundingBox = m_player->getComponent<CBoundingBox>();
    auto& playerState = m_player->getComponent<CState>().state;

    for (auto& e : m_entityManager.getEntities("tile"))
    {
        Vec2 overlap = Physics::getOverlap(m_player, e);
        Vec2 prevOverlap = Physics::getPreviousOverlap(m_player, e);
        auto& entityTransform = e->getComponent<CTransform>();

        if(overlap.x > 0 && overlap.y > 0)
        {
            std::cout << "Collision detected" << std::endl;          
           
            if (prevOverlap.y > 0) {

                std::cout << "Horizontal collision" << std::endl;
                // Calcualte if from left or right collision occurred
                playerTransform.pos.x = (playerTransform.pos.x < entityTransform.pos.x) ? playerTransform.pos.x -= overlap.x : playerTransform.pos.x += overlap.x;
            }
        }
    }
}

//Helper functions in Physics.cpp
Vec2 Physics::getOverlap(std::shared_ptr<Entity> player, std::shared_ptr<Entity> entity)
{
    auto& playerTransform = player->getComponent<CTransform>();
    auto& playerBoundingBox = player->getComponent<CBoundingBox>();
    auto& entityTransform = entity->getComponent<CTransform>();
    auto& entityBoundingBox = entity->getComponent<CBoundingBox>();

    Vec2 delta = { abs(entityTransform.pos.x - playerTransform.pos.x), abs(entityTransform.pos.y - playerTransform.pos.y) };

    float ox = (entityBoundingBox.halfSize.x + playerBoundingBox.halfSize.x) - delta.x;
    float oy = (entityBoundingBox.halfSize.y + playerBoundingBox.halfSize.y) - delta.y;

    if (ox > 0 && oy > 0) return Vec2(ox, oy);

    return Vec2(0.0f, 0.0f);
}

Vec2 Physics::getPreviousOverlap(std::shared_ptr<Entity> player, std::shared_ptr<Entity> entity)
{
    auto& playerTransform = player->getComponent<CTransform>();
    auto& playerBoundingBox = player->getComponent<CBoundingBox>();
    auto& entityTransform = entity->getComponent<CTransform>();
    auto& entityBoundingBox = entity->getComponent<CBoundingBox>();

    Vec2 delta = { abs(entityTransform.prevPos.x - playerTransform.prevPos.x), abs(entityTransform.prevPos.y - playerTransform.prevPos.y) };

    float ox = (entityBoundingBox.halfSize.x + playerBoundingBox.halfSize.x) - delta.x;
    float oy = (entityBoundingBox.halfSize.y + playerBoundingBox.halfSize.y) - delta.y;

    if (ox > 0 && oy > 0) return Vec2(ox, oy);

    return Vec2(0.0f, 0.0f);
}
 

Pages: [1]