TiledMaps and real objects - iphone

I'm creating a Super Mario Bros. remake for the iPhone using Cocos2d.
I'm having a hard time understanding how add real objects to a tiled map.
I know that you can add an object-layer to the tiled map and set the position of it,
but I'm not sure how to define WHAT object if should be.
For example, I'm creating a simple Super Mario Bros.
I want to add different blocks to the map, one should hold a star, one should hold a coin, and so on.
So my question, how do I correctly create class-objects in a tiled map?

I have used object layers in the past. For you needs you could create an object layer and drop an object on that layer. Type of box. It could have variables in the object of powerup then the value could be whatever you want. Coin, 1Up, Star etc..!

Related

Best way to create an interactive map using an existing image

I'm trying to create an interactive world map like in Europa Universalis IV or Crusader Kings II using Unity3D. These two games create the map using an existing image (like this https://i.imgur.com/y2gtX2N.png). I have never done something like this before and I'm really confused on which is the best approach/technique to use.
What I need is an idea on how to show the map and be able to select every single province taken from the image.
My method would require you to provide each region as an individual sprite.
1) Add all sprites to the scene and form the world map (SpriteRenderer or UI.Image).
2) Make a Component that handles interaction, and add it to each World Map Part GameObject.
Implement the following interfaces to it, and specify the logic accordingly:
3) Add the Polygon Collider 2D Component to each World Map Part GameObject.
4) Add the Physics Raycaster 2D Component to your Camera.
If you choose to use UI.Image (which I do not recommend), you can skip steps 3 and 4, and instead set alphaHitTestMinimumThreshold to 1 on your World Map Part Component's Awake().

Random spawn avoiding colliders

I am working on a 2D game with colleagues that is basically a fighting game on a vertical map with platforms. Right now I am spawning items on the map for player to gather, but I was wondering how could I do that while checking that I'm not in one of the platform's collider to avoid spawning the item in?
You should take a look at the overlap functions from Physics2D (for example here). Just generate a drop position and pass it to this function, if it returns null then the position is free. If you get unexpected results make sure to set the layer mask to the same as the platforms or whatever objects you don't want to intersect with.

Drag and match objects

I want to make a game which needs to drag an object (UIView class object) and intersect with an another object (UIButton) and the same time an event generates , and checks the matching of object is right or wrong.
If wrong the objects come back to its position.
The touch and drag I have done with the object but elasticity of the objects position is not working properly ? Any example or help Please
If you're using standard (non-GL) programming, you can use the object's frame to check if the objects intersect. There's a call for it. You can take the relevant points of one object and check if they are inside the other object's polygon. If they are, then you have a collision. The Quartz 2D drawing tools have lots of intersection methods for these types of objects.

Best Z ordering method for tile-based, cocos2D-iPhone games?

I want to make isometric, tile-based, iPhone games with cocos2D.
Sprites need to be drawn on-top of other sprites that are "behind" it. I'm looking for the best way to do this.
I'd like to avoid the painter's algorithm because it involves sorting all the sprites every frame which is expensive.
The Z buffer algorithm is supported by the GPU and cocos2D so this is what I'd like to use, but there is a problem. Some sprites, like buildings for example, occupy multiple tiles. Assigning a Z value to such sprites is difficult.
These are the options I've thought of:
Comparing two buildings and determining which one is "in-front" is
easy. So buildings can be sorted then assigned a Z value based on
the sort order. This wouldn't be any different from the painter's
algorithm. The OpenGl ES Z buffer wouldn't be necessary.
Assign a Z value to each building based purely on its location on
the map (without knowledge of where other buildings are). I'm
finding this difficult. I think it is possible, but I haven't been
able to come up with a formula yet.
Use multiple sprites for images that occupy more than one tile, so
all sprites will be exactly the same size. Z orders can then be
easily assigned based on what tile the sprite is occupying. The
problem with this solution is that it makes the game logic much more
complicated. All operations on a single building will have to be
repeated for each sprite the building is made-up of. I'd like to
treat each object as a single entity.
Modify the cocos2D code to allow sprites to have multiple Z values
at different points. If a sprite can have multiple Z values based on
what tile a particular part of the sprite falls on, then calculating
a Z value for that section is easy. I won't need to compare the
sprite to any other sprites. I believe this is possible by using
multiple quads for each sprite. The problem with this is that it is
a bit complicated for me since I am new to OpenGL ES and cocos2D. I
don't completely understand how all of the internal data structures
work. Although it seems like the most elegant solution if a formula
cannot be found.
I will up-vote any suggestions or references to helpful resources.
For #2, you can compute the Manhattan distance of the center of the object and use this value as the z-value of that object. It will work as long as you avoid very long objects in your map like 5x1 object or worse. But if you really need a long object to be placed in a tiled map, managing the z-order of objects in the map by setting a z-value using a formula is impossible.
To prove this:
1.) Place two 2x2 objects in a map horizontally and leave a unit tile between them.
2.) Place a 3x1 object between them. Let's name the 2x2 objects to A and B, and the 3x1 object to C.
3.) If you just rotate C(not changing its position), z-order of A and B interchange.
-If B is now in front, some objects behind B will be in front of A because of just the rotation of C. And it's costly to know which objects in back of both A and B previously will become in front of A after C's rotation.

How to select objects in OpenGL on iPhone without using glUnProject or GL_SELECT?

I have 3 OpenGL objects which are shown at the same time. If the user touches any one of them, then that particular OpenGL object alone should display in screen.
Just use gluUnProject to convert your touch point to a point on your near clipping plane and a point on your far clipping plane. Use the ray between those two points in a ray-triangle intersection algorithm. Figure out which triangle was closest, and whatever object that triangle is part of is your object. Another approach is to give each object a unique ID color. Then, whenever the user touches the screen, render using your unique ID colors with no lighting, but don't present the render buffer. Now you can just check the color of the pixel where the user touched and compare it against your list of object color ID's. Quick and easy, and it supports up to 16,581,375 unique objects.
You would have to parse every object of your scene and check the possible collision of each one of them with the ray you computed thanks to gluUnProject.
Depending on whether you want to select a face or an object, you could test the collision of the ray with bounding volumes (e.g. bounding box) of your objects for efficiency purposes.