Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Dozens of objects running code lags game (distance checking for each object)

Author
Message
Tobias_Ripper
11
Years of Service
User Offline
Joined: 24th Mar 2013
Location: REPCONN inc.
Posted: 15th Nov 2015 05:37 Edited at: 15th Nov 2015 06:05
It's a cuboid game (cube based 3D game) quite small though

I need to disable rendering for any cube that is too far away. Currently this is my logic:

I created a random terrain and stored each cube id in an array.
Then I have a for loop that checks each cube id if it is close to the player object and if it is, it sets that cube to visible, not - invisible.

Now this causes a massive amount of lag and of course since every cycle it has to go through every single cube and I have a 1000+ cubes and that will still increase.

With this being the logic, how can I get the cubes to run this code on their own? Like a self sustained object. Or perhaps a different solution?

Video if the issue: https://www.youtube.com/watch?v=bVH_7sKsa2Y
Eisenstadt Studio: Eisenstadtstudio.com
Composers Page: Milesthatch.net
haliop_New
User Banned
Posted: 15th Nov 2015 06:10 Edited at: 15th Nov 2015 06:12
but why does your cycle , cycles trough all the objects ? I don't get it..

this is wrong everyone.
you are running on atleast 60 fps use the energy of the loop.
what most people have:


if you have more then enough objects to go around.. it will bottleneck your CPU or your GPU or both..

how to fix:

there are some options.
1st - easiest -


2nd solution is more preferred as it will not even calculate the distant objects that you don't see

no code needed here but an explanation.
so lets say
you have 1000 objects
you start your level at 0,0,0 and the objects are created along the Z axis forwards ...
so all the objects that stands on 0z to 100z are in one array
all the objects that stands on 100z to 200z are in the second and so on...
so you'll get an array of objects



and now the loop that cycles trough all the objects
cycles only for say the first 3 closest Z sections of the array so



now you also have to check for other stuff like


if an object was set visible but you check it again without asking if the object is visible it will decrease your FPS
If you set something to an object that dosent exists it will decrease your FPS

so always check:



tell me if you need more assistance , Tweaking is my kind of cake in coding

btw , I didn't want to say it .. but you can get even more faster cycles
if you combine both solutions
Zwarteziel
13
Years of Service
User Offline
Joined: 22nd Jan 2011
Location: Netherlands
Posted: 15th Nov 2015 06:14 Edited at: 15th Nov 2015 07:41
Hi,

Are you using the array to store information about the cubes' position? If so, you will want to look into making a 'Line of Sight'-routine. This will only show objects that are within a certain radius around the player/camera. When you move, you re-calculate this position and turn objects on or off accordingly.

I made an example of a circular LOS-routine some time ago. You can find it here. Hope It helps!
Tobias_Ripper
11
Years of Service
User Offline
Joined: 24th Mar 2013
Location: REPCONN inc.
Posted: 15th Nov 2015 06:42
haliop_New
I do apologize as I am not too aware of the best optimized methods of problem solving so if something seems amateur and ineffective that would be my limited self trained logic so any improvements will be used for my own learning.

I got the method you were talking about. In a 2D game the X and Y coordinates of a block would also double up as the first and second keys of the array and that would contain the ID of that object. This makes it quite easier. I wish AppGameKit supported 3 dimensional arrays natively though.

Zerotown - Thanks for the link, however it says the "entry does not belong to you" so I think its private

PS:
I am going thorough a transition from Game Maker Studio to AppGameKit since in game maker you created an object with it's own code and dropped as many instances of that object as you needed. which explains why I tried to loop through each object with the same code. It sort of worked but poorly and this showed me that I need a new way of thinking.
Eisenstadt Studio: Eisenstadtstudio.com
Composers Page: Milesthatch.net
haliop_New
User Banned
Posted: 15th Nov 2015 06:43
Zero .. I also wanted to see it but it says :

That codebase entry does not belong to you. Return to codebase.

somehow this link is for editing that code or something .. and not just viewing it.
Tobias_Ripper
11
Years of Service
User Offline
Joined: 24th Mar 2013
Location: REPCONN inc.
Posted: 15th Nov 2015 06:50
With the use of the X and Z coordinates as array keys to get the closest object, how exactly could I add a 3d - Y dimension. AppGameKit does not support 3 dimensional arrays?
Eisenstadt Studio: Eisenstadtstudio.com
Composers Page: Milesthatch.net
haliop_New
User Banned
Posted: 15th Nov 2015 07:17
sure it does



works just fine for me

I am sorry I wasn't aiming at dissing your knowledge I 2 had to learn the hard way.
its just that actually most people even the most advanced ones still use the old fashion idea of loops.

when in reality you are not running the loop each second but atleast 30 frames of the loop each second.. most of the time much more.
so you can always decide how many times you want something to run via one second or via one frame...

but I have another thing I want you to think about...
instead of just hiding the objects... have you thought of

Rendering the far objects to an image , texturing that image to a wide object or some wide objects
and putting it on the screen ?then set their visibility off ?
so you actually will see all the objects inside an image that is represented by one plane ? then its just one draw call , when you get closer , that image will take a new image without the closest objects
so you will have huge amount of objects in screen at any time just not in real time , like they wont be dynamic but you don't really care about it because they are too far away to even notice... like a scenery object texture.

about me being a bit upset , for the last 2 days I'm so mad of what happened in France it turns out I get angry really quick , I am sorry.
haliop_New
User Banned
Posted: 15th Nov 2015 07:27
btw I have now saw the video...
the use of vectors for this is not needed as it takes more time to calculate...

you can simply do this :

Tobias_Ripper
11
Years of Service
User Offline
Joined: 24th Mar 2013
Location: REPCONN inc.
Posted: 15th Nov 2015 07:31
Ah yes. 2nd year something happens in France. Someone is outta set up a donation fund for international donations to help the victims.

0__0 oh wow it actually does support 3D arrays, well this solves it then. I can track the players position and convert it to array coordinates. Thank you for clearing that up.

There is a thing in game maker where you are able to deactivate an object and it's different from simply not drawing it on it's own. So I'm assuming that you are talking about rendering the farthest objects to screen and disabling it from the individual render pipeline? The far objects are walk-able and intractable though so I am having a bit of a hard time understanding what you mean by that method.
Eisenstadt Studio: Eisenstadtstudio.com
Composers Page: Milesthatch.net
Tobias_Ripper
11
Years of Service
User Offline
Joined: 24th Mar 2013
Location: REPCONN inc.
Posted: 15th Nov 2015 07:33
PS Oh yeah with the 3d array method I won't even need to use vectors because the array coordinates will already double as distance too, got it.

I am wondering if the integers in AppGameKit have a value limit?
Eisenstadt Studio: Eisenstadtstudio.com
Composers Page: Milesthatch.net
Zwarteziel
13
Years of Service
User Offline
Joined: 22nd Jan 2011
Location: Netherlands
Posted: 15th Nov 2015 07:43 Edited at: 15th Nov 2015 07:45
Hey guys,I've edited the link. Should be working now. (?) The example is 2d, but I use the exact same code for my 3d dungeon crawler.
Tobias_Ripper
11
Years of Service
User Offline
Joined: 24th Mar 2013
Location: REPCONN inc.
Posted: 15th Nov 2015 07:48
Awesome, thanks Zero
Eisenstadt Studio: Eisenstadtstudio.com
Composers Page: Milesthatch.net
haliop_New
User Banned
Posted: 15th Nov 2015 10:21
here you go
took me some time but it works good on solid FPS both ways , with or without render to image.
enjoy.

BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 15th Nov 2015 17:06
For a smoother solution, it would be better to query 1/12 of the objects each cycle, instead of all the objects every 12th cycle.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Tobias_Ripper
11
Years of Service
User Offline
Joined: 24th Mar 2013
Location: REPCONN inc.
Posted: 16th Nov 2015 01:02
Alright, I'm already working on adapting the solution with my game.

Are there any other Object events that can be disabled along with the Visibility? like I think Collisions remain even if the object is not displayed
Eisenstadt Studio: Eisenstadtstudio.com
Composers Page: Milesthatch.net
haliop_New
User Banned
Posted: 16th Nov 2015 11:48
sure
I wrote it upwards in the first comment

just wanted to show how to use render to image to get a lot of objects at once , cause maybe you have
a lot of buildings, roads, foliage(trees,bushes,grass) etc... so at the end it turns out to be quite a few .
Van B
Moderator
22
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 16th Nov 2015 12:59
It might be faster and easier just to maintain a set range of visible objects, in a square. Like you could scan a square area around the camera, and show objects inside the area, and hide objects on the border. So long as this area only moves at a maximum of 1 grid unit per loop, it'll maintain that square area of visibility.

Like if you had an array of 100x100, and stored the object numbers in this - you would work out the grid/array location of the camera, and set 2 coordinates a distance away from the camera position to scan across - like camx-16 to camx+16 for example.

So on a grid of 100x100 cubes, you'd have 10,000 cubes - but with a range of 16, you'd only show 1024 objects or less.

The main benefit with this brute force technique, is that you don't have any complex calculations to slow things down - when scanning the array it's easy to tell if an object is at the edge of the square (hide it) or inside the square (show it). The square itself might just be what you can see in a single screen.

I use this technique quite a lot with terrains, because a lot of the time, the visibility calculations you need will impact on performance benefit - this is a method that uses very little processor time, and is good when the engine itself tries to do it's own object hiding, it kinda reinforces that instead of trying to replace it if you know what I mean.
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 16th Nov 2015 14:43
Building an index of location/viewing angle to establish which objects are visible is a great way forward. It might take a while to build the index in your editor (iterating through every possible group of positions/angles available in the game-world) but should be fast in the game engine as there are no calculating distances required, just a simple lookup based on player's position. You can also build LOD into the system (use lower poly models or even bill-boards for objects further away).

If we had built in fog commands, it could all be seamless without the need for objects to pop into existance as you approach. In fact, we can do fog ourselves with a little 3rd party shader magic (thanks Preben!).

I have a little 3D game project lined up. I might have a crack at this over the weekend.


V2 T1 (Mostly)
Phone Tap!
Uzmadesign
29 games
18
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 16th Nov 2015 23:05
Here's another thought. Just because the map is, say 100 by 100, doesn't mean you need an array of cubes 100x100. You only need enough cubes to fill the visible map. It's then just a case of positioning the cubes around the player and swapping textures/colours about to make the cube look correct for the location it's in. The last point can be handled by having an array that holds all the information for each cell for the entire map.

Here's an example of what I'm taking about (written in 2.14b so you might need to tweak it a little to get it to work on 2.15):



This only create an array of cubes 9x9x9, ie four cubes in all direction to make up the visible map. It then swaps the colour and turns cubes off depending on what cell the player is in. I tried this on my Nexus 10 and was getting 55 fps. But I expect a bit of further optimisation will get it at a rock solid 60 fps.

If I added collision I would only check with those cells directly adjacent to the cell the player was in, so you'd only be checking a maximum of 20 cells (9 on the floor, 9 on the ceiling and 8 for the walls), not all of them will be filled with cubes so it'll actually be less then this most of the time.

This is kind of a simple example but hopefully it gives the idea.
Invaders of the 29th Dimension - available now on Google Play
Find me on indieDB
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 17th Nov 2015 10:27
Nice snippet 29 games! You rock!!
V2 T1 (Mostly)
Phone Tap!
Uzmadesign

Login to post a reply

Server time is: 2024-11-17 00:43:35
Your offset time is: 2024-11-17 00:43:35