Optimizing code

Get help using Construct 2

Post » Mon Apr 16, 2012 5:03 am

Hey

I'm doing a condition that check if an object overlaps another and the one with Y higher is set above to the other.

but right now this condition is consuming a lot of FPS when there's a lot of objects (testing with 100)

the condition is simple:
sprite1: is overlaping sprite1
sprite1:pick nearest to (450, layoutheight)    ------- sprite 1:move to top layer

note: The ideal way is check only his y position but didn't found a condition like that so I added a random X value in the pick nearest event.


without the event the game runs with smooth fps(50+fps) with 100 objects, with the event on, it drops to 10fps.

Any idea how to optimize this event? the problem is probably checking every object overlaping another one..Guif0DA2012-04-16 05:05:05
B
30
S
6
G
5
Posts: 433
Reputation: 6,061

Post » Mon Apr 16, 2012 5:16 am

What are you trying to achieve? If it's z-ordering based on y position, try this plugin: http://www.scirra.com/forum/plugin-isometric-z-ordering-based-on-y_topic46247.html
Moderator
B
88
S
32
G
33
Posts: 3,005
Reputation: 27,432

Post » Mon Apr 16, 2012 5:19 am

What makes sense to overlap the same sprite that you mention sprite 1 overlaps sprite 1?

I think you should to post your .capx so we need to know what is clearly problem
B
96
S
25
G
20
Posts: 3,054
Reputation: 22,642

Post » Mon Apr 16, 2012 6:00 am

[QUOTE=Arima] What are you trying to achieve? If it's z-ordering based on y position, try this plugin: http://www.scirra.com/forum/plugin-isometric-z-ordering-based-on-y_topic46247.html[/QUOTE] from few tests i made with this plugin it seems to work very good with a lot of objects.. =D

bug I had to add a new layer only for the objects I want to change his y according to his Y position..
B
30
S
6
G
5
Posts: 433
Reputation: 6,061

Post » Mon Apr 16, 2012 3:04 pm

If you test "Sprite is overlapping Sprite" with 100 Sprites, Construct 2 must test for a collision between every combination of pairs of Sprites, which is about 100 x 100 = 10,000 collision checks per tick, which at 60 FPS is trying to do 600,000 collision checks per second. That kind of thing doesn't even run well in C++ engines, you're just asking for way too much processing to be done.

A simple way around it is to maybe only check every 1 second, or come up with a simpler way of trying to do what you're doing.
Scirra Founder
B
359
S
214
G
72
Posts: 22,951
Reputation: 178,578

Post » Mon Apr 16, 2012 9:51 pm

[QUOTE=Ashley] If you test "Sprite is overlapping Sprite" with 100 Sprites, Construct 2 must test for a collision between every combination of pairs of Sprites, which is about 100 x 100 = 10,000 collision checks per tick, which at 60 FPS is trying to do 600,000 collision checks per second. That kind of thing doesn't even run well in C++ engines, you're just asking for way too much processing to be done.

A simple way around it is to maybe only check every 1 second, or come up with a simpler way of trying to do what you're doing.[/QUOTE] ty for reply.
I thought this was the problem as i mentioned.. the plugin working good for now but the ideal would be achieve the same performance without plugin
B
30
S
6
G
5
Posts: 433
Reputation: 6,061

Post » Mon Apr 16, 2012 9:54 pm

the problem is your checkin constantly and its not somethin thats gonna happen constantly. You should only check in instances where it might change and then only once as long as it doesnt change again.
B
13
S
5
G
2
Posts: 306
Reputation: 3,262

Post » Mon Apr 16, 2012 9:55 pm

Unfortunately, computers just are not capable of performing 600000 collision checks a second... sorry ;)
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,400

Post » Mon Apr 16, 2012 10:42 pm

[QUOTE=aridale] the problem is your checkin constantly and its not somethin thats gonna happen constantly. You should only check in instances where it might change and then only once as long as it doesnt change again.[/QUOTE] any idea about how to do that?
B
30
S
6
G
5
Posts: 433
Reputation: 6,061

Post » Thu Apr 26, 2012 6:26 pm

I was wondering along the same lines- My game spawns bad guys and they sometimes spawn on top of each other as they are programmed to randomly spawn outside of the screen and come into the screen every random number of seconds. For every new bad guy this adds another factorial to the amount of checks of overlapping that I would have to do. That is if there are five bad guys spawning- there are 5! number of ways in which they can spawn on top of each other- this gets really large really fast to write if this guy is over lapping this guy etc. I am new to programming games. SO I was wondering if there is some tool in construct 2 that can call all objects of a certain type a group name like ->ENEMY and then make it so they can't spawn on top of each other???? The thing is they have to be able to overlap with objects in the same layer as they are programmed to animate when they die- and the animation is an object in the same layer.   I was also wondering if anyone knows how to make a color always on the bottom- with most spriting and animating there is a color that is always on the bottom- or every other color gets displayed if this color (say white) ever overlaps with other colors so that objects can overlap (like a plane flying over an island). I thought initially that the layers would suffice but since the sprite image is always a square then sprites which aren't squares have a color on the outside- like white.
B
2
Posts: 1
Reputation: 402

Next

Return to How do I....?

Who is online

Users browsing this forum: Jarnopa, semaso and 10 guests