How to? A tiny little script (Near sensor)

Greetings guys!
It’s been long since I posted here! Now I can, since I’m in a short holiday!
I need help to make a LOD system. It works fine, only problem is that it;s got a very short max range, sice I’m using a Near sensor. I need your expertise in devicing a python version of the sensor, so I can elongate the range and the reset numbers.
Thank you for your attention!

how about using a sphere parented to the character and lodding the items once they collide with the sphere? dont know if this would be the best way to do it, but its at least a working posibillity.

i think that this would be the fastest way to calculate it too, only faster way i can imagine would be to use a cube instead of a sphere


Well it should work, Thanks! If only the collision detector worked even if the character is on a stand still. Collision only works if there’s movement (according to my tests). So I still will need the script.

What, 1000 Blender units aren’t far enough for you? What scale are you working at?

Then there’s this method of any gameobject: getDistanceTo. It’ll get you any distance between two objects as a float.

Thank you, very insightful and thoughtful. However, I must point that I’m python illiterate, otherwhise this post wouldn’t have seen the day. My scenes are indeed big, the main planet is about 20000 units of diameter, it has got a ring around it, 30000units diameter, in it asteroids need to be seen to a certain distance, at 1000 units, they just pop up at your face, or disapear too soon.

the problem you encounter here is the maximal few width of blender.

i would greatly recommend you to scale everything down and apply scale afterwards.

i think that otherwise you will run into problems, but i havent tested.


I’d love to make 1000 my maximum scale/distance, however, I’m using the simpler fps template, and althought I asked Raiderium to allow lower scale of the template, this has no been possible till now. At lower scales I also run into collision inacuracies (objects colliding too much with the floor, going through others, fps drops, etc).
I’ve been trying solutions for the scale ever since beggining of last year, so I pretty much tried everything, my setup runs fine as it is, I just need a small script describing the near sensor. I modified a script so I make a planet rotate very slow, by setting the DRot at -0.000001, 2 decimals below the normal, so it’s quite possible to do it, if someone know how to, pliz give me a hand!

the problem i personally got with the python solution is,
that it would need to cycle through ALL the objects in the scene to find out which one is near,

nevertheless, here’s a little script which should do the trick, just place it on your playerchar and it should work, if you connect it with a always sensor.
this of course will not handle the lod, only the distance to the objects.

g = GameLogic
c = g.getCurrentController()
o = c.owner

scene = g.getCurrentScene()
objects = scene.objects

for object in scene.objects:
	if "lodObject" in object:
		if o.getDistanceTo(object) <= 30000:

BUT this will be really slow if you have many objects in the scene and is far from being a good solution!

you can scale an object and afterwards click
object -> Clear/Apply -> Apply Scale and Rotation to Object Data
if you do this the physics should still be working, although i dont know anything about the fps template you use. you should really test, if you can scale, because the near sensor is programmed in c and has been written to be fast, while my python script has be written to merely work.

have fun,

Thank you very much for your script! I see however that it will be troublesome for I have many things to setup, and your script does not replaces the Near sensor, which has a properties selection abilities.
In my lod system, I add a player property to my character, then I attach a near sensor to an empty, which has to detect if the object with ‘player’ is within the range, and it adds an object. So the the empty is set like this:
near: 100, player, distance: 1000, reset: 999 -> and ->edit object:85, add object:tree
so every 100th frame it spawns the object which disapears at 85th frame. This is perfect for me, so I was upset to see such limit in the range…

hello again :slight_smile:

i will do a demo sometime where i further explain everything,
atm i can only upload a blend which does what it should but isnt well documented.

i will upload anyways, try to figure it out, its not that complex

just look at the objects and what scripts, sensors and actuators they have.
if your lodObjects have exactly the same setting and only ONE object messures the distance, this maybe could be usable, many objects doing this will surely screw up your game.

have fun with the blend,
left & right key move the red cube
if in range of the other cubes the plane from layer 2 gets added

sorry for the rush, but i have an team meeting in about two hours and have to finish my milestone till then :slight_smile:



lodTest.blend (450 KB)

I really appreciate your efforts, however, nothing happened when I moved the cube!

blender 2.49b only

it works perfectly here.

what does the python console say?

Yes, I used 2.49b and nothing happens, no error, nothing. I tried the radar sensor to see if it would work. It does to a certain extent, until it does whatever it wants, like the images show. In the first, the asteroids , at least some of them, show where and when they should, ad on the second, shows the problem with the radar sensor, its unpredictable, it should be called laser instead of radar.

have you changed the dist in the python script to 30000?

it HAS to work, if you set up everything like in my blend file.

if you look into the scripts everything should be explained there

Ok, it worked! I’ll have to rebuild the scene to implement it. It’s already too messy! I’ll start tomorrow, I’m working in another project now. Thanks alot. I’ll see how it runs with thousands of objects.

i highly advise that you DO NOT use a LOD script, the problem is that the low poly versions of the models are stored in memory at the same time as the higher poly ones, so in essence it just makes things worse.

as i understood it, he wants to completely hide the objects.

but, i have to agree, i dont think that my script is scalable enough to be used for anything that big (as i pointed out before)

just imagine you have 1.000 objects, that means that EVERY frame, my script has to cycle through ALL those 1.000 objects, decide which one to hide/show and do that.
this can become very very cpu intense (60000 calls per second are a bit much i guess, havent tested it)

greetings again

Plus, if I understand it correctly, the resolution of a scene decreases as the scale increases. By resolution, I mean the precision with which you can represent e.g. a position. I can’t find any good info on it, but basically since Blender uses floats internally, the smaller your scene, the smaller the step between possible positions. Think of it as every object using fractions of the largest scale in the scene, and having limited decimal places to work with.

This means that precision is always the same whatever the total scale of your scene, but if you have a solar system several million times larger than your ship, your ship will be forced to use the precision of the solar system, which is way less than what you’d get in a small scene.

All this boils down to very large scales + very small scales in the same scene = bad.

I just got confirmation on #blendercoders that this is the case.

My recommendation to you is to fake it. That is, make it look right, but not be strictly accurate to scale. You can do this through a background scene with the whole solar system vastly scaled down, and whatever’s near the camera (Saturn’s rings) in another scale in a foreground scene. Then you’d have to translate the camera movement between the two scales.

Anyway, if you run into hard coded limitations of sensors, you should work at a reduced scale. With the right setup, fiddling with physics etc, you can get close enough.

Someone on #blendercoders just mentioned another problem: Z-buffer resolution, but I don’t know much about that.

Its possible we could use your services as a beta tester, though not on a game project so it might not be fun, but it would help. Ill keep you posted ok?

Hmm, I suppose you’re right. The BGE works best in the limited range of 1000 units. Scaling up the spacing will imediatly scale up the default size of object acuracy.
I make an asteroid field with base 9 objects. I’ve got 3 types of asteroid groups, each having between 10 and 30 objects. So I’ve got about 100 objects hidden on a layer. I use no low polys, although I could, so it’s not a pure LOD system. I use empties to spawn the objects. I have tested it already, in a closer environment and it works fine. The empties add the objects for as long as the camera is in range, the main planet is a scaled sphere, and terrain detail works the same way as the asteroids, being that the average size of terrain patches is 3000X3000X1000. Still pretty big, but that makes less big objects.
If the camera’s clipping was independent of the spacing, I wouldn’t use any spacing, and play everything within range. My idea is to have an entire seamless system in one scene, obviously Blender can’t handle that, so I’m trying to make just one planet.
In sum, I’ve got a big scene made out of relatively small objects…