Adding Collision Detection to SKTileMapNode - sprite-kit

What's the recommended way of adding collision detection to an SKTileMapNode for a side scroller?
Say I have a simple tilemap with just one single tile to use as the ground, how can I detect when my player sprite lands on one of the filled ground tiles?

I'm not entirely sure this will work, but it seems you could add a node to each of a SKTileMapNode's tiles and, based on whatever texture component is on it, give it a physics body appropriately shaped and setup for matching that texture.
Here is someone attempting something like this:
https://forums.developer.apple.com/thread/50043
You don't ask for it, but for greater granularity (eg a tile that's partially covered and needs a sloping physics floor element), it seems this would be the place to start:
https://developer.apple.com/reference/spritekit/sktiledefinition
But I can't find exactly how to know what part of a texture is on any given tile of a tile-map. There must be a way to do this, but I'm just not quite seeing it.

Related

Recommendations for clipping an entire scene in Unity

I'm looking for ways to clip an entire unity scene to a set of 4 planes. This is for an AR game, where I want to be able to zoom into a terrain, yet still have it only take up a given amount of space on a table (i.e: not extend over the edges of the table).
Thus far I've got clipping working as I want for the terrain and a water effect:
The above shows a much larger terrain being clipped to the size of the table. The other scene objects aren't clipped, since they use unmodifed standard shaders.
Here's a pic showing the terrain clipping in the editor.
You can see the clipping planes around the visible part of the terrain, and that other objects (trees etc) are not clipped and appear off the edge of the table.
The way I've done it involves adding parameters to each shader to define the clipping planes. This means customizing every shader I want to clip, which was fine when I was considering just terrain.
While this works, I'm not sure it's a great approach for hundreds of scene objects. I would need to modify whatever shaders I'm using, and then I'd have to be setting additional shader parameters every update for every object.
Not being an expert in Unity, I'm wondering if there are other approaches that are not "per shader" based that I might investigate?
The end goal is to render a scene within the bounds of some plane.
One easy way would be to use Box Colliders as triggers on each side of your plane. You could then turn off Renderers on objects staying in the trigger with OnTriggerEnter/OnTriggerStay and turn them on with OnTriggerExit.
You can also use Bounds.Contains.

Unity Mesh Deformation with Collisions

How to access the 2D mesh of a sprite by code then change the shape of the sprite?
I want to make a game similar to Agario
I just was wondering how to achieve this jelly form when touching objects either by collisions or triggers ?
I would like to see more answers.
Scaling won't get you the kind of deformations you want. Coding deformations the way agar.io does it from scratch is quite difficult. I can see multiple ways of doing this, so I'm going to list them from most recommended to least recommended:
Start with a flat 3D mesh and render your sprite on it as a texture so you basically get a billboard. Then use collision events to get the contact points and use math to figure out how to move the mesh's vertices in response to the contact. You can see someone achieving that effect here and you can see a full blown tutorial for a sphere here, a highly recommended read. Your idea with getting the line from the center of the circle via the contact position and decreasing its length is sound, though the implementation is a bit more complex than that if you want it to behave like agar.io.
Get Anima2D, a free asset that can among other things convert sprites to meshes. Then again use collision events to get the contact points and distort the mesh.
Use Anima2D or a different asset with equivalent capabilities and figure out how to use 2D bones in order to get something like agar.io's effect. You could also try 3D bones on a plane/billboard mesh.
Send the collision data to a vertex shader that is programmed to deform the thing it's rendering.
you can contact gameobjects with Trigger function. That function is working automatically with GameObject's tag names. Here is how you can get Triger function
And also you can change size of GameObjects when they touch each other
More info about scaling
you can code almost anything you want and here is about Mesh of sprite

Can I move dynamic physics bodies using SKAction when only contact detection is needed?

I am looking at tutorial where things are defined like this:
planes are sprites with dynamic physics bodies
plane moving is done with actions by following the path
there is contact detection between bullet and plane
bullet is sprite and it has physics body set to be static (which is little unusual in my opinion)
Here is the link to tutorial for more information.
Questions:
When we use actions to move physicsBodies is there a difference how we set body's dynamic property? Because bullet is static but still there is no problem for movement.
When we have situation like this, where we don't need collision detection, but just contact detection, and we can't move sprites (enemies) by applying forces or impulses, what is a good approach? Is this approach correct?
I think this is nice way, but I would like to be fully aware what is really happening when we use this method and are there any drawbacks or possible problems.
There are posts on SO that suggest we shouldn't use actions for moving dynamic physics bodies. I am aware that we can't use this approach in every case. For example this would not work for moving platform with other object on it, because there would be no correct physics simulation between body on the platform and platform moved by action. But in cases like from this tutorial, where we only need contact detection for object that can be moved only by actions (moved along path) I suppose it's not a problem ?
static means that the body isnt affected by physics. That doesnt mean it cant be manually repositioned or moved. So if something is set to static, it participates in the physics simulation, but it isnt affected by it. Think of a plane hitting a mountain. The plane is dynamic, the mountain is just going to sit there even though its participating in the physics. But you could still move that mountain around manually using a position or an action.
Not totally understanding your question, but I'll give it a shot.
You can move physicsBody's manually (using position property or actions), but you need to ask yourself why you're doing that. You typically don't want to do it because they're bypassing the physics simulation. If you're forcing it to move around, what's the point of using a physics body in the first place.. right?
But if your physicsbody is something like a powerup that is totally static, and you just want it to move around in a circle using an action then thats fine. You probably just want to use contact detection for the bullet, powerup, etc without actually moving it around using physics. Nothing wrong with that.

Breaking up a sprite in cocos2d

I'm making a 2D plane fighting game for the iPhone in cocos2d. I'm trying to make it so when you shoot an enemy plane, it breaks into a couple of separated pieces that will fall out of sight. What is generally the best practice for breaking one sprite up into many? should I create new images for each separate piece, or treat the initial image like a sprite sheet, and make a new sprite from segments?
Please look at this tutorial
It makes a grid of points then moves the internal (not-edge) ones around randomly a bit so it's not all perfect triangles. Then each update it moves/rotates the triangles separately--then draws them all at once.
You treat the whole thing as a sprite, so can run any of the usual actions on it. This example uses CCMoveBy to move the whole group down off the bottom.

Creating a Body - Cocos2d/Box2d

I have a ball and another sprite. When the ball collides with the sprite it simulates falling.
My only problem is the other sprite is just on big image and the ball is on top of it, but there are spaces on the sprite and a lot of corners. I need to determine if the sprite has touch one of the corners. I know this is hard to understand.
So, my question is, is it possible to make a body without
b2PolygonShape blockShape;
and
blockShapeDef.shape = &blockShape;
OR
is there an alternative I can use? I cannot set the image as a box and it would take way to long to set edges because there are so many corners.
I have already set up the collision detection.
I really need help with this.
Thanks!
If you want it to react properly, you have to make a polygon using every single corner coordinate.
But don't be lazy about it. You can use SpriteHelper for creating *b2PolygonShape*s out of your sprites.
Or another alternative: VertexHelper