Question

I am trying to show a bunch of triangles with random colors on the center of the screen. Those triangles, with various continuous and steady velocities should go out of sight (beyond the screen limits) depending on their speed.

I can't work out what doesn't work right, actually.

Triangle initialization:

In every triangle I set the 1st point to be somewhere in [-0.1, 0.1]. So I get a (firstX, firstY) for the first point. The next two points are (firstX-0.02f, firstY-0.02f) and ((firstX+0.02f, firstY+0.02f)) correspondingly. So, all the triangles should be equilateral, right?

Next, for each triangle, I set a speed for X and a speed for Y axis, both being in [-1/2000, 1/2000]. This speed is steady and will never change for each triangle.

Triangle updating:

The triangles update each frame, because they are moving with some speed, as stated. I know that I will have to multiply it by deltaTime, but this is not important for now.

So, in the update function, I basically find each point in every triangle and I update their X and Y based on the speed of that triangle.

Every of the 3 points in every triangle, have the same speed (speed varies from triangle to triangle, though).

I cannot understand why doesn't every triangle simply move to some edge of the screen and then disappear.

What I see is triangles, starting from close to the center of the screen (as expected, as firstX and firstY are values close to 0) and becoming bigger and bigger, while it seems that their speed decreases.

The code:

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

    }

}

Preview of what I see: http://i.imgur.com/fF7djBt.gif

And I repeat that I would just expect the triangles to stay as small as they are moving with some velocity to the edges of the screen. I do not get

  1. How do they become larger?
  2. How do they stop being equilateral?
  3. Why does the speed seems to decrease?
Was it helpful?

Solution

The problem was that COMPONENTS_NUM_PER_TRIANGLE should be initialized to be 18 and then

private float[] vertices = new float[COMPONENTS_NUM_PER_TRIANGLE * MAX_TRIANGLES_NUM];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top