13

So, I'm still not certain if it's actually a bug or merely my lack of experience, but I've been working on a 2D platformer game (using only C++ and SDL2) for roughly 2 years now (on and off; sometimes off for months) and I'm extremely embarrassed about this, but for the life of me, I cannot seem to get the player character's movement and collision physics working properly. It's driving me absolutely insane.

I've read articles and tutorials, referenced books, and posted about it in game development communities (e.g., gamedev.com, Discord servers, etc.), but even though the fundamental structure and explanations made sense, getting the code to work has been unsuccessful, albeit not completely so, but if I get one thing working, another thing breaks. It feels like I'm trying to repair a vase that fell off of a skyscraper and turned to dust on the street below.

I've always been a very tech savvy person with a fiery passion for programming, electronics and game/software/embedded/web development, but to be honest, having such a difficult time with things like this that — in theory, at least — seem like trivial bumps in the road have made me feel like I'm never going to be successful in this field. But regardless of the depressing thoughts of worthlessness, my passion doesn't let me stop trying. Who knows, maybe it'll have to remain just a hobby. 😕

Comments
  • 1
    I had the a very close problem once a while ago.
    I don't know how movement in SDL works, but did you think about making a very basic collision handling? If you know the position, height and width of every object, in a very simple case you only have to check in the player movement routine if the next step collided with one of these objects. This should work and be only a couple of lines of code.
    If you then wanted to optimise it, you could try to only check objects that are currently visible or near to the player.
  • 0
    @Benedikt Well, I keep thinking that it might partly be due to the placement of my movement/collision code within the codebase, too. The GameObject base class is structured with the HandleInput > Update > Render pattern and I guess I'm not exactly sure how to properly handle the code for input, movement and collision detection/resolution separately. In the hustle and bustle of trying to make it work, the "complexity" of the code tends to increase, but I always feel like it should be short, simple and succinct, but I just don't know how to execute it correctly. Do you know what I mean?
  • 1
    @MrHallows I dont know what you have, but you should have a World singleton somewhere (or passed in some kind of context). Or even a message passing/event system that lets the colliding object know what it collided with, and how far inside the other thing it is. After that you just need to pull it back by that much. This is trivial for circular and AABB collisions. Much harder for polygons, keep it simple for now as platforms dont need a super precise collision. Maybe if you can be more specific about your architecture and whats breaking I could help more.
  • 2
    Even worse then there are bugs in the compiler/language itself and it isn't your fault..

    As such you have to discover these and find workarounds.

    I suggest starting from the very very beginning of your code and comment out everything but one task at a time, watch it run, study it, see if there is a memory leak.

    If there is, and you can't figure out why, rewrite the code to do the same task in a different way, and see if you still get the memory leak.

    Yes it can take an absolute forever to achieve a perfect working piece of software this way, but you can do it. :-)

    You can also cheat and include memory clearing up routines to fix the issue from the other side of the fence so to speak.

    It's a very slow process and folk ask me why it might take me a month to code a simple function when they can do the same thing in an hour.

    Yes, but mine is perfect, your's crashes on 10% of users machines in the first hour..
  • 1
    2D collision detection pixel perfect ?

    Eg. checking if pixel from object A is in the same place as pixel from object B ?

    Or the, circle around object A overlaps circle around object B approach ? (I prefer pixel perfect, so it is more realistic and you can miss collisions by a gnats whisker !)

    Library routines ?
  • 1
    What problems are you having exactly? I have some experience coding 2d platformer engines ;)
Add Comment