我尝试用Java制作Libgdx游戏,我必须画树。 当我尝试制作和绘制树木是可以的,但它需要很多时间。 所以我尝试生成一个数组中的所有分支并在之后绘制它,但我有一个渲染问题。
当我尝试先生成然后绘制树时,我只是在屏幕上看不到任何内容。 但我觉得渲染相同的东西..
代码运行良好:
public void drawBranch(OrthographicCamera b2dCam, int pointX, int pointY, double directionX, double directionY, float size,
int lineWidth, int recursion_number) {
// Finding the End Point of the Vector(line) to draw it
int x2 = (int) (pointX + directionX * size), y2 = (int) (pointY + directionY * size);
// Drawing the line from initial to final point
int lineWidth2 = getCurrentLenght(recursion_number, lineWidth);
Vector2 start = new Vector2(pointX, pointY);
Vector2 end = new Vector2(x2, y2);
// Draw the line
HelperGraphicsClass.DrawDebugLineToPPM(start, end, lineWidth2, getCurrentColor(recursion_number), b2dCam.combined);
// If the maximum recursions are reached we need to stop
if (recursion_number >= CURRENT_RECURSIONS) {
return;
}
// Finding the size of next branch by shrinking the current branch
float new_branch_size = size / SHRINK_FACTOR;
double directionX2;
double directionY2;
// Incrementing the recursion_number by 1
int n2 = recursion_number + 1;
// Finding the Direction Vector of the next branch
directionX2 = Math.sin(ANGLE) * directionY + Math.cos(ANGLE) * directionX;
directionY2 = -(Math.sin(ANGLE) * directionX) + Math.cos(ANGLE) * directionY;
// Drawing the branch
drawBranch(b2dCam, x2, y2, directionX2, directionY2, new_branch_size, lineWidth2, n2);
// // Finding the Direction Vector of the corresponding branch on
// the other
// // side i.e. with angle = -ANGLE
directionX2 = Math.cos(-ANGLE) * directionX + Math.sin(-ANGLE) * directionY;
directionY2 = -Math.sin(-ANGLE) * directionX + Math.cos(-ANGLE) * directionY;
// Drawing the twin branch
drawBranch(b2dCam, x2, y2, directionX2, directionY2, new_branch_size, lineWidth2, n2);
}
代码运行错误:
public void saveBranch(OrthographicCamera b2dCam, int pointX, int pointY, double directionX, double directionY, float size,
int lineWidth, int recursion_number) {
// Finding the End Point of the Vector(line) to draw it
int x2 = (int) (pointX + directionX * size), y2 = (int) (pointY + directionY * size);
// Drawing the line from initial to final point
// g.drawLine(pointX, pointY, x2, y2);
int lineWidth2 = getCurrentLenght(recursion_number, lineWidth);
Vector2 start = new Vector2(pointX, pointY);
Vector2 end = new Vector2(x2, y2);
memoryTreeBranchs.get(0).add(new TreeBranch(start, end, lineWidth2, getCurrentColor(recursion_number)));
// If the maximum recursions are reached we need to stop
if (recursion_number >= CURRENT_RECURSIONS) {
return;
}
// Finding the size of next branch by shrinking the current branch
float new_branch_size = size / SHRINK_FACTOR;
double directionX2;
double directionY2;
// Incrementing the recursion_number by 1
int n2 = recursion_number + 1;
// Finding the Direction Vector of the next branch
directionX2 = Math.sin(ANGLE) * directionY + Math.cos(ANGLE) * directionX;
directionY2 = -(Math.sin(ANGLE) * directionX) + Math.cos(ANGLE) * directionY;
// Drawing the branch
saveBranch(b2dCam, x2, y2, directionX2, directionY2, new_branch_size, lineWidth2, n2);
// // Finding the Direction Vector of the corresponding branch on
// the other
// // side i.e. with angle = -ANGLE
directionX2 = Math.cos(-ANGLE) * directionX + Math.sin(-ANGLE) * directionY;
directionY2 = -Math.sin(-ANGLE) * directionX + Math.cos(-ANGLE) * directionY;
// Drawing the twin branch
saveBranch(b2dCam, x2, y2, directionX2, directionY2, new_branch_size, lineWidth2, n2);
}
我画得那样:
public void drawGeneratedBranchs(OrthographicCamera b2dCam) {
for (int i = 0; i < memoryTreeBranchs.size(); i++) {
for (TreeBranch tb : memoryTreeBranchs.get(i)) {
HelperGraphicsClass.DrawDebugLineToPPM(tb.getStart(), tb.getEnd(), tb.getLineWidth(), getCurrentColor(i + 1),
b2dCam.combined);
}
}
}
我感觉很糟糕,我看不出差异,因为2周我无法解决这个问题......
感谢您的帮助!!