I have 3-4 players in game and many objects (like stone, tree, cactus..etc). I have to find the collision detection between the objects and the players.
What is the easiest way with the speed performance to find the collision between objects and players.
If I pass player position for step movement to object class to find the collision detection it might be slow performance (each_step_of_player * all_objects).
Another idea I had is that I will write a schedule function for each object in Object class and for each schedule function call I will check collision with all player position ( Object * all_players). How is this idea regarding the speed performance?
Any another idea will be appreciate?
You can use grid-based collision detection.
Break your screen into grid of square cells, each cell at least as large as the largest object (this is important). Each object is assigned a cell, based on where the center of the object is. When you move the object, you reassign it to corresponding cell. Now you don't have to check an object against all other objects for collision, only against objects in its cell and adjacent cells.
Related
I'm developing a Dungeon Crawler style game and set a bunch of position objects as a "grid" for the player walk and stored them in a parent object, like a list. So I wanted to use that same list for the enemy moviment but after watching many A* Pathfinding tutorials for weeks I didn't managed to have any ideas of how to solve it since I'm not using a regular 2D grid. Here's a reference.
I took a look at your image, and you do in-fact have a grid. Just because you placed your objects does not mean you can calculate the "grid" for them from the list.
I would suggest you create a function that you call when your level starts.. and it iterates through the children of that object and generates a data structure that allows you to use A* to calculate pathing. You can use the transform.position of each object to determine it's relative placement/adjacency.
There is many ways you can do this. One suggestion would be creating an invisible collider with a trigger (can be on an empty GameObject) in different areas on your grid. Give the enemy a default velocity and attach a collider to the enemy and select the checkbox for trigger so when the enemy moves onto the trigger, you can call OnTriggerEnter (from a script attached to the enemy or the invisible collider GameObject) and make it change its movement. How you want to implement the AI is up to you.
edit as per comment
put invisible game objects positioned in your scene to how you would envision them being in an array from a birds eye view. In a script assign them to an array. Get the enemy to lerp between points in the array based on your ai algorithm. https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html
I am making a 2D game and I have created two circles, one named player and one named enemy. The circles are drawn by calling a drawPlayerCircle and drawEnemyCircle function, both which are called in my display() function. At the moment my project isn't Object Orientated which you can already probably tell. My collision works for both circles as I have calculated penetration vectors and normalised vectors etc... However my question is, expanding this game further by having 10-20 enemies in the game, how can I work out the collision to every single enemy quickly rather than calculating manually the penetration and normalised vectors between my player and every enemy in the game which is obviously very inefficient. I'm guessing splitting my player and enemy entities into their own class but I'm still struggling to visualise how the collision will work.
Any help will be greatly appreciated! :)
Collision between two circles is very quick to test. Just check if the distance between the circle centre points is less than the sum of the two radii.
I would just start by comparing the player against every other enemy. Doing 20 comparisons is not going to tax any computer from the last 10 years. I'm a big believer in just writing the code that you need right now and not complicating things, especially when you are just starting out.
However if you end up having millions of circles and you've profiled your code and found that testing all circles is slow, there are quite a few options. The simplest might be to have a grid over the play area.
Each grid cell covers a fixed amount of space over the play area and stores a list of circles. Whenever a circle moves calculate which grid cell it should be in. Remove it from the grid cell that it is currently in, and add it to the grid cell that it should be in. Then you can compare only the circles that overlap the player's grid cell.
Space partitioning. Specifically, if you're in 2D, you can use a quadtree to speed up collision detection.
I have had a good look at other similar questions here but cant seem to find one that answers my question. The answer may be simple in which case please excuse my ignorance..
I have a game with a large amount of balls (over 100). These are created dynamically depending on the amount I want. These balls are physics objects which must collide with each other, which they do by default (no collision group assigned). I also have a container in the game which the balls should not collide with but which i use to detect collision callbacks.
The problem comes when i try to assign groups to the balls and the container to prevent the collision with the container. If i assign the same collision group to all the balls:
// This is looped through and applied to all the 100+ balls
ball.physicsBody.collisionGroup = #"ball";
and to the container:
container.physicsBody.collisionGroup = #"ball";
then the balls will not collide with the container but they also do not collide with each other.
How would i go about making all the balls collide with each other but not with the container whilst still being able to get the collision callback from the balls intersecting the container? Do i need to apply unique group or category names to each of the balls?
I have a sprite with a triangle shape and I want to know when this triangle intersects with another object (a CCSPrite). I have been using CGRectIntersectsRect but it is not accurate because it take the bounding box of the the two sprites and not the actual shape.
--Edit
I think one way is to define several points around the actual triangle and check intersection between those points with another sprite. I am just wondering if there is an easier way to do this.
You can use box2d to detect collisions accurately. It can be useful if you have many different complicated shapes. Or you can just check intersections of shape's edges.
If there is many objects to detect collisions with, I offer to use box2d. It has good internal optimizations to be able to work with large amount of objects. In this case you will just have to create physical body equal to the your sprite's shape before adding object to your game layer.
So with Tiled, I can set Tile Properties directly on a tile before placing it on a map like so:
This is how I have done collision checking, by setting the collision property to 'true' and then checking the tile properties when moving a sprite.
However, I would like to add a 'teleport' tile. When the player walks on a specific tile, it takes them to a separate location.
The problem I am running into, is that when you set a property on a tile, you only get to set it once, and not on the tile instance. Meaning every tile would have the same teleport location.
Am I overlooking something? Is there a better way to go about doing this in Cocos2d in general?
You can use the object layer for this. Add an "object" (that's just a rectangle or point in Tiled) to the teleporter tile and use the object's properties to connect two locations together.
When you load the map you could walk over all objects to find the connecting objects. Then you know the two tile locations of the teleporter end points which you could store in a teleportation array. Every time your player moves to a new tile, check the teleportation array to see if the player is on one of the teleportation fields, and if he is, move him to the other teleportation tile.
Of course you could also check intersection with the object (rectangle) but since there's a chance that you might accidentally create an object (rectangle) that spans multiple tiles it seems more reliable to check these objects before the game starts.
Well this probably is the best way but it's what I've done. You could create a meta layer and have separate tiles for each teleporting pad. So when you check if the player is on teleportingpad1 you set the players location to receiverPad1 (which could be another tile, object in tiled, or just a point you set when you check for collisions). And you would just make another one e.g. teleportingpad2, teleportingpad3, etc. for more pads.