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:
Related
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];
Consider this: you create a new project on Cocos2D 2.0. You have the traditional Helloworld layer. You add a layer to it with the following structure:
Helloworld (cclayer)
│
┕━ baseLayer (cclayer)
│
┕━ myReducedNode [CCSprite node]
│
┕━ myFullSprite (ccsprite)
│
┕━ smallSprite (ccsprite)
myReducedNode is a node inside baseLayer, created using [CCSprite node] and has a scale applied to it, so, when I apply that scale I reduce myFullSprite and all smallSprites at the same time.
myFullSprite is a 1024x768 points sprite inside myReducedNode.
smallSprites are 230x348 points sprite inside myFullSprite.
Consider this craziness:
first I apply a scale of 1 to myReducedNode. When I drag smallSprite and check its coordinates, everything is fine. If I position smallSprite on the top left corner of myFullSprite, I read the center coordinate of smallSprite as being (115,594) which is the correct value.
I apply a 0.8 scale to myReducedNode. Dragging smallSprite to the same top left corner of myFullSprite, cocos is now reporting the center of smallSprite to be (17,641) ?????????!!!!!!
I am talking about local coordinates, I mean, the position smallSprite is inside myFullSprite.
What is causing this? There's no apparent logic on this number... This number has no relation with the scale applied to the top node.
What am I missing here? I am banging my head on the wall for days, trying to figure this puzzle!!! thanks.
More information. I hope this helps figure out why the coordinates have those values...
baseLayer position is (612, 389) on Helloworld.
myReducedNode position is (0,0) on baseLayer.
myFullSprite position is (0,0) on myReducedNode
I think you should take a look at convertToWorldSpace:, since you are scaling and nesting things, transformations most likely apply to those coordinates.
Here you have a question that might be useful and this post on cocos2d too
Try this:
CGPoint smallSpriteLocalPosition;
smallSpriteLocalPosition =
[smallSprite.parent convertToNodeSpace:smallSprite.position];
Then print out those coordinates and see if they register properly. That should give you the node (local) coordinates of the smallSprite relative to its parent, the fullSprite. You should also be able to convertToWorldSpace for coordinates within the window bounds.
This is what has worked for me in the past when working with child sprites; it can be a bit tricky. Make sure you use the proper variables in the convert call, otherwise you won't get the right data. Let me know if that works as I haven't tried it with layers that are three deep.
after a few changes in code and a several days of research and tries, I conclude this is a bug of Cocos2D or a lack of consistency between how Layers, Sprites and Nodes work (as suggested by LearnCocos2d) , as there's no way to explain the obtained values. I will try to fill a bug report on that.
I have a maze game that using cocos2d
I have one main sprite that can save "friend" sprite
Once "friend" sprite collide with main sprite, the "friend" sprite will follow main sprite everywhere.
Now I dont know how to make "friend" sprite follow main sprite with static distance and smooth movement.
I mean if main sprite going up, "friend" will be at behind the main sprite.
If main sprite going left, "friend" sprite will be at right of main sprite.
Please help me and share me some code...
You can implement the following behaviour by using the position of your main sprite as the target for the friend sprite. This would involve implementing separation (maintaining a min distance), cohesion (maintaining max distance) and easing (to make the movement smooth).
The exact algorithms (and some more) are detailed in a wonderful behavior animation paper by Craig Reynolds. There are also videos of the individual features and example source code (in C++).
The algorithm you need (it is a combination of multiple simpler ones) is Leader following
EDIT : I have found two straightforward implementations of the algorithms mentioned in the paper with viewable source code here and here. You will need to slightly recombine them from flocking (which is mostly following a centroid) to following a single leader. The language is Processing, resembling java-like pseudcode, so I hope the comprehension should be no problem. The C++ sourcecode I mentioned earlier is also downloadable but does not explicitly feature leader following.
I am not aware of any cocos2d implementations out there.
I have a simple solution kind of working fine. Follow the cocos2d document getting started lesson 2, Your first game. After implement the touch event. Use the following code to set seeker1 to follow cocosGuy:
- (void) nextFrame:(ccTime)dt {
float dx = cocosGuy.position.x - seeker1.position.x;
float dy = cocosGuy.position.y - seeker1.position.y;
float d = sqrt(dx*dx + dy*dy);
float v = 100;
if (d > 1) {
seeker1.position = ccp( seeker1.position.x + dx/d * v *dt,
seeker1.position.y + dy/d * v *dt);
} else {
seeker1.position = ccp(cocosGuy.position.x, cocosGuy.position.y);
}
}
The idea is at every step, the follower just need to move towards the leader at a certain speed. The direction towards the leader can be calculated by shown in the code.
I have a tire in my screen and I would like to have a rope connected to the top of the tire and to the top of the screen. I have incorporated physics into my game with chipmunk+spaceManager so I need to some how have this rope react to the physics also. I just need it to move back and forth with the tire when it gets hit. I have used a cpConstraintNode to get a line drawn to use it as a rope up to this point but everything I have seen and looked into, there is no way to attach a CCSprite to a constraint. So my question is How would I go about creating this rope and have it react the same as the tire when it is moving? Here is my code that I have done with the constraint: I am using cocos2d and chipmunk + spaceManger
//The "rope"
cpVect a1 = cpv(0,30); //Local coordinates of tire
cpVect a2 = cpv(70,320); //World coordinates (staticBody is at (0,0))
//calculate the length of the rope
float max = cpvdist(cpBodyLocal2World(upper->body, a1), a2);
cpConstraint *rope = [game.spaceManager addSlideToBody:upper->body fromBody:game.spaceManager.staticBody toBodyAnchor:a1 fromBodyAnchor:a2 minLength:1 maxLength:max];
cpConstraintNode *ropeNode = [cpConstraintNode nodeWithConstraint:rope];
ropeNode.color = ccBLUE;
This post presents a great way to draw ropes in Cocos2D using Verlet Integration.
The only drawback is that the example uses Box2D. But the code can be ported to chipmunk.
If you're wanting to use the VRope project on github to integrate VRope and chipmunk, I've created a new branch that does just that. You'll find it at:
VRope Chipmunk Branch
An example of using it is:
Creation
pinPointJoint =
cpSlideJointNew(body,
body2,
body.anchorPoint,
body2.anchorPoint,
minimumLength,
maximumLength);
cpSpaceAddConstraint(space, pinPointJoint);
rope = [[VRope alloc] init:pinPointJoint batchNode:ropeBatchNode];
Updating
[rope update:delta];
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