Quantcast
Channel: GameDev.net
Viewing all articles
Browse latest Browse all 17825

Control flow based on type in C++

$
0
0
Hi guys, I'm not quite sure how to achieve this Here what I got so far, my GameMode class own an object of type Physics: class Physics { private://variables GameMode* Game; public://constructors Physics(GameMode* gameRef); ~Physics(); public://methods bool IsColliding(Entity* current, ECollisionTest collisionTestType); private://methods bool BoxCollisionTest(const Rect& current, const Rect& other); }; And since all my entities have a pointer to the GameMode they can retrieve a pointer to the Physics object, so when they move they can query the Physics object to see if they collided with something, passing "this" as the object to control against all the Entities inside GameMode, like this: //Collision Check if (PhysicsManager->IsColliding(this, ECollisionTest::EBoxCollisionTest)) { LogConsole("Collision"); } and IsColliding() goes like this: bool Physics::IsColliding(Entity* current, ECollisionTest collisionTestType) { for (auto& E : Game->Entities) { if (current != E.get())//avoid self testing { switch (collisionTestType) { case ECollisionTest::EBoxCollisionTest: { if (BoxCollisionTest(current->GetCollisionBox(), E->GetCollisionBox())) { return true; } } break; } } } return false; } Now, there are two things I don't like about my current setup. 1) I am testing against all the entities. The paddle of the Breakout game can only move left and right, and hit either a ball or the left/right boundary, makes no sense to test it against 50 bricks at every update. 2)From inside the Paddle class I am calling "IsColliding(this, ECollisionTest::EBoxCollisionTest)" so there is the assumption that all the entities that can collide with this one require a box test. May not be the case, maybe I have a "worm" entity running on the screen and it requires another type of collision test, so the choosen function to run shouldn't relying on me explicitly saying which one is, but it should be called at runtime trough overload, based on the type of Entity being compared. So maybe the Paddle<->Ball comparison would do a BoxCollisionTest, while Paddle<->Worm comparison would do something else. So how do I get it to work the way I want? This is what I want: 1)Physics object is working with Entity*, and yet I need it to being able to distinguish between a Paddle* and or a Brick* or a Boundary* or a Ball* (all of this inherit from entity), a bit kind of like Unreal Engine BlueprintEditor Cast node, which allow me to say "Cast to Door" and if the thing was a door it return success, otherwise fails. Can you show me an example of how such mechanism is built, in code(C++)? 2)Physics object should just call a generic TestCollision() function between the "this" passed (the current colliding object) and all the other entities, and then the overload resolution calls the appropriate function based on the types. And yet this shouldn't fail to compile for pairs for which I don't explicitly overload, for instance I wouldn't overload for Paddle<->Brick because such comparison will never happen, so when Paddle<->Brick comparison is performed, it should just return false even though I never declared a TestCollision(Paddle* p, Brick* b). How can this be achieved?

Viewing all articles
Browse latest Browse all 17825

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>