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

Pages: [1]
1
General / Implementing "place_empty" function in C++ from GameMaker
« on: May 16, 2021, 11:53:35 am »
Hi, recently I started writing a 2D game in C++ with SFML but now I'm stuck in the collision mechanism. I want to program the entities in a way so that every time Update() is called I check whether they are touching with solid objects and determine how they should move (up/down left/right).

In case you didn't know, in GameMaker (legacy versions) there are two functions, place_empty(x, y) which checks if there is a collision box in the given coordinates and position_empty(x, y) which checks if the calling object interacts with another object in the given coordinates.

bool PositionFree(sf::Vector2f Position, bool Solid) {
    // GetEntities() returns a pointer to this type -> std::vector<CEntity*>
    for (auto Entity : GetEntities()) {
        //printf("EntPosX = %.1f, EntSizeX = %.1f, PosX = %.1f\n", Entity->GetPosition().x, Entity->GetSize().x, Position.x);
        //printf("EntPosY = %.1f, EntSizeY = %.1f, PosY = %.1f\n\n", Entity->GetPosition().y, Entity->GetSize().y, Position.y);
       
        if (Entity->IsSolid() != Solid) {
            continue;
        }
       
        if (Position.y > Entity->GetPosition().y && Position.y < Entity->GetPosition().y + Entity->GetSize().y) {
            if (Position.x < Entity->GetPosition().x) {
                return true;
            }
        }
    }
    return true;
}

I have this utility function but it doesn't work as expected. The vertical Y position works fine (I guess?) but the horizontal X line does not. Can you help me rewrite it correctly? :)

- regards, fancode992

2
General / Re: Getting intersection direction/side.
« on: December 17, 2019, 05:57:42 pm »
you should use sf::Sprite::getGlobalbounds() instead of the local to test for intersects.
you may try manifold for detecting the collision from every directions up/down or left/right, here an excellent tutorial in this topic with examples:

http://trederia.blogspot.com/2016/02/2d-physics-101-pong.html
Hello Mortal, thank you for helping me :).

3
General / Getting intersection direction/side.
« on: December 15, 2019, 08:11:33 pm »
Greetings,
As the title says, how can I get the side a sprite collides with another one? For example, I have an enumeration called "Direction" which includes north, south, west and east, a function shall return only one.

I am currently checking collision between two sprites via this code:
spritevar.getLocalBounds().intersects(spritevar2)

I am sorry for being so undescriptive. Thank you. :)

Pages: [1]
anything