How to add and delete objects based on distance from the player?

The collision meshes are boxes.
My friend is testing this on an older notebook. Without many static objects on the screen, it runs very well. The thing is when there are around 100 of these static box objects, the fps goes down 10+ frames.
Weirdly, it runs 60 fps on my friend’s notebook while using Ubuntu and below 50 fps on Windows.
I was wondering if I can optimize it.
I’ve already done it to create physics objects in a specific place when needed and then delete them afterward if they are not necessary, but not by distance.

Interesting, I don’t know how to really use it but it looks that it could help by calculating the grid.

This looks great. I’ve tried the blend file but I can’t find the object with the properties that use the drawstuff.py. I’m not entirely sure how it works.

it is on the hemi lamp

Huh!? xD What beautiful arcana is this!? The terain mesh, it is just a quad with a multi res modifier - and yet, somehow it has a defined shape? This file is looking very interesting.

Edit: Hmmm I figured it out, it’s hidden in the sculpt mode! That’s a very interesting approach, I don’t think I would have considered in a million years xD Why did you make it like that?

I did this for my game demo, maybe is not the best aproach but it works, at least for me… hope it helps you out.

There is some blend file in the thread

There is a logic culling / physics culling setting/lod…

the idea where to make LOD easy on the ground planes.

Oh, I see. I didn’t know you could create an object’s properties in-game.
That’s why I wasn’t sure where to find it.

How did you set up the appearance and disappearance of the objects in one of your videos?
The one where it looks like the world is creating while you walk.
is it based on distance?

Yes, everything is based in distances (near sensor)

For this you need a putter object (empty) and the cube (hidden because is going to be added with “add object”) and build something with the empties and try with the main character to see how it works :slight_smile:

In my game demo is a bit more elaborated because I can create the game level in real time by adding cubes whenever I want, and by using logic culling and physics culling I was a ble to manage more than 800 objects.

So I wanted to experiment by replacing the same cube but with another one with no collisions and simpler logic bricks, and it works just nice.

In that video I just deactivate the logic bricks that add the cube… so they just dissapear.

How does the grid work?
I have to set the width and scale myself?
It also gives me this error:

AttributeError: type object 'KDTree' has no attribute 'size'

KD_Tree_example.blend (754.5 KB)

so this builds a grid - and then draws all points in a radius

what you need is to add objects inside a radius if they are not added, and remove them past a certain radius if they are loaded.

“loading bubble”

How does it work?
From what point does it create a grid? From the own object?
Can I specify with this which objects to load?

this is just creating the grid - and accessing it - this is not loading / unloading objects yet.

I will make a better demo one sec

KD_Tree_example_loading_bubble.blend (769.2 KB)


this randomly generates a bunch of locations - you could use a look up table / dictionary or something instead as well

1 Like

KD_Tree_example_loading_bubble_with_terrain_planes_Start.blend (872.6 KB)

this picks from 4 types of objects and randomly spawns them per tile - and also spawns a tile

you could use perlin noise instead of random - and use perlin noise to deform the terrain too

1 Like

Thank you for your help, this looks really impresive.
Can you set it up for the player to have this bubble?
I’m not sure how far can this grid expand without loosing the performance.
What I wanted to achieve is:
Bubble_001 Bubble_002
To have Empty objects on map instead of a static objects.
Then activate the static objects when you are in range.
I guess it’s similar to what you are suggesting but it seems this calculate the whole map.

yeah you can do this - so when I build the KDTree I do it from a grid - / list

you can do
#scrape scene objects and get all objects with the property ‘Spawn_Location’

obList =[]
for obj in own.scene.objects:
    if 'Spawn_Location' in obj:
        obList.append(obj)

and use this list instead

when I get a chance I will set it up.

Since the grid didn’t work for me yet, I’ve been trying something else.
I’m not sure if this would be fast for processing or not.

for obj in scene.objects:
    if "Box" in obj:
        dist = obj.getDistanceTo(player)
        obj['dist'] = dist
        if obj['dist'] < 5 and obj['distState'] == False:
            addBox = obj.scene.addObject('Box', obj, 0)
            addBox.setParent(obj)
            obj['distState'] = True
        elif obj['dist'] >= 5 and obj['distState'] == True:
            for child in obj.children:
                if "BoxSpawned" in child:
                    child.endObject()
            obj['distState'] = False

The Empty objects have a Box property and when you are near them, they’ll spawn static objects for a collision. If you are far enough, the static objects are deleted.