Reputation: 514
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
Upvotes: 3
Views: 1784
Reputation: 208
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 boolean
s 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.
Upvotes: 3
Reputation: 89
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);
}
}
Upvotes: 1