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.
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 have a rotated Rectangle inside a closed tile map of rectangles.
What would be the best way to check if the player (which is the rotated rectangle) is intersecting with one of the rectangles of the tile map?
Picture from inside the game to better show what the map looks like:
If it matters, the Player's type is sf::Shape and the map's data is inside an int array.
SFML does not provide collision detection, it only has method to check if two axis-aligned rectangles intersect. If you need something more complex, you will have to implement if yourself.
If you don't need precision detection, you can test Sprite.getGlobalBounds().intersects(...) with the rectangle of the map.
If you want ideal collision detection, you have more then one option:
Pixel perfect Collision. First check if bounding box intersect the map tile and them check all non-transparent pixels for collision. Not very fast but easy to implement and may be suitable for your case.
Mathematical methods, there are more that one, but take a look at Separating Axis Theorem. If your are only limited to rectangles (or/and circles and convex polygons), it will work best.
For anyone still having this issue:
You should look into the getTransform() and getInverseTransform() functions of sf::Transformable (https://www.sfmldev.org/documentation/2.5.1/classsf_1_1Transformable.php). Getting the inverse transforms of the player and a specific wall allows you to use a simple AABB collision algorithm (like SFML already implemented it in getGlobalBounds().intersects(...)). You basicly look at the local coordinate system of the player and how the wall is positioned to it, all translations, rotations and scaling ignored.
I have a canvas in Qt (QGraphicsScene in QGraphicsView) on which user can add shapes: circle, square, rectangle, ellipse and triangle and change the size of either (as single QGraphicsObject subclass). Between these shapes user can create lines and the direction of the line is indicated by drawing an arrow at the intersection point of the line and a shape in the similar manner to Qt's example ElasticNodes (the connection is also QGraphicsObject suclass).
Now in order to support bi-directional connections and multiple connections of the same shapes I change any subsequent connection line into quadratic curve with progressively bigger arc to allow their selection and visualisation.
The implementation of the shapes is so that their centre is NOT top left, but instead the centre of the shape.
The connection between them is a line between these two centres pushed back in Z axis so it hides behind the shapes.
The arrow is position at the edge on a shape is determined depending on a shape: radius for circle and trigonometrics for other shapes.
Now when using quadratic curve I need to reposition the arrow to the intersection point of the curve and a shape. With that point I can then use the same procedure to render the arrow because I can get the angle at certain point from QPainterPath.
However he biggest challenge is to detect this collision point. The only option I can think of is to use QPainterPath::intersected but for that to work it requires fill areas (i.e. making the curve with a width of at least 2) and I would still need to somehow extract the correct point from the result - not sure how yet.
I would appreciate any ideas on how I could go about this.
Im using Level Helper and SpriteHelper to create my sprites, images, levels and more importantly animations and physics.
Note, by physics i mean the debugdraw you can see on the simulator that is used for collision detection.
I created a swimmer and added physics to this. Done the code so the physics follows the swimmer around the pool as they move. I have now come to animate the swimmer, make the legs kick etc. Now when i load my game and only the first sprite of the animation is the outline for the physics. So i can see the legs kicking on the swimmer but the debugdraw mesh of the physics doesn't animate as well. Now this is not really a problem until for example my swimmer loses their legs (weird game i know). I change the animation to now a swimmer with no legs but again the physics mesh still shows the legs. So any collisions with stuff still happens where the legs were but they shouldnt. This make sense?
Is there a way to update the physics on the new animation or do i need to remove my whole swimmer and draw a new one?
Any help at all would be great. Thanks
This makes sense, since your sprite uses the same box2d mesh in both states. If you want to have a different collision behavior after altering your Sprite, you should assign another (smaller) mesh body.
Note that even in cocos2d side, your sprite still has the same container box with the new animation.
In order to keep using the SpriteHelper functionality you might want to create 2 different sprite-body sets: one with the full body, then after the "accident" replace it with the legless sprite.
Now, gameplay-wise, my opinion is that there shouldn't be any collision for the legs anyway. since they are moving, players will not find it weird to have them not colliding. You could use a mesh body with no legs and use it for both sprites. Except if you want to have different collisions as a gameplay feature (Like giving the player the choice to cut his legs in order to fit in a smaller cave etc)
I have a sprite with a triangle shape and I want to know when this triangle intersects with another object (a CCSPrite). I have been using CGRectIntersectsRect but it is not accurate because it take the bounding box of the the two sprites and not the actual shape.
--Edit
I think one way is to define several points around the actual triangle and check intersection between those points with another sprite. I am just wondering if there is an easier way to do this.
You can use box2d to detect collisions accurately. It can be useful if you have many different complicated shapes. Or you can just check intersections of shape's edges.
If there is many objects to detect collisions with, I offer to use box2d. It has good internal optimizations to be able to work with large amount of objects. In this case you will just have to create physical body equal to the your sprite's shape before adding object to your game layer.