如何在libGDX中的网格/棋盘上旋转3D彩色立方体

问题描述

已编辑1 解决了不使用四元数的问题-使用了setToRotation和setTranslation,请参阅消息结尾...

解决以下任务方面的任何帮助将不胜感激(我是libgdx和3d编程的新手-这是一个私人项目)

  • 每边各有不同颜色的3d立方体
  • 在网格/“棋盘”上使用W / A / S / D或向左/向右/向上/向下旋转多维数据集
  • 保留位置(在positionarray [] [] []中的x,y,z)的轨迹

目标:创建一个像Endorfun(例如https://www.youtube.com/watch?v=4IEFC_Y3nrA)或Bloxorz(https://www.youtube.com/watch?v=rUyVK1IxyPk)这样的游戏。

我在github(https://github.com/timewasteGames/rollingBox)上发现了一个小项目,该项目已经允许使用W / A / S / D在“棋盘”上移动/旋转立方体。 使用我想扩展/适应我的项目的四元数(至少对于这个问题可以得到一个小的工作示例)。

我可以使用以下代码将立方体的每一面更改为不同的颜色(6张图像)

int attr = VertexAttributes.Usage.Position | VertexAttributes.Usage.normal | VertexAttributes.Usage.TextureCoordinates;
modelBuilder.begin();
modelBuilder.part("front",GL20.GL_TRIANGLES,attr,getMaterial("assets/texture/black.PNG")).rect(-2f,-2f,2f,-2,-1);
modelBuilder.part("back",getMaterial("assets/texture/green.PNG")).rect(-2f,1);
modelBuilder.part("bottom",getMaterial("assets/texture/red.PNG")).rect(-2f,-1,0);
modelBuilder.part("top",getMaterial("assets/texture/blue.PNG")).rect(-2f,1,0);
modelBuilder.part("left",getMaterial("assets/texture/orange.PNG")).rect(-2f,0);
modelBuilder.part("right",getMaterial("assets/texture/yellow.PNG")).rect(2f,0);
_Box = new ModelInstance(modelBuilder.end());

private Material getMaterial(String texture) {
  FileHandle fileHandle3 = Gdx.files.internal(texture);
  Texture texture3 = new Texture(fileHandle3);
  Material material2 = new Material(TextureAttribute.createDiffuse(texture3));
  return material2;
}

但是立方体没有在新位置改变其颜色。 我应该在RollingBox#setTransform以下部分中进行更改吗?我应该使用6种不同的模型并更新模型吗?

...
if (direction == RotationDirection.FORWARD) {
  ...
} else if (direction == RotationDirection.BACK) {
  ...
} else if (direction == RotationDirection.RIGHT) {
  ...
} else if (direction == RotationDirection.LEFT) {
  ...
} else {
  _tempRotation.idt();
  // Add some _tempRotation.set(Vector3.Z,90f); ???
}

说实话,我在3D数学方面并不擅长,并且会感激源代码,我用了很多时间搜索了不同的源代码,但我感到迷茫...

已编辑1 : 给我一个解决方案的想法: 如果按下W / A / S / D,则初始化一些值

newPosition = new Vector3();
newPosition.x = grid.player.position.x;
newPosition.y = grid.player.position.y;
newPosition.z = grid.player.position.z;
if (direction == RotationDirection.RIGHT) {
  newPosition.add(0,1); // add or sub,x or z,depending on direction

  alpha = 0;
  fromAngle = 0;
  toAngle = fromAngle + 90 % 360; // + or - depending on direction...
}
...

更新播放器的位置(=立方体)

final float delta = Math.min(Gdx.graphics.getDeltaTime(),1 / 30f);
alpha += delta * speed;
float angle = fromAngle + alpha * (toAngle - fromAngle);
Vector3 tmpV = new Vector3();

Vector3 axis;
if (newPosition.x == grid.player.position.x) {
  axis = Vector3.X;
} else {
  axis = Vector3.Z;
}

tmpV.set(grid.player.position).lerp(newPosition,alpha);
grid.player.getInstance().transform.setToRotation(axis,angle);
grid.player.getInstance().transform.setTranslation(tmpV);

if (angle > toAngle && direction == RotationDirection.RIGHT) {
  // top moving,// update players position
  grid.player.rotate(RotationDirection.RIGHT);

旋转实现基于 Rotate Cube and track its sides 并“替换”多维数据集。 不太优雅,但对我有用...

Screenshot

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)