Using tiles in SFML and collision detection - c++

I decided to look a good tileset to use and found some, but the question is how I actually load the tile from the image file as there seem to be more than one tile in one file?
Also how do I implement a collision detection for non square tiles?
Example: Tileset

Images(sfml1.6) or Textures (sfml2.0) are usually drawn using sf::Sprite.
The usual way to do it is to let all sprites share the same tileset texture/image, and then use sf::Sprite::SetSubRect(const IntRect &SubRect) to set the area of the texture that should be drawn.
The Sprite class in the sfml API is probably a good place to start
http://www.sfml-dev.org/documentation/2.0/classsf_1_1Sprite.php
http://www.sfml-dev.org/documentation/1.6/classsf_1_1Sprite.php

Related

Is there a way to detect the pixels of a sprite for a collision in SFML?

It doesn't have to be pixel-perfect collision, but I want it to be as close as possible to the actual pixels of the sprite. FYI, I created a 32 by 32 sprite but then I was only able to fill estimately half the amount of pixels, so the rest is just transparent.
Most games out there don't use anything close to pixel perfect collision and it's usually not needed. Having some approximated rectangle or a combination of multiple rectangles is usually enough.
SFML itself provides intersects() and contains() functions for it's sf::Rect<T> class.
There's also some collision detection class in the SFML wiki which also features a bit-mask collision, that's basically a pixel-perfect collision detection.

SFMLtexture shaped like player

So I've been making my own shooter in C++ using SFML library. I couldn't find a proper answer to a problem I've encountered. Is there any function in SFML that would set player and enemies sprites not to the size of their textures (just rectangles), but to the "colored" areas of the source png file so the sprite would match the shape of player?
So from what I understand you probably want to create a sprite with the same bounds as your texture assuming your player is "colored" and the rest in you texture is transparent. First, your texture and the sprite (sf::Sprite) will have the same bounds as the image (png) and the scale you set, depending on the image size. However, the only thing that will be drawn onto the screen will be your "colored" part since the rest of the image has 0 as the alpha value (transparent). So there is no actual need to create a sprite which has the same "bounds" as it's visible parts. Unless, you are handling collisions.
If you are indeed handling collisions, please look into something called pixel perfect collision detection. (SFML "intersect" function uses Bounding Box detection, if you're using that.)
If this is not an answer you seek, please elaborate the question and I'll help out :)

Cocos2d - hiding, clipping or masking sprite like in game Candy Crush

I have a problem and I saw it also in the game Candy Crush Saga, where they successfully dealt with it. I would like the sprite to show only when it is in the board (see image link below). The board can be of different shapes, like the levels in the mentioned game.
Has anyone some ideas how can this be achieved with Cocos2d?
I will be very glad if someone has some tips.
Thank you in advance.
image link: http://www.android-games.fr/public/Candy-Crush-Saga/candy-crush-saga-bonus.jpg
In Cocos2d you can render sprites at different z levels. Images at a lower z-level will be drawn first by the graphic card and the images (sprites) with a higher z-value will be drawn later. Hence if an image (say A) is at the same position of the other but has a higher z-value you will see only the pixels of image A where the two images intersect.
Cocos2d uses also layers so you can decide to add Sprites to a layer and set the layer to a specific z value. I expect that they used a layer for the board (say at z=1) with a PNG image containing transparent bits in the area where you can see the sprites, and a second layer at z=0 for the sprites. In this way you can only see the sprites when they are in the transparent area.
Does this help?
I found out Cocos2d has a class CCClippingNode which does exatclly what I wanted. First I thought it can clip only rectangular areas, but after some research I found it can clip also paths.

Cocos2d-x TMX maps sharing Texture image file with game sprites?

I am using cocos2d-x 2.1.4 and would like to use the same image file e.g. spritesheet.png for
the tiles on a tmx map built using Tiled Map Editor as well as;
in-game sprites, which I usually make the spritesheet using TexturePacker.
The reason for combining is to reduce the number of draw calls, since there is enough texture space for the sprites and tiles to all be shared on one image. If I used TexturePacker with Tiled, I think I would have to reassign tile positions every time TexturePacker rearranges the tile images, so that is not a good approach.
I know I can get a CCTexture2D* from a CCSpriteframe, which I wanted to try to insert into CCTextureCache, so that the CCTMXLayer can pick up the texture (which is actually from a spritesheet). However, there is no method to do so.
What is the common technique of dealing with a combined spritesheet for both tiles and sprites in cocos2d-x?
Change the sort algorithm in TP to name, then prefix your tile sprite images with _ or some other character to make sure they come first in the sprite sheet. You may want to create dummy tiles to reserve space for potential future tiles to avoid the risk of the tile sprites being rearranged when you add some more tiles in between.
Regardless of that, one additional draw call isn't going to make a difference.

cocos2D collision on edge shape only

I'm developing an iOS game with cocos2D.
My game is simple, there are levels, and a rotating sprite.
The sprite need to go from the beginning to the end of the level without losing his lives.
So there is two possibilities for me :
1°) Already working good
Tilemap based levels with 2D pixels styles tilesets
Custom collision detection on the edge of the hero's sprite bounding box, and the tilemap collision.
2°) Would be better graphics, and better users experience (without physics, only collision):
map base on vector graphics / SVG
collision detection using the edge of the hero's sprite shape and the map
But, i read the cocos2D/Box2D documentation, and i doesn't found a collision detection on the edge of the sprite's shape ONLY. It's like a pixel perfect collision (already found algo).
I only want to know if one of the 4 edge of my hero's shape is colliding a border of the level, and if yes which shape is colliding (because my sprite is rotating).
Someone have an idea ?
Thank you a lot for your time.
One polygon shape should be attached to your Hero's body via fixture.
To detect a collision point use contacts between dynamic(hero) and static(walls) bodies.
Just take your hero's shape and divide in half to find the pixel width of the hero shape (the radius) and detect collision if the distance between your hero and another sprite is equal to or less than this radius.