Emitting An Unlimited Amount of Particles - cocos2d-iphone

I am trying to make my particle system in Cocos2d-iPhone to have an unlimited amount of particles. This is the code I have so far.
//Create the particles
CCParticleExplosion *explosion = [CCParticleExplosion particleWithTotalParticles: PARTICLECOUNTER];
explosion.anchorPoint = ccp(0.5f, 0.5f);
explosion.color = [CCColor colorWithRed:0.1f green:0.2f blue:0.5f alpha:1.0f];
explosion.life = 100.0f;
explosion.positionType = CCPositionTypeNormalized;
explosion.position = ccp(0.5f, 0.5f);
[self addChild:explosion z:0];
What should take the place of PARTICLECOUNTER?

Related

How to make a physical wall using cocos2d / chipmunk?

How to make a physical wall using cocos2d / chipmunk ? So that sprite just can not go throught another one sprite(wall)? Sorry,if it's already answered somewhere, I can't find any information for beginners.
This is from cocos2d/chipmunk template. Your sprite should be on chipmunk body, to get that change position of sprite to position of body in your update method.
CGSize s = [[CCDirector sharedDirector] winSize];
_space = cpSpaceNew();
cpSpaceSetGravity( _space, cpv(0, -100) );
//
// rogue shapes
// We have to free them manually
//
// bottom
_walls[0] = cpSegmentShapeNew( _space->staticBody, cpv(0,0), cpv(s.width,0), 0.0f);
// top
_walls[1] = cpSegmentShapeNew( _space->staticBody, cpv(0,s.height), cpv(s.width,s.height), 0.0f);
// left
_walls[2] = cpSegmentShapeNew( _space->staticBody, cpv(0,0), cpv(0,s.height), 0.0f);
// right
_walls[3] = cpSegmentShapeNew( _space->staticBody, cpv(s.width,0), cpv(s.width,s.height), 0.0f);
for( int i=0;i<4;i++) {
cpShapeSetElasticity( _walls[i], 1.0f );
cpShapeSetFriction( _walls[i], 1.0f );
cpSpaceAddStaticShape(_space, _walls[i] );
}

Opengl Camera * Projection * Coord = Nothing

I have the following code to initialize my two matrices in lwjgl:
GL20.glUseProgram(shaderProgramID);
Matrix4f camera = new Matrix4f();
Matrix4f.translate(new Vector3f(0, 0, 0), camera, camera);
FloatBuffer buffer = BufferUtils.createFloatBuffer(16);
camera.store(buffer);
buffer.flip();
GL20.glUniformMatrix2(GL20.glGetUniformLocation(shaderProgramID, "camera"), false, buffer);
GL20.glUseProgram(0);
float near = 0.1f, far = 100.0f, top = 300, bottom = -300, left = -400, right = 400;
Matrix4f projection = new Matrix4f();
projection.setIdentity();
projection.m00 = 2*near/(right - left);
projection.m11 = 2*near/(top - bottom);
projection.m22 = -(far +near)/(far - near);
projection.m23 = -1;
projection.m32 = -2*far*near/(far - near);
projection.m20 = (right+left)/(right -left);
projection.m21 = (top + bottom)/(top-bottom);
projection.m33 = 0;
FloatBuffer buffer2 = BufferUtils.createFloatBuffer(16);
projection.store(buffer2);
buffer.flip();
GL20.glUniformMatrix2(GL20.glGetUniformLocation(shaderProgramID, "projection"), false, buffer);
GL20.glUseProgram(0);
The vertex shader does the following:
gl_Position = projection * camera * vec4(vert, 1);
I dont know what could be wrong with it... anyone an idea?
I know this is late but for completenesse's sake:
The code above leaves no program bound when glUseProgram(0): is called, thus the later call of glUniform* creates a GL_INVALID_OPERATION as there is no program oject present. This also means that projection is never actually set for your program, so upon rendering it is an empty matrix which multiplied with a matrix and a vector still results in the null vector.
Also you might consider saving the uniform locations once after loading the shader and check if it actually is available.

Box2D debug test

I have a problem with Box2d. I created an object (circle) in the center of the screen that rotates to see if I set Box2d well, following the tutorial that I found on internet. The problem is that the circle is created, but I can't rotate despite I followed exactly the tutorial found on internet. This is the code:
file .h:
b2World * _world;
GLESDebugDraw * _debugDraw;
file .mm:
-(void)setupWorld {
b2Vec2 gravity = b2Vec2(0.0f, 0.0f);
bool doSleep = false;
_world = new b2World(gravity, doSleep);
}
-(void)setupDebugDraw {
_debugDraw = new GLESDebugDraw(PTM_RATIO*[[CCDirector sharedDirector] contentScaleFactor]);
_world->SetDebugDraw(_debugDraw);
_debugDraw->SetFlags(b2DebugDraw::e_shapeBit |b2DebugDraw::e_jointBit);
}
-(void)testBox2D {
CGSize winSize = [CCDirector sharedDirector].winSize;
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position = b2Vec2(winSize.width/2/PTM_RATIO,winSize.height/2/PTM_RATIO);
b2Body *body = _world->CreateBody(&bodyDef);
b2CircleShape circleShape;
circleShape.m_radius = 25.0/PTM_RATIO;
b2FixtureDef fixtureDef;
fixtureDef.shape = &circleShape;
fixtureDef.density = 1.0;
body->CreateFixture(&fixtureDef);
body->ApplyAngularImpulse(0.01);
}
-(void)updateBox2D:(ccTime)dt {
_world->Step(dt, 1, 1);
[self updateBox2D:dt];
}
-(void) draw {
glDisable(GL_TEXTURE_2D); glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY);
_world->DrawDebugData();
glEnable(GL_TEXTURE_2D); glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}

in init:
[self setupWorld];
[self setupDebugDraw];
[self testBox2D];
Two problems:
Are you recursively calling updateBox2D? This looks like an infinite loop to me, and I'm surprised that it's not crashing your application.
Instead, you should only call your world's step once (or a few times depending on how you've setup your time step) during your CCScene tick method.
Your next problem is that you're applying a small impulse to your body, but it is only a one-time impulse... Angular impulses are still affected by damping, so the body will not rotate indefinitely by default. To keep your body rotating, you need to set the angular damping to zero:
bodyDef.angularDamping = 0.0f;

vertices smaller than sprite when rendered on iPhone screen

I am using vertex helper to get the edges of my sprites. I am using Cocos2d + Box2d on the iPhone. The problem I am getting is that the debug draw on the device I am testing on comes up but the vertices rendered or shown is smaller than my sprites even after tracing the edges of the sprites carefully and using the same sprite.
OK here is a screen shot of the problem!
The green triangle is supposed to fit with the edges of the sprite. Also this is the code I used:
This is the GLESDebugDraw code
- (void)setupDebugDraw {
debugDraw = new GLESDebugDraw(PTM_RATIO* [[CCDirector sharedDirector]
contentScaleFactor]);
world->SetDebugDraw(debugDraw);
debugDraw->SetFlags(b2DebugDraw::e_shapeBit);
}
#import "Box2DSprite.h"
#interface Stalag :Box2DSprite {
b2World *world;
}
- (id)initWithWorld:(b2World *)world atLocation:(CGPoint)location;
#end
#import "Stalag.h"
#implementation Stalag
- (void)createBodyAtLocation:(CGPoint)location {
b2BodyDef bodyDef;
bodyDef.type = b2_staticBody;
bodyDef.position = b2Vec2(location.x/PTM_RATIO,
location.y/PTM_RATIO);
self.body = world->CreateBody(&bodyDef);
body->SetUserData(self);
b2PolygonShape shape;
int num = 8;
b2Vec2 verts[] = {
b2Vec2(36.8f / 100.0, -79.2f / 100.0),
b2Vec2(10.3f / 100.0, 33.2f / 100.0),
b2Vec2(1.8f / 100.0, 80.6f / 100.0),
b2Vec2(-4.6f / 100.0, 84.5f / 100.0),
b2Vec2(-8.5f / 100.0, 80.3f / 100.0),
b2Vec2(-22.6f / 100.0, 19.4f / 100.0),
b2Vec2(-31.8f / 100.0, -45.6f / 100.0),
b2Vec2(-37.5f / 100.0, -75.7f / 100.0)
};
shape.Set(verts, num);
b2FixtureDef fixtureDef;
fixtureDef.shape = &shape;
fixtureDef.density = 1000.0;
body->CreateFixture(&fixtureDef);
}
- (id)initWithWorld:(b2World *)theWorld atLocation:(CGPoint)location {
if ((self = [super init])) {
world = theWorld;
[self setDisplayFrame:[[CCSpriteFrameCache
sharedSpriteFrameCache] spriteFrameByName:#"hill.png"]];
gameObjectType = kStalagupType;
[self createBodyAtLocation:location];
}
return self;
}
#end
Please can anyone tell me what do I do and what I am doing wrong?
Thanks
When specifying vertices coordinates you are dividing it by a "magic number" 100. Actually it is your PixelToMetersRatio (PTM_RATIO in cocos examples). Also you are using box2d debug draw. And when creating debug draw class your must pass PTM_RATIO in it's constructor. I think you are passing another number there (not 100). Does it solve the problem ?

Cocos2d draw a polygon using CCSprite

Can you help. Want to draw a polygon (beams at different angles) and apply box 2d body to it. Can you please let me know how to create a CCSprite with a polygon shape
Any examples would help
Cheers
Create Polygon body.
-(void) createDynamicPoly {
b2BodyDef bodyDefPoly;
bodyDefPoly.type = b2_dynamicBody;
bodyDefPoly.position.Set(3.0f, 10.0f);
b2Body *polyBody = world->CreateBody(&bodyDefPoly);
int count = 8;
b2Vec2 vertices[8];
vertices[0].Set(0.0f / PTM_RATIO,0.0f / PTM_RATIO);
vertices[1].Set(48.0f/PTM_RATIO,0.0f/PTM_RATIO);
vertices[2].Set(48.0f/PTM_RATIO,30.0f/PTM_RATIO);
vertices[3].Set(42.0f/PTM_RATIO,30.0f/PTM_RATIO);
vertices[4].Set(30.0f/PTM_RATIO,18.0f/PTM_RATIO);
vertices[5].Set(18.0f/PTM_RATIO,12.0f/PTM_RATIO);
vertices[6].Set(6.0f/PTM_RATIO,18.0f/PTM_RATIO);
vertices[7].Set(0.0f/PTM_RATIO,30.0f/PTM_RATIO);
b2PolygonShape polygon;
polygon.Set(vertices, count);
b2FixtureDef fixtureDefPoly;
fixtureDefPoly.shape = &polygon;
fixtureDefPoly.density = 1.0f;
fixtureDefPoly.friction = 0.3f;
polyBody->CreateFixture(&fixtureDefPoly);
}
Create your sprite
Attach your sprite to the Polygon body via Fixture and UserData
fixtureDefPoly.SetUserData() = spriteObject;
b2Fixture *fixture;
fixture = circleBody->CreateFixture(&fixtureDefPoly);
fixture->SetUserData(#"spriteObject");
Then Iterate the sprite to the body in your update method.
The easiest way is to open an image editor (such as paint for example or photoshop) and create the image you want. The use it in your program.
Also there is a helloWorld scene when creating an xcode application using cocos2d box2d template. It creates a set of squares with a texture.
CGPoint startPt = edge.start ;
CGPoint endpt = edge.end ;
//length of the stick body
float len = abs(ccpDistance(startPt, endpt))/PTM_RATIO;
//to calculate the angle and position of the body.
float dx = endpt.x-startPt.x;
float dy = endpt.y-startPt.y;
//position of the body
float xPos = startPt.x+dx/2.0f;
float yPos = startPt.y+dy/2.0f;
//width of the body.
float width = 1.0f/PTM_RATIO;
b2BodyDef bodyDef;
bodyDef.position.Set(xPos/PTM_RATIO, yPos/PTM_RATIO);
bodyDef.angle = atan(dy/dx);
NSLog([NSString stringWithFormat:#"Setting angle %f",bodyDef.angle]);
CCSprite *sp = [CCSprite spriteWithFile:#"material-wood.png" rect:CGRectMake(0, 0, 12, 12)];
//TODO: fix shape
[self addChild:sp z:1 ];
bodyDef.userData = sp;
bodyDef.type = b2_dynamicBody;
b2Body* body = world->CreateBody(&bodyDef);
b2PolygonShape shape;
b2Vec2 rectangle1_vertices[4];
rectangle1_vertices[0].Set(-len/2, -width/2);
rectangle1_vertices[1].Set(len/2, -width/2);
rectangle1_vertices[2].Set(len/2, width/2);
rectangle1_vertices[3].Set(-len/2, width/2);
shape.Set(rectangle1_vertices, 4);
b2FixtureDef fd;
fd.shape = &shape;
fd.density = 1.0f;
fd.friction = 0.300000f;
fd.restitution = 0.600000f;
body->CreateFixture(&fd);