Draw physics body with two circle shape cocos2d v3? - cocos2d-iphone

I want to draw Red onze Physics Body in cocos2d v3. A shape with 2 circle and perforation. Is it impossible and how to do that by code ? Thanks
http://greenhornbold.com/wp-content/uploads/2013/01/RED-CIRCLE-2.jpg

Is this what you're asking about?
projectile.physicsBody = [CCPhysicsBody bodyWithCircleOfRadius:projectile.contentSize.width/2.0f andCenter:projectile.anchorPointInPoints];

Related

How to draw half an ellipse in Raphael JS?

I've been trying to understand the following code, but I don't seem to gain control over what the parameters are doing. I have to draw half an ellipse at a certain location. Could anyone explain to me what the parameters of the path mean in order to master this shape. Thanks.
var curve4 = paper.path("M150,150 A100,70 0 1,1 150,10")
.attr({"stroke-width": 2, stroke: "red"});
Ok I solved the syntax:
paper.path("M x_start,y_start A width,height rotation 1,direction x_end,y_end")
About that 1 between rotation and direction I'm not sure. It's probably also involved in the direction bit. I created a fiddle to play around with the ellipse parameters: https://jsfiddle.net/ansjovis/vgw3vdc8/4/

cocos2d two physics bodies move together (chipmunk)

As the picture shows.
How can I make part A stick intensely with part B ? to make a tank body ?
contex shape is OK but cancerve is not . so I want to make two parts stick together and move....
Thanks in advance. PS I am using cocos2d v3 , it is chipmunk encapsulated with cocos2d, no Box2D here..
=============
Now I have resolved the issue . use Shapes list can do . But new questions comes : how can I put the gun on tank ? pivot body can rotate some angle but not 360.
there are only 4 connects in CCPhysicsJoint:
+(CCPhysicsJoint *)connectedPivotJointWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB anchorA:(CGPoint)anchorA;
+(CCPhysicsJoint *)connectedDistanceJointWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB
anchorA:(CGPoint)anchorA anchorB:(CGPoint)anchorB;
+(CCPhysicsJoint *)connectedDistanceJointWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB
anchorA:(CGPoint)anchorA anchorB:(CGPoint)anchorB
minDistance:(CGFloat)min maxDistance:(CGFloat)max;
+(CCPhysicsJoint *)connectedSpringJointWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB
anchorA:(CGPoint)anchorA anchorB:(CGPoint)anchorB
restLength:(CGFloat)restLength stiffness:(CGFloat)stiffness damping:(CGFloat)damping;
no one fit.
And my question is , can I rotate a angle on a shape in shape-arranged body ?
You can create a body with multiple shapes attached.
http://www.cocos2d-iphone.org/docs/api/Classes/CCPhysicsBody.html#//api/name/bodyWithShapes:

Chipmunk Physics Positioning

I've looked around quite a bit and haven't been able to find a concise answer to this question. For my Cocos2D game I have integrated the Chipmunk physics engine. On initialization, I setup the boundaries of the 'playing field' by establishing a series of bodies and static shapes as follows:
- (void)initPlayingField {
// Physics parameters
CGSize fieldSize = _model.fieldSize;
CGFloat radius = 2.0;
CGFloat elasticity = 0.3;
CGFloat friction = 1.0;
// Bottom
CGPoint lowerLeft = ccp(0, 0);
CGPoint lowerRight = ccp(fieldSize.width, 0);
[self addStaticBodyToSpace:lowerLeft finish:lowerRight radius:radius elasticity:elasticity friction:friction];
// Left
CGPoint topLeft = ccp(0, fieldSize.height);
[self addStaticBodyToSpace:lowerLeft finish:topLeft radius:radius elasticity:elasticity friction:friction];
// Right
CGPoint topRight = ccp(fieldSize.width, fieldSize.height);
[self addStaticBodyToSpace:lowerRight finish:topRight radius:radius elasticity:elasticity friction:friction];
// Top
[self addStaticBodyToSpace:topLeft finish:topRight radius:radius elasticity:elasticity friction:friction];
}
This setup works, except for one thing-- the positions of the boundaries appear to be off by a significant amount of pixels to the right. After initializing a player, and sending him towards the boundary walls, it does not bounce off the edges of the screen (as I would expect by setting up the bounding box at 0,0), but rather some number of pixels (maybe 30-50) inwards from the edges of the screen. What gives?
I initially thought the issue came from where I was rendering the player sprite, but after running it through the debugger, everything looks good. Are there special protocols to follow when using chipmunk's positions to render sprites? Do I need to multiply everything by some sort of PIXELS_PER_METER? Would it matter if they are in a sprite sheet? A sprite batch node? Here is how I set the sprite position in my view:
Player *player = [[RootModel sharedModel] player];
self.playerSprite.position = player.position;
self.playerSprite.rotation = player.rotation;
And here is how I set the position in my Model:
- (void)update:(CGFloat)dt {
self.position = self.body->p;
self.rotation = self.body->a;
}
(Yes, I know it's redundant to store the position because chipmunk does that inherently.)
Also, bonus question for for those who finished reading the post. How might you move a body at a constant velocity in whatever direction it is facing, only allowing it to turn left or right at any given time, but never slow down?
What's the shape of the physics body? Perhaps you simply forgot to consider making the shape the same size of the sprite. If you only consider the position, then the sprite will be able to move halfway outside the screen at any border because its position is at the center of the sprite.
To move a body at constant velocity, set its velocity while disabling friction (set to 0?) and allowing it to rebound off of collisions with its impact speed (in Box2D that would be restitution = 1, don't know about Chipmunk).
Have you tried debug drawing the collision shapes? Cocos2D 2.1 has my CCPhysicsDebugNode class in it that makes it pretty easy to add. That will let you know if your geometry is lined up with your graphics. How thick are your boundary shapes? Are you possibly adding the sprites to a parent node that might be offsetting them?
Chipmunk doesn't require you to tune it for any real units. You can just use pixels or whatever.
Lastly, #LearnCocos2D has the right idea about the constant velocity. Set friction to 0 and elasticity to 1.0. You will also probably want to check it's velocity every frame and accelerate it back towards it's normal velocity each frame. Colliding with non-static geometry will cause it to lose speed.

Cocos2d - Collision Detection of Rotated Sprite

I try to detect collision between two sprite.
if(CGRectIntersectsRect([SpriteA BoundingBox], [SpriteB boundingBox]))
But when i Rotate any sprite than collision detection is not perfect..
I know to use pixel perfect Collision but i have no idea about it.
Please anyone help me for how to detect collision, Give me any block of code if any.
You can use box2d to make it detect all collisions for you
In two ways you can do.
Use box2D body for your sprite. Example: CLICK HERE
Use CGMutablePathRef, and use CGPathContainsPoint() instead of CGRectIntersectsRect.
Example: CLICK HERE
You can also refere the Ray Wenderlich Tutorial for the detection of the Collision between any 2 Box2D bodies.
it's possible! try with CGPath.
I had the same problem. I've resolved with this tutorial: http://bobueland.com/cocos2d/2011/the-magic-of-cgpaths/
for rotate the path try this method, it rotated the path round the center of the boudingBox:
-(CGPathRef) rotateCGPath:(CGPathRef)path corner:(CGFloat)radians
{
CGRect bounds = CGPathGetBoundingBox(path);
CGPoint center = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
CGAffineTransform transf = CGAffineTransformIdentity;
transf = CGAffineTransformTranslate(transf, center.x, center.y);
transf = CGAffineTransformRotate(transf, -radians);
transf = CGAffineTransformTranslate(transf, -center.x, -center.y);
return CGPathCreateCopyByTransformingPath(path, &transf);
}
after this you detect the collision simple with:
if (CGPathContainsPoint(Collisionpath, NULL, collisionPoint, NO))
{ //is inside the path }
good luck!

How to move background in cocos 2d

Hi i want to develop game like 'Doodle jump'.But i have some problem with the following features-
1.How to move background scene/image.
2.How to detect collision between object.Is it needed a physics engine like box2d or i should just use manual collision.
3.what should be the size of the background image.
4.In fact i have no idea how does background move .So i need a explanation from someone.
Background Movement
A) You could create a TMX Tilemap and then make a very high Tiled-Map.
B) You could create one texture and then cycle the texture coords instead of really moving it.
Detect it manually. Best is detect it via "Point in Boundingbox" or "Rect in Rect".
For more detail visit my blog entry for collision detection with cocos2d : http://www.anima-entertainment.de/?p=262
Size of an Image
Keep in Mind that textures are always at power of 2 in the memory. If you want to create one Background-Image at retina highresolution (960x640 Pixel) in the memory will be a texture of 1024x1024. If possible use smaller Background-Images and stretch them. (like 512x512). But I really would recommend for big scrolling images the TMX Support.
CCTMXTiledMap * tmxNode = [CCTMXTiledMap tiledMapWithGMXFile:#"Level.tmx"];
// lets say you want to move it 50 pixels down in 1 second :
[tmxNode runAction:[CCMoveBy actionWithDuration:1.0 position:ccp(0,-50)];
To create a tilemap : http://www.mapeditor.org/
In the folder of cocos2d, you could get many demos of tilemap.
TileMapTest.h
TileMapTest.m
refer this tutorial this will helpful for you.
http://www.raywenderlich.com/2343/how-to-drag-and-drop-sprites-with-cocos2d
this is used screen movement with pan recognizer