How do I optimize this code more?

  • Hello World,

    Check out the capx below please, it's an aiming system for a game I'm working on. Click on a B sprite and aim, if you move it slowly you will see the framerate stay pretty constant, however if you start moving it fast the frame drops quite considerably.

    Is there anything else I can do to improve it performance wise?

    Thank you.

    Link: https://www.dropbox.com/s/vonpi3icldzen ... 2.rar?dl=0

  • I would probably try:

    1) set laser to max range

    2) check for overlaps

    3) loop over each of the overlapped targets

    4) loop over the lines of the collision polygon of those targets; determine the intersection of the two line segments; if the intersections distance from the shooting point is smaller than localvar_distance (which is initially set at maxrange + 1), save the intersection point to local variables as well as the UID of the hit unit.

    -> after all lines are checked, you will have determined the first collision with your ray, have the x and y of the collision point and UID of the hit unit, in case you wanna know which unit is hit.

    Currently you are doing 50000 collision checks and 7000 poly checks per second for one laser, which is very hefty. With the solution above you would probably do 2 checks (1 for sight, 1 for determining the hit) * amounts of enemy units * 60 fps = 720 checks (current example scene) and then some math calculations, limited to only the objects that could potentially be hit. I think it will be faster.

    One problem is that you can't access the collision polygon coordinates. so you'll have to place image points at the collision polygon points (in an orderly fashion). If you need image points for something else, you will also need a way to determine which image points belong to the collision polygon and which are there for another purpose.

    One way for determining intersections (I haven't tested it myself) can be found here:

    https://lassieadventurestudio.wordpress ... -hit-test/

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Woow, that sounds super complicated.... I was hoping for something simpler. I'll see if I can make any of it work, thanks mindfaQ .

    Any other suggestions are greatly appreciated guys.

  • Hmm, I tried my suggestion out and at least my implementation wasn't really faster than your methode. So maybe one could do something like:

    set laser to max range -> check for overlap

    pick the nearest of the overlapping objects, substract the maximum distance from any point of the object to the point of origin -> you'll get a value kind of close in front of the object you will be hitting first.

    Then check for overlap again with a while loop (be careful with that, make sure endless loops are not possible), but instead of starting at 2 pixels length, you start near the object you are going to hit, so you'll end up with much less collision checks I think.

  • Oh dear. K, I'll try mindfaQ .

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