I have an object in a scene which, for this example, is called "centerobject." Lets say we have 20 object scattered all over our scene. To get the objects, I could use a near sensor, which will trigger when the object is near a property/material. I am looking for something similar to this. Here's what I'm looking for: all objects with a property ("detectable") that are in x range (near sensor) have a property ("distance") which reads the distance between the object itself and the centerobject. How can I do this? I am looking to have an answer in python. Thanks!
Measure to faces
The near sensor detects when any face of an object's (physics) hull is within a certain distance. This is especially good when you work with in-homogeneous formed shapes such as planes, poles, streets, fences. But this can be a pretty slow detection even in native code.
While a near sensor provides you with all the information you need a Python controller to evaluate that results. Be aware the distance will be the distance to the center - not to the nearest face.
PROPERTY_DISTANCE = "distance" def distanceToProperty(controller): nearSensor = controller.sensors if not nearSensor.positive: return center = controller.owner for hitObject in nearSensor.hitObjectList: hitObject[PROPERTY_DISTANCE] = center.getDistanceTo(hitObject)
You can control the filter via the GUI:
Near/Radar/Collision sensor [True Level Triggering] --> Python controller Module: detector.distanceToProperty
Measure to distance
In difference to the near sensor this method measures to the center of the object only. This is good on homogeneous formed shapes such as spheres, cubes, house.
The evaluating code is the same as before. The difference is that you need to perform the sensing via Python. That means your Python controller acts as a sensor (but is no sensor). To do that you have to constantly trigger the Python controller.
PROPERTY_DISTANCE = "distance" PROPERTY_DETECTABLE = "detectable" def distanceToProperty(controller): center = controller.owner for object in center.scene.objects: if PROPERTY_DETECTABLE in object: object[PROPERTY_DISTANCE] = center.getDistanceTo(object)
Always sensor [True Level Triggering] --> Python controller Module: detector.distanceToProperty