libGDX set rotation point 3D

梦想的初衷 提交于 2019-12-01 07:05:02

问题


I have two objects base and weapon and I need to set rotate point of weapon to position of base.

public Test(){
position1 = new Vector3(0,0,0);
baseModel = modelLoader.loadModel(Gdx.files.getFileHandle("data/models/tower/bases/base1.g3db", FileType.Internal));
        base = new Base(baseModel, position1);

        position2 = new Vector3(3,10,5);

        weaponModel = modelLoader.loadModel(Gdx.files.getFileHandle("data/models/tower/weapons/weapon2.g3db", FileType.Internal));
        weapon = new Weapon(weaponModel, position2);
}

Here is update method

public void update(float delta){

        weapon.transform.rotate(0, 1, 0, 45*(delta/2));
        base.transform.rotate(0, 1, 0, 45*(delta/2));
}

Thank you for answer


回答1:


A rotation around a point is the same as translating to that point, rotating and then translating back.
So this process consists of 3 steps:

  1. Translate to the rotationPoint, for example translate(3, 0, 0)
  2. Rotate arround the center (which is now the rotationPoint), for example rotate(0,1,0, 45*delta)
  3. Translate back (the translation is relative to the rotation), for example translate(-3, 0, 0);

In this case the code then looks like this:

weapon.transform.translate(3, 0, 0).rotate(0,1,0, 45*delta).translate(-3, 0, 0); 


来源:https://stackoverflow.com/questions/26656334/libgdx-set-rotation-point-3d

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!