# Simulating Ray Direction and getVectTo

My example is, lets say I have a script that uses the raycast / raycastTo methods. How would I simulate ray direction through that.

This is what I am talking about: rayDirection

Bump…Anyone?

Also, just on another note I have another question: Can you simulate the affect of the object.getVectTo method…with maybe vertices?

Help would be nice…
Sunjay03.

Do you mean Grade 9 in school?

You mean you want a vector (ray) from one object to another?

That’s super easy

import Mathutils
from Mathutils import Vector

vec1 = Vector(object1.worldPosition)
vec2 = Vector(object2.worldPosition)
vecFromVec1toVec2 = vec1 - vec2

This’ll get you a Vector (an X, Y, Z list, basically) that points from one object to another.

You can also just subtract each element of the position list, but Vectors have some nice functions built-in.

the getVectTo() function basically works like this:

`````` from Mathutils import Vector

vec = Vector(pos1) - Vector(pos2)
``````

Or like this

``````pos1 = [x,y,z]
pos2 = [x1,y1,z1]

vec = [None, None, None]
vec[0] = pos1[0] - pos2[0]
vec[1] = pos1[1] - pos2[1]
vec[2] = pos1[2] - pos2[2]
``````

So if you get the vertex position ect.

I’m not sure what you mean by simulating the ray direction, but if you want to cast a ray along a vector it would be like this:

``````from Mathutils import Vector

cont = GameLogic.getCurrentController()
own = cont.owner

pos = Vector(own.position)
vec = Vector(something)

ray = own.rayCast(pos+vec, own, 0, '')
``````

This is what I am talking about: rayDirection

@Andrew: Thanks! I hope it works! Also, would that be the local vector or the global vector? getVectTo returns both…

Bump…anyone?

@Andrew: Its not working…here is an example: getVectTo Testing.blend (130 KB)

Check the console, its different.

Bump…

Well, of course, because source.getVectTo(target) would work like this:

manual_vec = Vector(target.worldPosition) - Vector(source.worldPosition)

Subtraction is not commutative, so the order in which you subtract matters.

Also, don’t forget that getVectTo returns unit vectors (magnitude of 1), so even though the direction will be the same, the components will likely be different, unless you scale their magnitude to that of manual_vec.

Try this script:

``````
import GameLogic as GL
from Mathutils import Vector

a = Vector([0,0,0])
b = Vector([2,5,10])

manual_vec = b-a
print manual_vec

own = GL.getCurrentController().owner

for ob in own.getVectTo(b)[1:]:
vec = Vector(ob)
vec.magnitude = manual_vec.magnitude
print ''
print vec

``````
``````
<b>print.py</b>

from GameLogic import *
from Mathutils import *

a = Vector([0,0,0])
b = Vector([2,5,10])

print a-b

own = getCurrentController().owner

for ob in own.getVectTo(b)[1:]:
print ´´
print ob

``````

First, a-b should be b-a. You get a vector by subtracting start from end, not vice versa.

Secondly, getVectTo() returns three values: A distance to given obj/pos, a normalized global vector and normalized local vector.

``````
<i># Unpack the tuple</i>
distance, worldvec, localvec = own.getVectTo(b)

``````

The two vectors are normalized meaning their length is 1 so if you’re after a vector to the given obj/pos you must first multiply the vector with the distance to set it’s length to same as distance.

``````
vec2b = Vector(worldvec) * distance

``````

So, it is the subtraction thing, it is just the second value minus the first value? Okay. That makes sense. I will try it. Also, thanks for the thing about the magnitude of 1. I didn’t know that.

Okay. So I tested it, and it worked! The reason it was different, was because, well for all the reasons you guys said.

1. The 2nd value must go first in the subtraction statement.
2. The magnitude of the vector from the getVectTo function is 1.

So, after I tested it, I noticed that the local and global values for the vector are the same. Is that supposed to happen? (I am talking about what the getVectTo statement returns.)

The global vector is the vector in world space.
The local vector is the vector in local space of the source object.

Here is an example:

The boy (vector source) is looking to the girl (vector target).
Both are standing on a street (world). The street is from left to right.
I. The boy looks along the street from left to right as well (light blue = dark blue direction). For the street it is the same direction as for the boy: local = global
II. The boy looks in the opposite direction of the street. The street hasn’t changed and still looks from left to right (dark blue). But the boys direction is from right to left (light blue). If he wants to see the girl, he has to turn around. So the local vector is negative.

I hope it helps

Hmmm. So how would you simulate that in a script. Here is the simple test blend I did for the simple getVectTo stuff.

getVectTo Testing.blend (130 KB)

You would calculate the local vector like this:

``````
from Mathutils import Matrix

mat = Matrix(*own.worldOrientation)
local_vec = global_vec * mat

``````

The * (star) in *own.worldOrientation simply splits the components into separate arguments.

The * (star) in *own.worldOrientation simply splits the components into separate arguments.

Wow, I only wished I learnt that earlier

Same here.

@Social: So it works by multiplying each value in the global_vec by the matrix?
Does that work?

Basically, this:

``````
from Mathutils import Vector, Matrix
global_vec = Vector([2.0,2.0,2.0]) #Because that is what the other script eventually returns.

mat = Matrix(*own.worldOrientation)
local_vec = global_vec * mat

``````

In Blender it returns,

2.000000, 2.000000, 2.000000

Sorry if I seem to be asking a lot of questions… :o

PS. I used the normal orientation which is this.
[[1.0, -0.0, 0.0], [0.0, 1.0, -0.0], [0.0, 0.0, 1.0]