3
votes

I developed a 2d game in android studio using SurfaceView, it's not complex in context of collision, just need to check collision between a moving-point and some static circles, for detect collision for one circle, I simply check if X of the point is between circle minX and maxX && point Y is between minY and maxY of circle.

So for checking collision in whole of the game, I repeat to check above code for all circles in each frame.

Game work so good when I have for example 10 circles, but if I add 30 circles, its FPS decrease so much and I face so much lag!

What should I do for this problem? should I use Box 2d physics ? what does it do for collision detection that games doesn't face lag problem even if there is so much objects which collide together?

Please help me with more detail, because I was wonder how a game engine work and decided to make a simple one, not only wanted to make and release a game(otherwise I could use a ready game engine).

Thanks in advance

2
So you have circle objects but their collision boxes are rectangles?Phil90
yes , because I check if(circle.minX <= point.X <= circle.maxX && circle.minY <= point.Y <= circle.maxY), but its no matter really :) I got lag when increase number of circles @AppPhilMehDi
What kind of a map do you have? Or what type of game? Is it a sidescroller or something like that?Phil90
no its a simple game with an constant surface(map), no scrolling available. do you think my algorithm has no problem and I should check other parts of my game? Isn't consume to check above code in each frame for 30 circles ? @AppPhilMehDi
@MehDi You need to create a binary map consisting of circle points as 1 and other as zero then it's easy to find an object is inside or outside the circle. But, I've not understand your problem actually, do you want to prevent object go inside circle or something else ?Sagar Gautam

2 Answers

3
votes

As for how game engines do it, it is probably easiest to look at their source code directly, as at least one version of Unity can do it - you can find the source code of Unity here.

In your case, you could potentially trade off increased memory consumption by your app to make collision detections essentially constant-time regardless of the number of circles, as suggested by @SagarGautam in his comment.

What you can do to achieve this is store a 2D array of booleans indicating all map pixels, setting each element to true if it is inside a circle (as per your previous code) or false otherwise. Doing this during the loading phase of the level/map should be fine, because now during rendering you can just look up the pixel coordinates in the array and see if it is inside a circle or not.

1
votes

In 3d game i use Colliders so check if there is any collider(mesh,box,etc.) give tag to that objects.and identify them by tag Example:

using UnityEngine;

using System.Collections;

public class ExampleClass : MonoBehaviour {

    void OnCollisionEnter2D(Collision2D coll) {
        if (coll.gameObject.tag == "Enemy")
            coll.gameObject.SendMessage("collided", 10);

    }
}