0

我有一个obj1创建按钮的对象 ( )。

self.createButton({ position={ 15.7, 0.1, -1.8 }, ...})

当然,那个位置是相对于 的obj1

obj1是一个袋子,它创建的按钮位于游戏板上,远离袋子(从 X 分量的大小可以看出)。这是不合适的,这意味着袋子不能移动。我想让游戏板创建按钮。

createButton从游戏板 ( ) 中调用时,如何确定要提供的坐标obj2

4

1 回答 1

1

我发现可以使用以下方法将相对位置转换为绝对位置:

local rel_pos = Vector(15.7, 0.1, -1.8)
local abs_pos = Vector(-v[1], v[2], v[3])
abs_pos:scale( obj1.getScale() )
abs_pos:rotateOver('z', obj1.getRotation()['z'] )  -- The order is important
abs_pos:rotateOver('x', obj1.getRotation()['x'] )
abs_pos:rotateOver('y', obj1.getRotation()['y'] )
abs_pos:add( obj1.getPosition() )

反转过程 usingobj2将获得要在 中使用的向量obj2


但是为什么 X 分量被否定了呢?我以为我错过了一些东西,直到我发现positionToWorld,这个函数旨在完成上述尝试。事实证明我仍然需要否定 X 组件。

local rel_pos = Vector(15.7, 0.1, -1.8)
local abs_pos = obj1.positionToWorld(Vector(-v[1], v[2], v[3]))

我只能得出结论,它createButton有一个错误,需要否定position它所提供的 X 组件。


以下是完整的解决方案:

local v = Vector(15.7, 0.1, -1.8)
v:setAt(1, -v[1])             -- Bug-compensated loc rel to obj1 -> Loc rel to obj1
v = obj1.positionToWorld(v)   -- Loc rel to obj1 -> Absolute loc
v = obj2.positionToLocal(v)   -- Absolute loc -> Loc rel to obj2
v:setAt(1, -v[1])             -- Loc rel to obj2 -> Bug-compensated loc rel to obj2
print(logString(v))

最终结果是替换

-- In obj1 (the bag)
self.createButton({ position={ 15.7, 0.1, -1.8 }, ... })

-- In obj2 (the game board)
self.createButton({ position={ -7.745875, 13.7634, -1.006971 }, ... })
于 2020-08-16T04:04:04.030 回答