我试图在屏幕中央显示一堆带有随机颜色的三角形。那些具有各种连续和稳定速度的三角形应该远离视线(超出屏幕限制),具体取决于它们的速度。
实际上我无法弄清楚什么是行不通的。
三角初始化:
在每个三角形中,我将第一个点设置为[-0.1,0.1]中的某个位置。所以我得到了第一点(firstX,firstY)。接下来的两点相应地是(firstX-0.02f,firstY-0.02f)和((firstX + 0.02f,firstY + 0.02f))。那么,所有的三角形都应该是等边的,对吧?
接下来,对于每个三角形,我设置X的速度和Y轴的速度,两者都在[-1 / 2000,1 / 2000]。这个速度是稳定的,每个三角形都不会改变。
三角形更新:
三角形更新每一帧,因为它们以一定的速度移动,如上所述。我知道我必须将它乘以deltaTime,但现在这并不重要。
所以,在更新函数中,我基本上找到每个三角形中的每个点,并根据该三角形的速度更新它们的X和Y.
每个三角形中的3个点中的每个点都具有相同的速度(速度从三角形到三角形不等)。
我无法理解为什么每个三角形都不会移动到屏幕的某个边缘然后消失。
我看到的是三角形,从靠近屏幕中心开始(正如预期的那样,firstX和firstY是接近0的值)并且变得越来越大,而它们的速度似乎越来越低。
代码:
import java.util.Random;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
public class MainMenu implements Screen {
private MyGame game;
private Mesh trianglesMesh;
private int screenWidth, screenHeight;
public static final int MAX_TRIANGLES_NUM = 2000;
//2 for position, 4 for color
public static final int COMPONENTS_NUM_PER_TRIANGLE = 6;
//3 vertices per triangle
private float[] vertices = new float[3 * COMPONENTS_NUM_PER_TRIANGLE * MAX_TRIANGLES_NUM];
//horizontal and vertical speed for each triangle
private float[] speeds = new float[2*MAX_TRIANGLES_NUM];
public MainMenu(MyGame game) {
this.game=game;
screenWidth=Gdx.graphics.getWidth();
screenHeight=Gdx.graphics.getHeight();
//2 components for position, 4 components for color
trianglesMesh = new Mesh(true, 3*MAX_TRIANGLES_NUM, 0, new VertexAttribute(Usage.Position, 2, "a_position"),
new VertexAttribute(Usage.Color, 4, "a_color"));
initTriangles();
}
private void fillRandomColor(int curTriangle, int init, int max, Random random){
for(int i=init;i<max;i++){
vertices[curTriangle*COMPONENTS_NUM_PER_TRIANGLE+i]=random.nextFloat();
}
}
private void initTriangles(){
/*
* How it works: Each triangle has an initial position that is inside the
* visible area. Each one is given a speed to a random direction. Once a
* triangle becomes invisible, there is a possibility that another one will
* come into place. This possibility is true as long as the current number of
* available triangles is less than MAX_TRIANGLES_NUM
* */
Random random = new Random();
for(int currentTriangle=0;currentTriangle<MAX_TRIANGLES_NUM;currentTriangle++){
//1st triangle point
//X and Y init is [-0.1, 0.1]
float firstX=(2*random.nextFloat()-1)/10, firstY=(2*random.nextFloat()-1)/10;
vertices[currentTriangle*COMPONENTS_NUM_PER_TRIANGLE]=firstX;
vertices[currentTriangle*COMPONENTS_NUM_PER_TRIANGLE+1]=firstY;
fillRandomColor(currentTriangle, 2, 6, random);
//2nd triangle point
vertices[currentTriangle*COMPONENTS_NUM_PER_TRIANGLE+6]=firstX-0.02f;
vertices[currentTriangle*COMPONENTS_NUM_PER_TRIANGLE+7]=firstY-0.02f;
fillRandomColor(currentTriangle, 8, 12, random);
//3rd triangle point
vertices[currentTriangle*COMPONENTS_NUM_PER_TRIANGLE+12]=firstX+0.02f;
vertices[currentTriangle*COMPONENTS_NUM_PER_TRIANGLE+13]=firstY+0.02f;
fillRandomColor(currentTriangle, 14, 18, random);
//setting up the continuous speed for the triangle
speeds[currentTriangle*2]=(2*random.nextFloat()-1)/2000;
speeds[currentTriangle*2+1]=(2*random.nextFloat()-1)/2000;
}
}
private void updateTriangles(){
/*
* Updates the position of the triangles, discards the ones not visible
* and possibly adds new ones.
* */
for(int currentTriangle=0;currentTriangle<MAX_TRIANGLES_NUM;currentTriangle++){
//updating the position of every point in triangle
//get the speed
float speedX=speeds[currentTriangle*2];
float speedY=speeds[currentTriangle*2+1];
//update the Xs
vertices[currentTriangle*COMPONENTS_NUM_PER_TRIANGLE]+=speedX;
vertices[currentTriangle*COMPONENTS_NUM_PER_TRIANGLE+6]+=speedX;
vertices[currentTriangle*COMPONENTS_NUM_PER_TRIANGLE+12]+=speedX;
//update the Ys
vertices[currentTriangle*COMPONENTS_NUM_PER_TRIANGLE+1]+=speedY;
vertices[currentTriangle*COMPONENTS_NUM_PER_TRIANGLE+7]+=speedY;
vertices[currentTriangle*COMPONENTS_NUM_PER_TRIANGLE+13]+=speedY;
}
trianglesMesh.setVertices(vertices);
}
@Override
public void render(float delta) {
// TODO Auto-generated method stub
updateTriangles();
Gdx.gl.glClear(GL20.GL_COLOR_CLEAR_VALUE);
trianglesMesh.render(GL20.GL_TRIANGLES, 0, vertices.length);
/*
* TODO
* Here must check when to go off the main menu screen.
*
* if(condition){
* game.setScreen(someOtherAwesomeScene);
* }
*
* */
}
@Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void show() {
// TODO Auto-generated method stub
}
@Override
public void hide() {
// TODO Auto-generated method stub
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
}
预览我看到的内容:http://i.imgur.com/fF7djBt.gif
我再说一遍,我只希望三角形保持尽可能小,因为它们以一定的速度移动到屏幕的边缘。我没有得到
答案 0 :(得分:0)
问题是COMPONENTS_NUM_PER_TRIANGLE应初始化为18然后
private float[] vertices = new float[COMPONENTS_NUM_PER_TRIANGLE * MAX_TRIANGLES_NUM];