Collision Question

0 favourites
  • 6 posts
  • I have a quite a lot of sprites that I turn on and off collisions for depending on if they are on screen or not. Since I did this optimization I seem to get a bit of stuttering when the game scrolls (on mobile). Without this optimization i get better frame rate and no stuttering but higher CPU usage.

    Without optimization i have around 20-40 collision checks/tick

    With optimization usually about around 2-5 collision checks/tick.

    Does the turning on and off of collisions for sprites do anything behind the scenes that could be causing this stuttering? Collision cells that needs updating?

  • Does the camera move ? Forcing you do do this on/off thing every tick ? That can be heavy, yes.

    What are the offscreen objects dooing?

    Is it a game with only interactions with 1 player object ? Or do they interact also with eachoter ?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I turned off collisions for my weather effect "paricle" objects because they aren't needed... and CPU usage dropped from 15% to about 8% when the weather is coming in "heavy".

    Having un-needed collision checks does seem to make a difference, especially when there's a lot of them happening.

    ~Sol

  • I turned off collisions for my weather effect "paricle" objects because they aren't needed... and CPU usage dropped from 15% to about 8% when the weather is coming in "heavy".

    Having un-needed collision checks does seem to make a difference, especially when there's a lot of them happening.

    ~Sol

    Yeah. Turning them off is always good when not needed, but constantly updating on/off is another thing. I have one project where i turn off collisions based on if they are on screen or not, and if you're moving around a lot in the layout, The turning on/off seems to be causing some jumpy scrolling.

    Sprite - Is on Screen -> Collisions Enabled

    Sprite - Is not on Screen - Collisions Disabled.

    So if you have a lot of sprites constantly moving in and out of screen, switching from disabled/enabled seems to cause some hiccups. Constanlty Z ordering all sprites in layout seemed to have a similar effect, so I optimized my Z ordering to only sort objects that actually needed to be sorted. For my isometric game, I'm only sorting 2 instances at any given time, the one in front and the one in back, and only if:

    Pick by evaluete: Family1.Y > Family2.Y & Family1.Zindex < Family2.Zindex

    or

    Pick by evaluete: Family1.Y < Family2.Y & Family1.Zindex > Family2.Zindex

    -> Call function Order (param 0: Family1.UID) (param 1: Family2.UID

    On function "Order".

    Pick Family2 by evalute: function.Param(0) &function.Param(1)

    Family2 is on Screen

    -> Set Family2 instance var "SortValue" to. Family2.Y

    -> Sort Z Order by Family2.SortValue

    // This causes the the function to trigger only 1 time, since the function will sort only any objects that does not have the correct sorting, and all the previous events will be false, so no more sorting is needed.

    This blogpost explains a bit why Z order has to be preserved in render cells. That's why I optimized my Z ordering, but it seem's turning collisions on and off, on the fly is heavier than just keeping them on, and could be causing the stuttering I'm experiencing.

    https://www.scirra.com/blog/ashley/14/how-render-cells-work

  • Render cells are usefull when used with layers that contain non moving objects. And a lot of non moving objects. A lot ? No, a big WORLD of non moving, static objects. Z-order is then maintainted once, only once.

    In fact, render cells are an improvement if you notice an improvement, else they are not. Thats the rule.

    About the 'hidden loops' you use. When there are 2 loops nested with counters that go from 0 to 9, that nested loop will run 100 times allready.

    Now, when you use 'Pick by evaluate', that is actual a loop, behind the scenes of course. It loops trough all (specified) objects and evaluates them. It is a fast loop, nevertheless a loop. Using families, that can be a bunch of objects. Using two families containing the same objects is actual a nested loop. 100 objects in the famelies = allready 10.000 times that it has to evaluate.

    Then you call the function. With again 2 'hidden loops'.

    Yeah that gets CPU demanding. For ALL objects in that family. If you really want to do this, then you to have filter both familys with the 'is on screen' before giving them to the first evaluate. That will allready do a lot.

    Oh, and, our friend Rex has a plugin to Z-order.

    http://c2rexplugins.weebly.com/rex_zsorter.html

  • Yep I do a similar thing with zordering...

    If the "rotation" of any of my characters changes, it triggers the zorder function. When I had it running all the time, the impact on fps was horrible. My game went from having 100 characters at 60fps to having 10 characters at 15fps... making zorder only trigger when it's needed really sped things up as well.

    ~Sol

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)