# BGE How to align object1 local z axis to object2 local z axis and to vector?

(Rusoski) #1

Good day community! I require your assistance on this one.

You see, I’ve been working on this script which sets the camera to follow an object and moves around it, always focusing the object.

Before anything else I am working on a space-planets environment game.

This is the code I am working with (not fnished):

``````import bge, math
from bge import logic

cont = logic.getCurrentController()
own = cont.owner
scene = logic.getCurrentScene()
obj = scene.objects
player = obj["player"]
gd = logic.globalDict

vectto = own.getVectTo(player)

own.alignAxisToVect([0.0, 0.0, 1.0], 2, 0.15) # I Require help with this line
own.alignAxisToVect(vectto[1], 1, 0.15)

x = player.position[0] + gd["radius"] * math.cos(gd["theta"]) * math.sin(gd["phi"])
y = player.position[1] + gd["radius"] * math.sin(gd["theta"]) * math.sin(gd["phi"])
z = player.position[2] + gd["radius"] * math.cos(gd["phi"])

own.position = [x, y, z]
``````

As you can see in the line 13 I am aligning the object’s local z axis to the world’s positive z axis, but that is not what I want to achieve.

What I want to achieve are these 2 things:

1.- Align this object (own) local z axis to the second object (player) local z axis, this to simulate space flight.

2.- Align this object (own) local z axis to the vector from the planet to the player, this is because it walks on a planet.

I have a second script where I calculate the vector to align the player to the planet:

``````from bge import logic
import math
from mathutils import Vector

cont = logic.getCurrentController()
own = cont.owner
scene = logic.getCurrentScene()

for obj in scene.objects:
if 'alignToSurface' in obj and obj['alignToSurface'] == True:

xDist = obj.worldPosition[0] - own.worldPosition[0]
yDist = obj.worldPosition[1] - own.worldPosition[1]
zDist = obj.worldPosition[2] - own.worldPosition[2]

distSquared = (xDist)**2 + (yDist)**2 + (zDist)**2

dist = math.sqrt(distSquared)

if dist &lt;= 20:
align = own.getVectTo(obj)[1]
obj.alignAxisToVect(align, 2, 0.15)
``````