2
$\begingroup$

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!

$\endgroup$
1
  • $\begingroup$ I know how to write the code for this, but I have never used the Game-Engine so I can't tell you how to implement it. What you want to do is make a list of all the objects you need to test. Then iterate through the list and look at the distance for each. Here is a link to a code sample $\endgroup$ – Glyph Dec 1 '15 at 2:32
3
$\begingroup$

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.

detector.py:

PROPERTY_DISTANCE = "distance"

def distanceToProperty(controller):
    nearSensor = controller.sensors[0] 
    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:

enter image description here

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.

detector.py:

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)

enter image description here

Always sensor [True Level Triggering] --> Python controller Module: detector.distanceToProperty
$\endgroup$
1
  • $\begingroup$ Great! It's always you who answer my questions, and I thank you a lot for that. I would be stuck far behind without you. I appreciate all the answers. I don't have time to try this out know, but I am sure it works as the code suggests. Thanks! :) $\endgroup$ – blackhole Dec 1 '15 at 22:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.