Lag with a moderately big program

I created a 2D shooter game with a few extra stuff like bonuses, shots and enemy boxes. The game creates a square pellt every third of a second and creates a random square enemy every few seconds. Overall the game checks many (around between 1000-10.000 (depends on the number of enemies and shots)) "if"-s 60 times/second. The game laggs. In some computers it's not really enjoyable. It only has a few sound effects with "minim" and doesn't have any Images. All of the pc's that I tried can run very complicated trending multiplayer games too, and so I'm confused. Is it Processing's fault, is it sth else? Should I try an other program? I'm still a noob in programing, and I can't understand how can such a simple program have performance problems.

Answers

  • we have to guess what you've done that is slow? doesn't seem fair.

  • edited October 2017

    Sorry, I'm just asking if it's usual for processing to be unable to run bigger codes smoothly. and if it would be beneficial for me to use an other program.

  • It's a few hundred 'if'-s and some calculations cuz of them. I don't think it should be a problem, but it is.

  • I don't think it should be a problem

    I'm still a noob in programing

  • It is possible that you are making a common mistake with big consequences for memory use / speed. For example:

    • calling loadImage() every time draw() loops (BAD)
    • infinitely adding on to an arrayList
    • unnecessarily iterating through the square of all possible collision detections
    • ... etc.

    But, as @koogs points out, if you don't share your code, we can't read your mind. It isn't reasonable to ask the forum to guess -- share your code and let the forum help you with a concrete problem.

    Imagine trying to answer this question with no further details: "I cooked a thing in what I think is the right way, but it burnt. Is that normal for your stove, or is it a bad stove? I don't know much about cooking."

  • edited November 2017

    Thanks for all the answers. My game might do hit detection checks a bit too many times cuz I have huge hit detection chunk and it checks max. 150-200 bullets at the same time. I will check on it. I didn't wanted to share the code cuz it's a jungle inside. I'll come back if I need further help. I'm sorry for making my question a bit over complicated and stuff.

  • edited November 2017

    Make sure to remove dead bullets from the ArrayList

  • If you are optimizing collision detection of many objects in a large space then one method is to divide into smaller screen regions and check each region -- I'm not sure if that was what you meant by "chunk." This requires tracking the current region of each (e.g. bullet) There are some recent posts on the forum about this approach.

    You can optimize differently if your 'bullets' are colliding with each other, or bouncing off walls, or not interacting with anything, and only causing damage to e.g. a ship when they pass through.

  • edited November 2017
    • Using image() with 5 parameters is bad - or using resize outside setup()

    • Of course instantiating objects is bad outside setup() (or instantiating more often than once)

    • When you e.g. check asteroids against each other make sure you check each pair only once (and not twice)

    • dist() can be optimized and should be avoided when time critical

    • Maybe try noSmooth()

    • int can be faster than float sometimes when using it as screen coordinates

  • edited November 2017

    Approach:

    You could do some rough time measurements using millis() for code sections. Using println().

    You could also just comment out code sections (e.g. bullet collision check) and see if it’s an improvement. So you can see which section is responsible for lagging and improve this.

Sign In or Register to comment.