How do I optimize this code more?

Get help using Construct 2

Post » Sat Jan 16, 2016 12:55 pm

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
B
21
S
11
G
6
Posts: 414
Reputation: 5,335

Post » Sat Jan 16, 2016 3:04 pm

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/
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 787
Reputation: 3,786

Post » Sat Jan 16, 2016 3:14 pm

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.
B
21
S
11
G
6
Posts: 414
Reputation: 5,335

Post » Tue Jan 19, 2016 6:29 pm

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.
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 787
Reputation: 3,786

Post » Wed Jan 20, 2016 2:26 pm

Oh dear. :( K, I'll try @mindfaQ .
B
21
S
11
G
6
Posts: 414
Reputation: 5,335


Return to How do I....?

Who is online

Users browsing this forum: ivanski1983, marcoscodas, megatronx and 12 guests