在libgdx中,如何根据两组坐标计算旋转modelInstance所需的旋转?

时间:2015-04-08 15:16:51

标签: 3d rotation libgdx transform angle

我正在尝试根据两组坐标测试定位胶囊的方法。我将模型添加到world中的方式与在libgdx示例中完成的方式相同。它们没有质量因此没有创建任何物体,仅modelInstancesendPoints[2]保持要使用的两个坐标的xyz个浮点数。坐标将是可变的,但出于测试目的,我是硬编码值。 dir1 vector3基本上是说模型正在指向。 dir2是通过从另一个坐标减去一个坐标来获得向量方向而创建的vector3。我已经尝试对这个向量进行规范化,但结果甚至更糟。 dist是矢量的长度,所以我知道要创建的胶囊的高度。我确信我最终可以扩展它,但这是为了测试旋转所以我还没有担心。

BaseEntityModeled与libgdx示例中的BulletEntity类似,而BaseEntityModeledConstructorBulletConstructor类似。后者将计划存储在以后使用,而前者创建要渲染的实际对象。

endPoints[0].set(-2000, 2000, -5000);
endPoints[1].set(2000, 4000, 2000);

Vector3 dir1 = new Vector3(0f, 1f, 0f);
Vector3 dir2 = endPoints[0].cpy().sub(endPoints[1]);

float dist = endPoints[0].dst(endPoints[1]);

Model capsule = modelBuilder.createCapsule(250f, dist, 8, new Material(ColorAttribute.createDiffuse(Color.WHITE), new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA)), Usage.Position | Usage.Normal);
world.addConstructor("capsule", new BaseEntityModeledConstructor(capsule, 0, null));

Model sphere = modelBuilder.createSphere(1000f, 1000f, 1000f, 16, 16, new Material(ColorAttribute.createDiffuse(Color.RED), new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA)), Usage.Position | Usage.Normal);
world.addConstructor("sphere", new BaseEntityModeledConstructor(sphere, 0, null));

BaseEntityModeled theCapsule = world.add("capsule", 0, 0, 0);

BaseEntityModeled sphere1 = world.add("sphere", endPoints[0]);
BaseEntityModeled sphere2 = world.add("sphere", endPoints[1]);

float k_cos_theta = dir1.cpy().dot(dir2.cpy());
float k = (float) Math.sqrt(dir1.cpy().len2() * dir2.cpy().len2());
Vector3 rotAxis = dir1.cpy().crs(dir2);

theCapsule.transform.setToRotation(rotAxis, k_cos_theta + k).trn(endPoints[0].cpy().add(endPoints[1]).scl(0.5f));

从我能够理解的情况来看,底部的公式应该计算Vector3和旋转物体所需的角度,但它们并不是正确的。我完全陷入困境。

spheres被放置在坐标上,因此我能够验证胶囊是否正确排列。

0 个答案:

没有答案
相关问题