In a classic fighting game like Mortal Kombat, character A is on the left facing right and character B is on the right facing left.

At some point the characters can swap positions and hence where they're facing to.

How could I detect this new positions on the characters?

I don't want to be constantly checking if(A.x > B.x + B.width) and I'm guessing is not what video games are doing. So what is the optimal way to detect a swap?

  • 1
    \$\begingroup\$ Why don't you want to check that if expression? It seems like a simple, concise, and clear way to detect the circumstance you care about. What specific downside of this approach would you like answers to address? \$\endgroup\$
    – DMGregory
    Commented Oct 17, 2023 at 0:56
  • \$\begingroup\$ I thought that maybe there was a more optimal way...like once every some event instead of checking that IF every frame \$\endgroup\$ Commented Oct 17, 2023 at 1:01
  • 1
    \$\begingroup\$ I mean, you could check only when character A moves right or B moves left... but really, one if running once per frame is not the kind of cost you need to worry about optimizing. Look out for code that runs hundreds or thousands of times per frame - that's where you'll see the most significant wins. \$\endgroup\$
    – DMGregory
    Commented Oct 17, 2023 at 1:07

1 Answer 1


Keep it simple.

You are asking for premature optimization, which is a good way to overengineer your game.

In general overengineering happens when trying to satisfy some fictitious requirements (in this case that would be that you don't want to check every frame because reasons, but that is not a function of your game, and as long as it can run correctly it isn't a restriction either).

To answer the question as written: You don't have to check every frame. If you have the conditional, you only need to change when the involved variables change.

In your case you only need to check if(A.x > B.x + B.width) when the first character move, or when the second character moves (or when the second character changes width).

And we could come up with a mechanism to react to changes to those values (anything from a dirty flag to an observer pattern).

If these changes are frequent (i.e. the characters are moving most frames, which would be the case on a fighting game), then said mechanism is added overhead compared to checking every frame.

It is even worse if the change can happen many times per frame.

If these changes are infrequent (which is unexpected for a fighting game), then you can have said mechanism to react to one of the variables changing by performing the comparison...

And then the question is: Can perform the required update (flip the characters) right away?

  • If you can't, you would have to set a flag to be check every frame... So you would still have an if every frame anyway. Making this effort not worth doing.
  • If you can, go ahead, and measure and compare the performance against checking every frame. Even if it more performant, you have the drawback that you are making the code harder to reason about, which might mean harder to maintain.

We have restricted the cases where the optimization might be useful to when the change to the variables is infrequent, and the update can be performed right away.

Yet, I do not know if the optimization even worth doing. So, how do we find out? We fuck around experiment.

First, We know that checking every frame works. Thus make that your initial implementation. You can call it a "reference implementation", and use it as base line for the correct behavior and performance.

Next, if testing that initial implementation you find that is has bad performance, you can run it in a profiler. The profiler will tell you where your code is expending more time.

I cannot tell for sure if the check will show up as a performance problem in your profiler. You have to do the experiment.

Yet, usually bad performance comes from some asynchronous operation your code is waiting on (probably some IO), or some CPU intensive computation (probably something you are doing many times each frame).

Thus: I do not expect if(A.x > B.x + B.width) to be a performance problem. If it turns out to be, then you will have quite a question to ask, and more information on what is going on.

To be completely honest, I struggle to imagine why this might be a performance problem (I'd be open to find out), or how it could be optimized (I'd hope that if have a real case, we would also have the information to figure it out).

Be aware that we are talking about two characters in a fighting game. This is not a problem of a naïve approach scaling poorly, because this does not have to scale.

If it needs to scale, we need to define how the behavior must scale beyond two character. But to avoid getting into that, and making this answer longer, I'll limit myself to mention a couple tools we would use to optimize this for thousands of characters: space partitioning and flow fields. I'll also mention that we would want to move some of the code to the GPU.

Thankfully, this is not the problem at hand.

Thus: Keep it simple. And don't get ahead of yourself.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .