I'm currently working on a new game in XNA and I'm just setting basic stuff up like sprites/animations, input, game objects etc.
Meanwhile I'm trying to think of a good way to detect collisions for all the game objects, but I can't really think of a fast algorithm, which limits the game to very few objects.
This is what I did in my last project that was a school assignment
public static void UpdateCollisions()
{
//Empty the list
AllCollisions.Clear();
//Find all intersections between collision rectangles in the game
for (int a = 0; a < AllGameObjectsWithCollision.Count; a++)
{
GameObject obja = AllGameObjectsWithCollision[a];
for (int b = a; b < AllGameObjectsWithCollision.Count; b++)
{
GameObject objb = AllGameObjectsWithCollision[b];
if (obja.Mask != null & objb.Mask!= null && obja != objb && !Exclude(new Collision(obja, objb)))
{
if (obja.Mask.CollisionRectangle.Intersects(objb.Mask.CollisionRectangle))
AllCollisions.Add(new Collision(obja, objb));
}
}
}
}
So it checks for all collisions between all objects, but excludes adding collisions I found unnecessary.
To then inform my objects that they are colliding, I used a virtual method OnCollision that I called like this
//Look for collisions for this entity and if a collision is found, call the OnCollision method in this entity
var entityCol = FindCollision(entity);
if (entityCol != null)
{
if (entityCol.Other == entity)
entityCol = new Collision(entity, entityCol.Obj1);
entity.OnCollision(entityCol);
}
Collision FindCollision(GameObject obj)
{
Collision collision = AllCollisions.Find(delegate (Collision col) { return (GameObject)col.Obj1 == obj || (GameObject)col.Other == obj; });
return collision;
}
This made my game run slower fairly quickly when the amount of gameobjects increased.
The first thing that pop ups in my head is to create new threads, would that be a good idea & how would I do that in a good way?
I have studied algorithms a little bit so I know basic concepts and how ordo works.
I'm fairly new to c# and programming overall, so don't go too advanced without explaining it. I learn quick though.
There are quite a few things you can do:
The nested for loops produce a quadratic running time, which grows rather quickly as the number of objects increase. Instead, you could use some acceleration data structures (e.g. grids, kd-trees, BVHs). These allow you to reduce the intersection checks to only the entities that can potentially intersect.
If you can order your entities, you can reduce the collision checks by half by just checking entity pairs, where the second is "greater" (with respect to the order) than the first. I.e. you do not need to check b-a if you already checked a-b.
This part can be potentially slow:
!Exclude(new Collision(obja, objb)))
If Collision is a class, then this will always allocate new memory on the heap and recycle it eventually. So it might be better to make Collision a struct (if it is not already) or pass obja and objb directly to Exclude(). This also applies to your other uses of Collision. You haven't shown the implementation of Exclude() but if it is a simple linear search, this can be improved (e.g. if you search linearly in a list that contains an entry for every object, you already have a cubic running time for just the loops).
The implementation of FindCollision() is most likely a linear search (depending on what AllCollisions is) and it can get quite slow. Why are you storing the collisions anyway? Couldn't you just call OnCollision() as soon as you found the collision? A different data structure (e.g. a hash map) would be more appropriate if you want to check if a collision is associated with a specific entity efficiently.
Finally, parallelizing things is surely a viable option. However, this is somewhat involved and I would advice to focus on the basics first. If done correctly, you can reduce your cubic running time to n log n or even n.
Related
I have searched the net and best possible way I found is this:
Transform GetClosestEnemy(Transform[] objects)
{
Transform BestTarget = null;
float ClosestDistance = float.MaxValue;
Vector3 currentPosition = transform.position;
foreach (Transform CurrentObject in objects)
{
Vector3 DifferenceToTarget = CurrentObject.position - currentPosition;
float DistanceToTarget = DifferenceToTarget.sqrMagnitude;
if (DistanceToTarget < ClosestDistance)
{
ClosestDistance = DistanceToTarget;
BestTarget = CurrentObject;
}
}
return BestTarget;
}
This seems the best way but my real question is, can I use Physics.SphereCast , OnCollisionStay or something to feed this function? I feel like they will be more expensive than just going through all of the possible objects. Is it true? How do these functions actually work?
The function is indeed very well written and optimized. However using Physics.SphereCast and OnCollisionStay to feed it would be nonsensical.
Physics.SphereCast is essentially a "thick" and more expensive raycast. It can tell you if an object with a collider is on its path, with a bit more detailed information about the first collider that was hit. Obviously finding the closest object if you only have one undermines needing an algorithm. If your underlying problem was to find the closest object in a particular direction it could solve it on its own.
There is a related function called Physics.SphereCastAll, which could make a bit more sense, since that one returns all objects in the path of the cast, but your question would have to be something along the line of "which object is closest to this unrelated point among all the objects in front this other point".
OnCollisionStay is individually called every physics timestep for every object with a collider and rigidbody that is in contact with the collider. It's no good for area selection, since it will actively push out all objects inside. You might have meant OnTriggerStay, which does work for an area, but is still not fitting for the problem. The nature of this method would require a bunch of memory to save what objects are in and a chunk of processing power since all that would be done every physics time step (0.03s by default), which is why I wouldn't even consider it. Personally I don't think it is even possible to solve your problem with OnCollisionStay, I would use a combination of OnCollisionEnter and OnCollisionExit if someone insisted on using triggers.
In my opinion the function that makes most sense is Physics.OverlapSphere. It returns an array of colliders within a specified radius. It could basically perform a physics based "prefiltering" so objects at the other end of the scene aren't even considered.
The following code shows an example on how you could use Physics.OverlapSphere to get a set of objects, convert them to transforms, pass them to your function and print the result.
Collider[] hitCollider = Physics.Overlaobjsphere(center, radius);
Transform[] objs = new Transform[hitCollider.Length];
for(int i=0; i<hitCollider.Length; i++){
objs[i] = hitCollider[i].transform;
}
Debug.Log(GetClosestEnemy(objs));
A small quirk is that Physics.OverlapSphere returns the Colliders and any given GameObject can have multiples of those. That means that we might be calculating the same object several times. In this case it is acceptable, since filtering them out will take more processing power than just running them thru another time.
The people that made the physics engine worked hard to optimize it. Nevertheless it can be expensive to involve physics. For example if you are going to check the same five objects over and over again doing a sphere overlap might not make sense. On the other hand if the objects of interest are instantiated and deleted all the time it might be unavoidable to use Physics to even gather a set to check against. For the average user these optimization considerations are unnecessary. Modern hardware usually has enough computational power. Never the less if you are curious you can always program a benchmark, repeating the algorithm a million times and measuring the time that it took using a Stopwatch.
Recently I was working on the door system for my multiplayer game, and I had a problem with door ID (that specifies which door to interact with). I wanted to avoid assigning IDs manually, due to my game has a lot of doors, so I have found a solution - instead of unreliable FindObjectsOfType, I have build following scene looper:
void IterateThroughScene(Scene scene)
{
SortedList<string, NetworkedObject> sortedObjects = new SortedList<string, NetworkedObject>();
//List<CrossSceneAudioSyncListener> audioListeners = new List<CrossSceneAudioSyncListener>();
foreach (GameObject sceneRootObject in scene.GetRootGameObjects())
{
foreach (NetworkedObject netObj in sceneRootObject.GetComponentsInChildren<NetworkedObject>())
{
Vector3 position = netObj.transform.position;
string transformHash = position.x.ToString("0.00") + position.y.ToString("0.00") + position.z.ToString("0.00");
transformHash = Hashing.MD5(transformHash);
sortedObjects.Add(transformHash, netObj);
//CrossSceneAudioSyncListener audioListener = netObj.GetComponent<CrossSceneAudioSyncListener>();
//if(audioListener != null) audioListeners.Add(audioListener);
}
}
//CrossSceneAudioSyncInstance.LoadObjects(audioListeners);
Dictionary<ushort, NetworkedObject> preparedObjects = new Dictionary<ushort, NetworkedObject>();
ushort lastId = _ids[scene.name];
foreach (var part in sortedObjects)
{
NetworkedObject preparedObject = part.Value;
preparedObject.GetObjectData().id = lastId;
preparedObjects[lastId] = preparedObject;
lastId++;
}
_ids[scene.name] = lastId;
_registeredObjects[scene.name] = preparedObjects;
}
Here I list step-by-step what this method is doing:
SortedList is instantiated to hold objects
2 foreach's are getting all objects in the scene
here script generates MD5 of position and adds it to SortedList
After that, Dictionary instance is created to hold final data
Next foreach is done, to loop through SortedList and assign NetworkedObject IDs in a reliable way.
This was doing a great job in small/medium scenes, but I started having serious problems with big scenes - I have a good PC & itaration takes 12 seconds, and my game's target are medium PCs.
So, Do you have any idea how can I optimize this algorithm?
As I do not know the internals of FindObjectsOfType it is hard to compare the method to your snippet. You did however mention that order matters, and as FindObjectsOfType is not guaranteed to return objects in any particular order, it would not work for your use case.
To optimize the snippet you would need to prune your search in some way. As you are looking at all root gameObjects, you can mark each root object with the number of doors that it has as children, and keep a counter for each root. That way, once you reach the number needed sent from the children, it can stop searching that root object.
However, as it seems you would just like to assign ordered ids to objects in a scene for referencing, I would advise to use an editor tool to handle the assignment. The code would be near identical but with the advantage of not needing to care about overhead during runtime as it is computed outside of the game. When implementing any editor tool that alters your scene, make sure to mark the scene as dirty to assure Unity will save the changes.
In our geometry library, we have a Polygon class, containing (amongst many others, of course) a method which checks if this polygon contains another polygon:
public bool Contains(Polygon other) {
//complex containment checking code goes here
}
Other important things to note are that the polygon is mutable, and that there is no way to be notified of when its shape changes.
Now, in one of our projects, we have a validation function, which checks for a huge number of polygons whether they are all contained within a single outer boundary.
Polygon boundary = ...;
foreach (var poly in _myMassiveListOfPolygons)
if (!boundary.Contains(poly))
//error handling code goes here
Profiling has determined this to be quite a bottleneck. Fortunately there is a way to vastly speed up the calculation. We know that the boundary polygon is always convex, so instead of using the general Contains method, we can write a specialized method that assumes the outer polygon is convex.
Initially I added something like this to the client code:
Func<Polygon, bool> containsFunc;
// Keep both options, in case my assumption about being always convex is wrong
if (boundary.IsConvex())
{
containsFunc = p => { ... }; //efficient code for convex boundary goes here
}
else
{
containsFunc = p => boundary.Contains(p);
}
foreach (var poly in _myMassiveListOfPolygons)
if (!containsFunc(poly))
//error handling code goes here
Joy all around! Performance has increased tenfold!
However, it doesn't seem right that the code for this alternative Contains method is located in the client project, where it can't be reused by others.
So I tried to move it to the Polygon class itself.
public bool Contains(Polygon other) {
if (this.IsConvex())
return ConvexContains(other);
else
return GenericContains(other);
}
private bool GenericContains(Polygon other) { ...}
private bool ConvexContains(Polygon other) { ...}
With this method, the client code returns back to the original. Unfortunately, there is one big difference compared to the previous code: Before there was one call to boundary.IsConvex() to select the correct function to use, where now this method is called for every invocation of Contains! Although this is still faster than using the generic Contains method, most of the performance improvements are lost again (not to mention performance decreases for concave polygons).
A third possibility would be to have two public methods to check containment, where one of them assumes that the polygon is convex (without doing the check itself, of course, otherwise we're back to the previous overhead problem). This doesn't seem like a good idea, since calling it by accident with a concave polygon could give unexpected results, making for hard to find bugs.
So finally, my question is, is there any good way to deal with this situation? Where should we put the alternative Contains method, and how should we call it?
Edit
I like the idea of caching whether or not a polygon is convex. But I cannot change the interface nor the behavior of the class, which is problematic in a case such as this:
//relevant part of the polygon interface
public List<Point> Points { get; set; }
//client code
var poly = new Polygon();
var list = new List<Point>
{
new Point(0,0),
new Point(10,0),
new Point(0,10)
};
poly.Points = list;
list.Add(new Point(1, 1));
This last line of code is executed on a regular List<Point>, there is nothing I can do about that. However, there would now be 2 requirements:
The polygon must now contain the added point. This is to ensure existing code doesn't break. This means that we cannot copy the Points into our own, observable list.
We must get notified of this change to the list of points (because our polygon has turned from convex to concave). This means that we cannot wrap the list by our own, observable list, because only changes made through the wrapper would cause a notification.
One option is to create another concept in the geometry library, say FixedBoundary, which could encapsulate that logic. It would be immutable so that you could safely cache the convexity. An example:
public class FixedBoundary
{
private Polygon boundary;
private bool isConvex;
public FixedBoundary(Polygon boundary)
{
// deep clone so we don't have to worry about the boundary being modified later.
this.boundary = boundary.Clone();
this.isConvex = this.boundary.IsConvex();
}
public bool Contains(Polygon p)
{
if (isConvex)
{
// efficient convex logic here
}
else
{
return this.boundary.Contains(p);
}
}
}
This of course adds some conceptual weight to the geometry library. Another option would be to add an ImmutablePolygon (and by extension ImmutablePoint) which could do the same, however conversions may be cumbersome and affect performance as well.
You could replicate what you've done already with the Func<T, bool> delegate.. internal to the Polygon.. perhaps like this?
private Func<Polygon, bool> _containsFunc;
// ...
public bool Contains(Polygon other) {
if (_containsFunc == null) {
if (this.IsConvex())
_containsFunc = ConvexContains;
else
_containsFunc = GenericContains;
}
return _containsFunc(other);
}
Each call to Contains after the first will not call IsConvex. Unless I have misunderstood you.. that sounds like what you're after.
Okay...you guys really shot yourselves in the foot by using List in your public interface. Here is how you can try to solve it (there is a small non-zero chance that this will incorrectly cache, but its a 1 in (1 <<32 ) chance.
As you have noted, we could cache the polygon's Convexness...but the problem then becomes a case of Cache invalidation.
Without being able invalid cache on object change. You would have to check for cache coherence on each call. This is where hashing comes in.
By default the hash for list is quite useless, you want to use a this solution for getting a usable hash.
So what you want to do is to add a nullable<int> _cachedHashCode on your polygon. When you call Polygon.IsConvex, you hash your List<Point>, compare with your _cachedHashCode, and if they don't equal, recompute your IsConvex.
I'm a fairly inexperienced programmer currently learning C# and trying to learn Game Design in general.
I'm using Microsoft's XNA framework to build a Galaga-esque Scrolling Shooter game. After a few rough trials in which I struggled with shoddy OOP structure, making some bad design choices, I've finally come up with a respectable start of an engine.
Presently I'm having problems with making collision detection not lag the game out. My current engine keeps all active game objects in a List object and cycles through each, checking if it collides with each other object. Needless to say, it could be a lot better.
Here's my collision checking, done in the ObjectHandler class.
public override void Update(GameTime gameTime)
{
...
//Handle collisions
foreach (GameObject obj in Objects)
{
ICollideable e = obj as ICollideable;
//Check if the object implements ICollideable
if (e != null)
{
//Check collision with each other object
foreach (GameObject obj2 in Objects)
{
//Check if the second object implements ICollideable
ICollideable e2 = obj2 as ICollideable;
//check if they are in the same sector
if (e2 != null && SameSector(e.Sector,e2.Sector))
{
//Check if the collision masks interesect
//if so call each object's collision event
if (e.Mask.Intersects(e2.Mask))
{
e.CollisionEvent(e2);
e2.CollisionEvent(e);
}
}
}
}
}
...
}
Here's the SameSector function.
private bool SameSector(Point p1, Point p2)
{
if (Math.Abs(p1.X-p2.X)<=1 && Math.Abs(p1.Y-p2.Y)<=1)
return true;
else
return false;
}
"Mask" here is a Rectangle object, which is part of the XNA framework. As you can see I've implemented a sort of spacial partitioning system, where each object sets which 60x60 square it's in. However, I'm not really sure that I've done anything useful as it takes just as much time to check if two objects are in the same sector (or adjacent sectors) as it does to check if they're colliding.
I've seen a question similar to this posted already, but it didn't quite satisfy my question. From it I did gather that a time management system is useful. I'll try to implement that eventually but as I'm still fairly new to programming, I'd like to optimize the collision checking itself before delving into more advanced design.
So, is there I way I can effectively optimize my current collision checking, or is what I have way off to begin with?
Spatial partitioning will only help you if you've implemented it in a way that doesn't require you to compare every object against every object in some way.
In your spatial partitioning grid, you want each grid cell to have some kind of membership list of objects within it, as well as for each object to be aware of what cell it's in. Then you only need to do comparisons between an object and all other objects within that cell and within immediately adjacent cells (because an object may overlap bounds). The downside is you now need to keep all this state updated.
I highly recommend the book Real-Time Collision Detection which covers several different broad-phase partitioning schemes in depth and their relative strengths and weaknesses, in addition to other CD topics. In addition to uniform grids, there are hierarchical grids, quadtrees, sweep and prune, and other techniques that may be more appropriate for you (sweep and prune might be particularly beneficial).
I have been developing a game in my spare time for the past few months. One sticking point that I have reimplemented over and over and not got 100% working is the collision detection. My system (posted below) mostly works however random things seem to happen from time to time, like the player being pushed outside the bounds of the level etc. Tutorials on the matter I have come across seem to offer basic workings, i.e, you have to know an object will be in relation to the object in question, but in a real game you wouldn't really know this. Here is my implementation but what I am after is if anyone knows of a good solid system for achieving what I am doing here.
Note that item comes from a collection of IItem which exposes the Rectangle and a few other bits for each item.
public void Move(float xAdditional, float yAdditional)
{
X += xAdditional;
Y += yAdditional;
foreach (var item in Level.Items)
{
if (item != this)
{
if (item.Boundary.Intersects(this.Boundary))
{
if (item.Boundary.Top > this.Boundary.Top) //we have collided with an object below us.
{
Y = item.Boundary.Top - (this.Boundary.Height/2);
}
if(item.Boundary.Bottom < this.Boundary.Bottom)//we have collided with an object above us.
{
Y = item.Boundary.Bottom + (this.Boundary.Height/2);
}
if(item.Boundary.Left > this.Boundary.Left) //We have collided with an object to the right.
{
X = item.Boundary.Left - (this.Boundary.Width/2);
}
if(item.Boundary.Right < this.Boundary.Right)// We have collided with an object to the left;
{
X = item.Boundary.Right + (this.Boundary.Width/2);
}
}
}
}
}
The final solution was to drop my own solution and implement Farseer.
Thanks
After some time with it I then opted for physics2d.net which I have found much more usable from a developers point of view.
First of all, Collision Detection depends heavily on the object geometry.
There are multiple proven to work solutions cast into linear algebra math,
but they are dependent on the geometries you try to use. Rather than
implementing an algorithm using your object's properties try to implement a mathematic equation directly and then use the result to determine if your collision happened or not. (like using vectors t = a-b, if |t|>0 collision, else not).
If you have a simple geometry like rectangles or circles you should take a
look into 'Mathematics for 3d game programming & computer graphics' by Eric
Lengyel.
Also keep in mind that you can achieve collision detection with other methods
like sprite collision. Another possibility is to use a higher level framework
doing this work for you (e.g. OSG)