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.
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
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;
}
// 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