Take the 2-minute tour ×
Blender Stack Exchange is a question and answer site for people who use Blender to create 3D graphics, animations, or games. It's 100% free, no registration required.

So far I could only find the method to get the current position of the objects. I think there is a method that returns the distance between them.

share|improve this question
add comment

4 Answers 4

up vote -1 down vote accepted

I am not sure how you plan on telling blender which two objects. Assuming they are both selected.

import bpy
from math import sqrt

def get_distance():
    """
    return: float. Distance of the two objects
    Must select two objects
    """
    l = []  # we store the loacation vector of each object
    for item in bpy.context.selected_objects:
        l.append(item.location)

    distance = sqrt( (l[0][0] - l[1][0])**2 + (l[0][1] - l[1][1])**2 + (l[0][2] - l[1][2])**2)
    print(distance)  # print distance to console, DEBUG
    return distance

get_distance()
share|improve this answer
    
I think the OP means in the BGE. –  gandalf3 Feb 18 at 22:12
    
How do I see the output of print in that part of the blender visualize? –  Guilherme Feb 19 at 2:25
    
in the console, aka terminal/cmd. You need to start blender form the console though as well –  Vader Feb 19 at 2:27
    
@Guilherme If you choose to mark this as accepted, please rephrase your question to correlate with it as this does not apply to the bge. Also, use of bpy in the bge is discouraged. –  iKlsR Feb 19 at 3:06
    
This answer isn't the game engine. –  ideasman42 Apr 18 at 10:50
add comment

You can use mathutils.Vector locations

import bge

scene = bge.logic.getCurrentScene()
# the object the sensor is connected to
owner = bge.logic.getCurrentController().owner
# the other object you want to get the distance to
obj2 = scene.objects['obj_name']
length = (owner.worldPosition - obj2.worldPosition).length

print(length)

This allows for easier manipulation, so for example if you only want to measure distance on a map (without taking altitude into account) you can do.

length = (owner.worldPosition.xy - obj2.worldPosition.xy).length
share|improve this answer
add comment

That would be getDistanceTo. It will calculate and return the distance between two objects from their origins.

import bge

scene = bge.logic.getCurrentScene()
# the object the sensor is connected to
owner = bge.logic.getCurrentController().owner
# the other object you want to get the distance to
obj2 = scene.objects['obj_name']
length = owner.getDistanceTo(obj2)

print(length)
share|improve this answer
add comment

I make BGE tutorials on my channel, and I have this exact solution, scripts, and .blend file in the description. Just keep watching through the video, I believe I show you how 3 minutes through. https://www.youtube.com/watch?v=d2BL9AxORec

share|improve this answer

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

1  
Welcome to the site :) Link only answers are discouraged here; if the link goes down, so does the answer. –  gandalf3 Jun 7 at 20:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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