Search code examples
javaandroidlibgdx

How to draw circle and collide it? Android/Libgdx


Hey I'm currently programming a Game where the user (displayed as Spaceship) has to avoid Asteroids. First of all I want to do this is in a really simple way. I would like to display the asteroids as circles and want them colliding to each other. Can someone answer me these Questions:

How do I draw a circle in Android/Libgdx/Java? How do I create random movement of the circles? How do I make these circles collide with each other and how to collide them with a Sprite in a simple way?

Btw, You don't have to answer all the questions :)


Solution

  • Well, you got a lot of beginner questions. No evil in that, but you should ask the google beforehand. I will answer your question with Canvas for the demonstration. Which contains prepared methods for basic shapes like circle.

    First I would recommend you reading Android guide lines such as this

    Second thing is collision. To check whether two circles are colliding you just take the distance of their centers (d) and compare it with the sum of their radii (r). Now if (d <= r) then collision is detected. Very good explanation is found here.

    Third is the random movement. Here is the idea. Lets say we get random number for every circle for example from 0 to 100. If the number is below 50, its moving to right, else to the left. Of course you are welcome to add your creativity to this idea.