I have a particle emitter and I would like to detect it when the particles collide some physics bodies.
Is there a native way to do that in the SpriteKit API or do I need "to cheat" ?
Individual particles can not collide. Not with physics, not any other way. You do not even get any information about an individual particle - you can't access it's position, rotation, velocity .. nothing.
If you wanted to "cheat" you'd have to emulate the particle emitter using sprites, and animate the sprites with actions or manually. However keep in mind that this is much less efficient than a particle emitter.
In addition, if we're talking "particles" which often means dozens or even hundreds of them on screen, the amount of physics processing and collision detection quickly becomes prohibitively expensive if you were to model them using sprites with physics bodies attached. Do a performance test before you go down this path.
Particles do not have physics bodies, so they don't collide with Sprite Kit's physics engine
You can set the physics body of the particle emitter the same way you set it for any sprite node. Then you can set the category bit mask property and the contact test bit mask. The method didBeganContact can detect the collision afterwards.
didBegan contact is called whenever two bodies contact each other . Here is the apple reference link for how this method works:
Click [here](https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKPhysicsContactDelegate_Ref/Reference/Reference.html#//apple_ref/occ/intfm/SKPhysicsContactDelegate/didBeginContact:"Apple iOS developer library")!
Related
I've been having difficulty implementing enemies with a billboarding system for a while:
For a new project I'm reusing some animated sprites from an old static camera shooter game I made. It consisted of shooting projectiles and hitting enemies advancing towards your position. The enemies were capsules which had an animated plane in front of them.
I wanted to go a step further and make a raycast system that would detect if the impact on a plane is on the alpha part of its texture. I achieved it using a SpriteRenderer and a plane that matched the size of the sprite which receives the impact of the Raycast.
The problem comes with the movement of the enemies. If I use Rigidbodies this conflicts with the plane which detects the impact, since I have to use a non-convex Mesh Collider for RaycastHit.textureCoord (With Convex colliders it doesn't return the correct position). I need Rigidbody based movement for the enemies to push each other when colliding.
I tried using Transform Movement, since the error does not occur, but this allows enemies to pass through each other and occupy the same space.
I started investigating navMesh but I'm not sure if it is possible to avoid collisions.
I started to place a group of trigger colliders on the enemy mimicking the shape of the sprite. But this makes it difficult for me to detect collisions when enemies do certain animations (Some enemies jump diagonally or their animations are a bit jittery).
I would like to keep the impact detection system for the sprite, but maybe there is another way to check the texture at the impact location.
I think my options are:
With rigidbody based movement: separate the animation and hit detection system from the enemy movement. And make the billboarding effect teleport to the object constantly.
With Transform movement: Detect if they are overlapping and move them in opposite direction to the overlapping to simulate the collision.
With motion based navMesh: I'm not sure if it will be useful to me, since the characters jump and I also believe that a Mesh Agent ship cannot simultaneously be an obstacle. I understand that the navMesh cannot be generated in runtime.
Leaving texture based detection and using the trigger colliders group: Animate the colliders to also follow the animations. Or simplify everything to a large collider, losing precision on impact.
-Find an alternative method to detect the impact coordinates in the texture, avoiding using RaycastHit.textureCoord and therefore Mesh Collider. (Although I would like to keep the impact detection system as it is, maybe there is a similar approach for the same result).
The reason I have not yet delved into these solutions is because I would like to keep the system simple. I'm trying to avoid separating the enemy into 2 gameobjects, simulating collisions or animating collider positions. I honestly don't know which way to go in order to move forward with the project.
I am trying to use Unity's physic scenes to perform some simulations outside of the main scene. That being said, I need all of the colliders from the main scene in the physics scenes. What is the easiest way to create a physics scene with all of the existing colliders from another scene? Do I have to loop through everything, use SceneManager.MoveGameObjectToScene(...) for every object then move them back at the end of the simulation? This doesn't seem scalable.
EDIT
I have a multiplayer game, when I get responses from the server I am taking their velocity and the servers position, then applying all local movement since that packet was sent via Physics2D.Simulate() to have the velocities correctly applied to the position. Now I need to do the same for the projectiles so I would need the map colliders as well as the player's. I was reading the best thing to do for physics sims is use physic scenes. If I call Physics2D.Simulate() in the main scene for more than 1 set of data (player movement vs projectiles) places I got a lot of screen jitter. Moving these simulations into separate scenes removes the jitter.
The only alternative I see is re-implementing Physics2D.Simulate() myself which seems like a bad (and complicated) idea.
Thanks.
I ended up only needing the collider and rigid body so I created an empty object in the physics scene with these 2 objects. Every time I needed to run the simulation, I would update the rigid body with the current players rigid body details then run the sim and apply the output velocity from the sim to the players rigid body again.
I have a Unity project in which there is a 2D game world that consists of static colliders to make the geometry solid to the characters that inhabit it. The player is a dynamic collider (with a non-kinematic rigidbody). There's also an enemy character that is also a dynamic collider. Both characters walk over the floor and bump into walls like I'd expect them to.
What I want to achieve is that the player and enemy are not solid to each other, so they can move through each other. I achieved this by putting the enemy and the player on separate layers and setting the collision matrix so that these layers do not collide with each other. The problem I'm having now, however, is that I do want to detect whether or not the enemy and the player ran into each other. I added a trigger collider to the enemy character, it's on the enemy layer which means it doesn't detect collisions with the player.
I thought of making a sub-gameobject for the enemy, put it on the player's layer, add a rigidbody and trigger collider to it and use that to detect collisions between the player and the enemy, but it feels so convoluted that it leaves me wondering if there isn't a more elegant solution for this.
Edit (2020-05-26): Nowadays you can tell the engine to ignore collisions between two given objects. Kudos to Deepscorn for the comment. Will leave the rest of the answer unchanged, and read with that in mind.
Yes, you need to create a child GameObject, with a trigger collider, and put it in a layer that interacts with the player layer.
No, you don't need to add a Rigidbody to the new GameObject, the parent's rigidbody already makes it a dynamic collider, so you will get OnTrigger events.
As a side note, just to keep things organized, if you create a child of the enemy don't put it in the player layer. For example, in the future you might need to disable the player's layer collision with itself. Furthermore, if your player interacts this way with many objects, I'd put a single trigger on the player instead of the enemies, on a separate PlayerTrigger layer, just to keep things simple.
Isn't there a simpler way? Not really. You definitely need non-interaction between the player and enemy colliders, but some kind of interaction between them too. So one of them needs to span two layers, or the whole interaction would be described by a single bool. The physics engine processes lots of information in one go, so you can set all the layers and collisions you want, but during the physics loop you have no further control on what happens. You can't tell the engine to ignore collisions between just two objects. Having only 32 layers, and having them behave in the rigid way they do are actually heavy optimizations. If you are concerned about performance for creating another layer, disable interaction between layers you don't need, like the trigger layer and the floor and walls, or layers that don't even touch.
Your alternative is doing it all by code, which is even less elegant. A single child capsule on the player doesn't sound that bad now, doesn't it?
Does anyone know how I can detect when an SKEmitterNode particle makes contact with a SKNode in the SKScene?
I want to apply a little force to the SKNode when a particle makes contact with it for a 2D game I'm creating.
You can't. You can't get the location, size and other properties of individual particles.
The essence of a particle system is that its particles are minimalistic and under full control of the particle system. Hence you don't normally get any access to individual particles in a game engine, mainly because it wouldn't make sense to do so.
There can be specialized particle emitters built on regular sprites where you have access to particles and where particles can have physics bodies. Solutions exist for other engines but I'm not aware of one that works with Sprite Kit. These are always slower than regular particle emitters, especially with physics enabled, forcing you to use a lot less particles to begin with.
Hello everyone I an very new to cocos2d, so I apologize if this is a simple question. I would like to create to sprites that collide when they hit each other.
For instance, one sprite, spriteA, is in a fixed position on the screen. And another sprite, spriteB, is moving around the screen. SpriteB will collide with spriteA. If that doesn't make sense or you don't understand it, tell me and I will elaborate further. Any help is appreciated. Thank YOU!
Try using Chipmunk or Box2d physics systems. These are included with Cocos2d and work by having a physics simulation that updates with every time the graphics change on screen.
The physics simulation will tell you if two objects are overlapping, and will provide physical attributes like weight, slipperiness (friction), speed and direction, which creates reactions like bouncing, sliding, realistic loss of speed and changes of direction on impact.
If you are new to physics simulation, here's a 30 second run down:
Create "bodies" in the physics simulation that represent each graphical sprite
Bodies are usually defined more simply than their graphical counterparts, like a circle, square or simple polygon shape
In order to update the graphics on the screen accurately, first you establish a pixels to meters ratio. Meters are notional (i.e. made up) measurement that is used in the physics sim
Each time the physics simulation "ticks", you update the graphics on screen accordingly
So if a body in the physics sim moves 1 meter, you might transform the pixel sprite 32 pixels
Here's a good tute on collision detection using Box2d.
http://www.raywenderlich.com/606/how-to-use-box2d-for-just-collision-detection-with-cocos2d-iphone
enjoy
Its actually very simple:
Just schedule a timer: [self schedule:#selector(checkForCollision:)];
Specify the method: - (void)checkForCollision:(ccTime)dt {}
In the curly braces, make CGRects for each sprite using CGRectMake.
Then in the same method, just call: if (CGRectIntersectsRect) {}
That easy!
Well technically speaking when the 2 sprites interact, or share at least one common point, then they are colliding. I'm a little confused by your question. Are you asking for direction on how to make the sprite move on screen, or are you asking how to handle the actual collision(such as calling a method if they collide)?