Oskar Loeprecht Oskar Loeprecht - 3 months ago 10
Android Question

Collison with Circles

I'm currently working on a Project where the user has to avoid Asteroids (drawn with ShapeRenderer) with a Spaceship(Sprite). The Asteroids are saved in a List Array. Now i'm trying to create a collision method when Sapceship and Asteroids collide. To make it as simple as possible i want to draw a Circle around the Spaceship and check for Collision. Eclipse says i have a I have a

java.lang.IllegalStateException: begin must be called first
.

Asteroid Class:

public Circle getBounds()
{
return new Circle(p.x, p.y, radius);
}


Asteroids Class:

public void update()
{

for (Asteroid a : asteroids)
{
a.update(Gdx.graphics.getDeltaTime(), xMin, xMax, yMin, yMax);
}

for (int i = 0; i < anzahl; i++)
{

Asteroid a1 = asteroids.get(i);

for (int j = i + 1; j < anzahl; j++)
{

Asteroid a2 = asteroids.get(j);

float abstand = a1.abstand(a2);

if (abstand < a1.getRadius() + a2.getRadius())
{

berechneKollision(a1, a2);

}

}

}

}


public void render(ShapeRenderer renderer)
{

for (Asteroid a : asteroids)
{
renderer.setColor(1, 1, 0, 1);
renderer.circle(a.getP().x, a.getP().y, a.getRadius());
}
}


Spaceship Class:

public Circle getBounds(){
return new Circle(sprite.getX(), sprite.getY(), sprite.getWidth());
}


CollisionManager:

public class CollisionManager extends Asteroids {

private Spaceship s = new Spaceship();

public void checkCollisions(){
for(int i = 0; i < asteroids.size(); i++)
{
if(asteroids.get(i).getBounds().contains(s.getBounds()))
{
//asteroids.remove(asteroids);
System.out.println("KOLLISION");
}
}
}

}


Screen:

public abstract class Screen {

public abstract void create();

public abstract void render(SpriteBatch batch);

public abstract void update();

public abstract void update(ShapeRenderer renderer);

public abstract void update(OrthoCamera cam);

public abstract void resize(int width, int height);

public abstract void render(ShapeRenderer renderer);

public abstract void dispose(int width, int height);

public abstract void dispose();

public abstract void pause();

public abstract void resume();

public abstract void checkCollisions();
}


MenuScreen:

public class MenuScreen extends Screen {

private OrthoCamera cam;
private Spaceship spaceship;
private Asteroids asteroids;
CollisionManager cm;

.
.
.

public void checkCollisions()
{
cm.checkCollisions();
}


Main Class:

public class MyGdxGame implements ApplicationListener {


SpriteBatch batch;
ShapeRenderer renderer;
OrthoCamera cam;
public static int WIDTH = 1080 , HEIGHT = 720; // resolution

@Override
public void create() {
batch = new SpriteBatch();
renderer = new ShapeRenderer();
ScreenManager.setScreen(new MenuScreen());

}
@Override
public void render() {

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

if(ScreenManager.getCurrentScreen() != null){
ScreenManager.getCurrentScreen().checkCollisions();
ScreenManager.getCurrentScreen().update();
ScreenManager.getCurrentScreen().update(renderer);
ScreenManager.getCurrentScreen().update(cam);

}

if(ScreenManager.getCurrentScreen() != null){
ScreenManager.getCurrentScreen().render(batch);
ScreenManager.getCurrentScreen().render(renderer);
}
}

Answer

You need to call

shaperenderer.begin(ShapeRenderer.ShapeType type)

before drawing and

shaperenderer.end();

after drawing end in render of astroids class.