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 :)
Related
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.
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.
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
I am kinda new to all this but I am trying to make myself a simple 2D game in c++.
I have decided to do a kind of a maze type game and what I am doing for this is drawing out the maze as a texture and then having another texture as my character move around inside this maze.
However I am hugely struggling with the collision detection so that my character doesn't just walk through the walls. I have been told that I can use glReadPixels to find the colour of the background but whenever I try this it just ignores the colour and still continues on through the walls.
Can anybody please help me with this and tell me how I can do it as I cannot find anything anywhere which can help.
Thanks in advance.
Depending on the maze type, if you only have vertical and horizontal walls of unit length, you could reprezent the maze and current position in a 2D array/matrix and decide whether the new position is OK to move into based on the content of the new position in the maze matrix.
You will have to do some translation to/from matrix coordinates and screen coordinates
Advantages:
you don't need to read from the screen
maze can be larger than what fits on the screen -- draw the relevant portion only
Disadvantages:
you can only have "bolck" type terrain (e.g. vertical/horizontal walls)
if you want to add movign enemies, the collision detection can be too coarse (you cannot avoid the monster "just by a hair/pixel")
I have been able to find a lot of information on actual logic development for games. I would really like to make a card game, but I just dont understand how, based on the mouse position, an object can be selected (or atleast the proper way) First I thought of bounding box checking but not all my bitmaps are rectangles. Then I thought f making a hidden buffer wih each object having a different color, but it seems ridiculous to have to do it this way. I'm wondering how it is really done. For example, how does Adobe Flash know the object under the mouse?
Thanks
Your question is how to tell if the mouse is above a non-rectangular bitmap. I am assuming all your bitmaps are really rectangular, but they have transparent regions. You must already somehow be able to tell which part of your (rectangular) bitmap is transparent, depending on the scheme you use (e.g. if you designate a color as transparent or if you use a bit mask). You will also know the z-order (layering) of bitmaps on your canvas. Then when you detect a click at position (x,y), you need to find the list of rectangular bitmaps that span over that pixel. Sort them by z-order and for each one check whether the pixel is transparent or not. If yes, move on to the next bitmap. If no, then this is the selected bitmap.
Or you may use geometric solution. You should store / manage the geometry of the card / item. For example a list of shapes like circles, rectangles.
Maybe triangles or ellipses if you have lots of time. Telling that a triangle has a point or not is a mathematical question and can be numerically unstable if the triangle is very thin (algorithm has a dividing).. Fix: How to determine if a point is in a 2D triangle?
I voted for abc.