Quantcast
Channel: Question and Answer » libgdx
Viewing all articles
Browse latest Browse all 434

Libgdx rotate sprite with collision

$
0
0

Im making a game where I have an object in the center of the screen and then balls come out from the sides of the screen. When you touch the screen the object rotates 90 degrees clockwise and it has to catch the balls with only the yellow side and if the balls touch the black sides of the object the player loses. I’ve come to the conclusion that I cant use a Rectangle but the Box2d although I have no idea how to use it. Any tips or advises?

Here’s the object: enter image description here

and here’s the code I got:

//Create()

//object texture
Up = new Texture("up.png");

upSprite = new Sprite(Up);

upSprite.setOriginCenter();

//creates the square in the middle of the screen
square = new Rectangle();
square.x = 630;
square.y = 720 / 2 - 32 /2;
square.width = 32;
square.height = 32;

upSprite.setPosition(square.x, square.y);

}


//creates the balls and sets their position as well as the random timer for each
private void spawnBalls1() {

    ball1.x = MathUtils.random(639, 641);
    ball1.y = 720;
    ball1.width = 32;
    ball1.height = 32;
    balls1.add(ball1);
    lastDropTime = TimeUtils.nanoTime();

}


//Render()

Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

batch.begin();

upSprite.draw(batch);

batch.end();

if (TimeUtils.nanoTime() - lastDropTime > 1000000000) {

    switch (MathUtils.random(4)) {

        case 0:
            spawnBalls1(); break;

        case 1:
            spawnBalls2(); break;

        case 2:
            spawnBalls3(); break;

        case 3:
            spawnBalls4(); break;
    }
}

Iterator<Rectangle> iter1 = balls1.iterator();
while(iter1.hasNext()) {

    Rectangle balls1 = iter1.next();
    balls1.y -= 350 * Gdx.graphics.getDeltaTime();
    if (balls1.overlaps(square)) {

        score++;
        showScore = "Score: " + score;
        iter1.remove();
    }
}

//more parts of the code

If you need more information of the code just say it. Thanks in advance!!


Viewing all articles
Browse latest Browse all 434

Trending Articles