AndEngine collision detection - andengine

I want to detect sprite collision on a hill but my sprite detect collision before hill as it detects rectangular shape. I want to detect collision on hill edges, I draw a triangle by using mesh but collideswith() method not working

You have two options:
1) You can use the Box2D extension and let Box2D handle the colisons
2) You can use the AndEngineCollisionExtension
-You'll have to replace your Sprite with PixelPerfectSprite

Related

Using a mesh collider in Unity with Bullet Physics

When using this ramp as a collider with Convex Hull the ball rolls on the edge like this:
Ramp and ball
Bullet physics settings
Blender
Thanks!
/T
Because that is not a convex shape.
Thanks! The creator of the plugin gave this solution to the shape:
approximate the shape with multiple simple colliders (boxes)
use BVHTriangleMeshShape if it is static
use the GImpactTriangleMeshShape if it is dynamic

How to add reaction force to a collision in Unity?

So when using rigidbody physics on a 2d sphere and a spinning animated 2d rectangle it only pushes the sphere aside when it collides. What I'm wanting is the sphere to fly off when collided (don't ask if it's if it's a problem with it colliding or not).
I've tried the physics materials and adjusting the rigidbody and such but nothing works. There's not a single thing online about it so attempting to code physics would be very difficult.
If I understand correctly.
You could apply AddForce or AddForceAtPosition function with ForceMode.Impulse argument to sphere when Collider2D.OnCollisionEnter2D(Collision2D) event trigger.
Or Just modify sphere speed when Collider2D.OnCollisionEnter2D(Collision2D) event trigger.
Note that Collision2D have contacts and relativeVelocity which may help you calc the force vector.

SpriteKit tilemaps collision with curved or sloped floor tiles

I am planning a platforming game for iOS using SpriteKit and Swift. I did some research on how to handle collisions of the player sprite and stumbled upon this article.
http://www.learn-cocos2d.com/2013/08/physics-engine-platformer-terrible-idea/
That article advices you not to use the build-in physics engine of SpriteKit but to implement things like moving, jumping and collision handling on your own. The platform tutorial on Ray Wenderlichs site suggests a similar approach.
So far so good but let's talk about floor tiles on which the player can stand upon. A custom physics implementation would be easy as long as the tiles are rectangular and have a flat surface (like in the tutorial from Ray Wenderlich), since you would use CGRectIntersectsRect to detect a collision. But how would you check the player collision on curved or sloped tiles? From what I have read, CGRectIntersectsRect does not account for tranparent pixel inside a sprites rect.
http://i.stack.imgur.com/lRP2Q.png
Look at the above tile for example. The white area (upper left) would be transparent. Now, if the player sprite would drop on this tile, the collision would be detected at the upper border of the tiles rectangle, although there are no ground pixels there (blue area, lower right). So ultimately the player would hover in mid-air above this tile. I could push the player sprite down a few pixels but that is a bit hacky and gets harder if the curved floor tiles have different slope angles.
So the question is, how can I handle these types of collision with SpriteKit alone (no extra frameworks like Cocos2D, Kobold Kit, ...)? Or is this approach entirely wrong and collisions in platformer with SpriteKit should be done fundamentally different?
Any help is very much appreciated!
I disagree with not using physics to handle collisions and contacts. You are really just trying to reinvent the wheel here by not using physics and implementing your own custom code.
If you are using the Tiled app then assigning a physics body is a simple task. Use the Objects in Tiled to assign various body types. In your code you can then go about creating a specific body for each object type.
For example:
In the above image I have created a 45 degree right side sloped floor. The object I added is called floor45R.
The next step is to parse your map objects. In case of the 45floorR, you create a physics body like this:
NSArray *arrayObjects = [group objectsNamed:#"floor45R"];
for (NSDictionary *dicObj in arrayObjects) {
CGFloat x = [dicObj[#"x"] floatValue];
CGFloat y = [dicObj[#"y"] floatValue];
CGFloat w = [dicObj[#"width"] floatValue];
CGFloat h = [dicObj[#"height"] floatValue];
SKSpriteNode *myNode = [SKSpriteNode spriteNodeWithColor:[SKColor clearColor] size:CGSizeMake(w, h)];
myNode.position = CGPointMake(x, y);
myNode.zPosition = 100;
CGMutablePathRef trianglePath = CGPathCreateMutable();
CGPathMoveToPoint(trianglePath, nil, -myNode.size.width/2, myNode.size.height/2);
CGPathAddLineToPoint(trianglePath, nil, myNode.size.width/2, -myNode.size.height/2);
CGPathAddLineToPoint(trianglePath, nil, -myNode.size.width/2, -myNode.size.height/2);
CGPathAddLineToPoint(trianglePath, nil, -myNode.size.width/2, myNode.size.height/2);
myNode.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:trianglePath];
myNode.physicsBody.dynamic = NO;
myNode.physicsBody.restitution = 0;
myNode.physicsBody.friction = 0.0;
myNode.physicsBody.categoryBitMask = CategoryFloor45;
myNode.physicsBody.collisionBitMask = 0x00000000;
CGPathRelease(trianglePath);
[worldNode addChild:myNode];
}
This works for any degree floor slope. Remember to set your player's and any other node's collision bit mask to collide with the floor.
In order for your player move smoothly over sloped floor, I recommend building the player's physics body in 2 pieces. A circle at the bottom and a rectangle at the top. The circle will prevent getting stuck in any potential cracks caused by 2 joining physics bodies.
SKPhysicsBody *firstBody = [SKPhysicsBody bodyWithCircleOfRadius:10 center:CGPointMake(0, 0)];
SKPhysicsBody *secondBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(5, 50) center:CGPointMake(0, 10)];
self.physicsBody = [SKPhysicsBody bodyWithBodies:#[firstBody, secondBody]];
You will have to adjust the position and center coordinates to match your sprite's image.
You can make use of a level editor, Tiled to handle curved and sloped floor tiles along with JSTileMap which processes the TMX map.
It will be able to able to handle contact with curved and sloped floor tiles but as for adjusting the angle of objects standing on top of those tiles, you would have to utilize some math functions, a good example is found here
You can still make use of SpriteKit's collision detection which will simplify things for you but create your own jumping or movement engine.

Getting the correct movement vector to fire a bullet from a rotated sprite

Hi,
I'm using a SKNode to represent a ship sprite. This sprite can be rotated and moved by touch events. This sprite should fire a bullet (another SKNode object) every second.
I plan to use SKAction to move the bullet.
The zRotation method of SKNode objects returns the rotation around the z-Axis. But how can I transform this into the right vector?
Thanks for your help
Stefan

stick a sprite to another when it collide with it

so I have a sprite that is created every second and that is moving at a random position (sprite1) and another sprite that has a fixed position (sprite2). I would like that when sprite1 collide with sprite2, sprite1 is like sticked to it (it stops moving and is sticked to it) . How can I do this please ? sorry for my english I'm french :/
p.s : sprite2 is rotating with accelerometer, so if sprite1 collide with it I would like that it rotate too :)
I think, you can try to use box2d to do this. It will help to detect collisions and to manage rotations, movement, etc.
I think, you can do it simply in Cocos2d.
1) First set the rect for sprite1 and sprite2 using CGRectMake(x,y,width,height)
2) As you told sprite1 is moving at random position and sprite2 is fixed to particular position, you can check them collide by using CGRectIntersectsRect([sprite1 bounds],[sprite2 bounds]).
3) if it intersects, set sprite1.position = sprite2.position
Note: you said sprite1 is rotating, rect can be fit only to the regular bodies. if you want exact collision or physical properties for sprite better you can go for box2d.
If you don't want to use Box2d (which can handle circle collisions), you can try something like this:
1.) Detect collision, is the distance between the two circles center point (x,y), less than the sum of the two circles radius.
2.) Make the Sprite1 stick to Sprite2, Stop the movement of Sprite1, and save the relative delta (x,y) to Sprite2, then whenever Sprite2 moves or rotates apply the same delta movement and rotation to Sprite1.