I have a point, and I want to move an object to another point a fixed distance from the first. The direction it moves will vary.

I know how to measure the distance between two coordinates, but I can’t quite get how to this.

How?

Thanks

I have a point, and I want to move an object to another point a fixed distance from the first. The direction it moves will vary.

I know how to measure the distance between two coordinates, but I can’t quite get how to this.

How?

Thanks

Try this:

obj.location = point + distance*direction

point and dirction are Vector distance a float

I tried it:

```
vectdir = [ mathutils.Vector((1,0,0)) ]
dist = 1
p2 = bpy.context.selected_objects[0].location
p2 = p2 + dist * vectdir
```

It throws an error:

“Vector addition: arguments not valid for this operation.”

you want vectdir = mathutils.Vector((1,0,0))

that is, no brackets around the right hand side (it should not be a list).

Ooops…

No brackets.

That works for what it is, but it isn’t doing what I want.

The “dist = 1” is just a multiplier of the vector distance.

It doesn’t really let me set a real world distance.

I do the following:

```
vectdir = mathutils.Vector((1,0,0))
dist =1
p2 = mathutils.Vector((0,0,0))
p2 = p2 + dist * vectdir
bpy.context.selected_objects[0].location += p2
```

For example: If I use

```
vectdir = mathutils.Vector((1,0,0))
```

Then the object moves one unit along the x axis, which equals a distance of 1.

If I use

```
vectdir = mathutils.Vector((1,1,0))
```

Then the object moves one unit along the x axis, and one unit along the y axis which equals a distance of 1.4142 in world units.

I want to be able to always move a set difference, in this case 1 world unit, but be able to enter any direction. Perhaps even a random direction, but always move the set distance.

Does that make sense?

Maybe try normalizing the direction vector first so that it becomes a unit vector that points in the correct direction. This unit vector multiplied by the distance should give the correct translation vector.

```
vectdir = mathutils.Vector((1,1,0))
vectdir.normalize()
dist =1
p2 = mathutils.Vector((0,0,0))
p2 = p2 + dist * vectdir
bpy.context.selected_objects[0].location += p2
```

Edit: The above is untested.

To avoid the potential nan result you could always test that vectdir.length isn’t 0 before the normalization.

I worked out this so far.

```
origin = mathutils.Vector((0,0,0))
vectdir = mathutils.Vector((1,1,1))
dist =(get_distance(origin,vectdir))
print(dist)
dist = 1/dist
p2 = mathutils.Vector((0,0,0))
p2 = p2 + dist * vectdir
bpy.context.selected_objects[0].location += p2
dist = get_distance(origin, bpy.context.selected_objects[0].location)
print(dist)
def get_distance(point1, point2):
v = point1-point2
return v.length
```

This seems to work.

It is set to a distance of 1.

If you change it to:

```
dist = 2/dist
```

Then you get a corrected distance of 2.

Anything wrong with this?

Hi terrachild,

funkywyrm’s suggestion of normalizing will acheive the same end as your code but should be faster. Both cases will have problems if your direction vector is Vector((0, 0, 0)) as this has no direction. If you’re not worried about zero length vectors then this will work fine

```
import bpy
import mathutils
# Distance to move the object
move_dist = 2.0
# Direction to move object in
vectdir = mathutils.Vector((1, 1, 1))
# Make sure the vector has length 1.0
vectdir.normalize()
# Move the object the required distance in the right direction
bpy.context.selected_objects[0].location += move_dist * vectdir
```

Cheers,

Truman

Hi tc,

probably pay to check out some basic vector maths like http://en.wikipedia.org/wiki/Euclidean_vector

As Funky pointed out ( and cos I dawdled also Truman ) the equation requires the direction vector to be a unit vector, ie it has length one.

I always wondered what normalize did.

I guess I figured out how to achieve the same thing without using it.

I will now do it the proper way, which as you pointed out will be faster.

Thanks everyone.