Problem to detect diamond shape in box2d - iphone

I make a simple game.
It has many diamond image and one ball. when ball touch with diamond shape then collision occur. in my application when ball touch on the edge then collision working correctly but when ball touch on the corner then collision is not working.
Code is hear..
- (id)init {
if ((self=[super init])) {
CGSize winSize = [CCDirector sharedDirector].winSize;
self.isTouchEnabled = YES;
self.isAccelerometerEnabled=YES;
// Create a world
b2Vec2 gravity = b2Vec2(0.0f, 0.0f);
bool doSleep = true;
_world = new b2World(gravity, doSleep);
// Create edges around the entire screen
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0,0);
_groundBody = _world->CreateBody(&groundBodyDef);
b2PolygonShape groundBox;
b2FixtureDef groundBoxDef;
groundBoxDef.shape = &groundBox;
groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(winSize.width/PTM_RATIO, 0));
_bottomFixture = _groundBody->CreateFixture(&groundBoxDef);
groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(0, winSize.height/PTM_RATIO));
_groundBody->CreateFixture(&groundBoxDef);
groundBox.SetAsEdge(b2Vec2(0, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO));
_groundBody->CreateFixture(&groundBoxDef);
groundBox.SetAsEdge(b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, 0));
_groundBody->CreateFixture(&groundBoxDef);
// Create sprite and add it to the layer
CCSprite *ball = [CCSprite spriteWithFile:#"ball1.png" rect:CGRectMake(0, 0, 16,16)];
// ball.position = ccp(180, 400);
ball.tag = 1;
// Create ball body
b2BodyDef ballBodyDef;
ballBodyDef.type = b2_dynamicBody;
ballBodyDef.position.Set(180/PTM_RATIO, 450/PTM_RATIO);
ballBodyDef.userData = ball;
ballBody = _world->CreateBody(&ballBodyDef);
// Create circle shape
b2CircleShape circle;
circle.m_radius = 16/PTM_RATIO;
//circle.m_radius = 50/PTM_RATIO;
// Create shape definition and add to body
b2FixtureDef ballShapeDef;
ballShapeDef.shape = &circle;
ballShapeDef.density = 1.0f;
ballShapeDef.friction = 0.0f; // We don't want the ball to have friction!
ballShapeDef.restitution = 0.0f;
_ballFixture = ballBody->CreateFixture(&ballShapeDef);
// Give shape initial impulse...
b2Vec2 force = b2Vec2(0, 0);
ballBody->ApplyLinearImpulse(force, ballBodyDef.position);
for(int i = 0; i < 5; i++)
{
static int padding=25;
CCSprite *block = [CCSprite spriteWithFile:#"diamond2.png"];
int xOffset = padding+block.contentSize.width/5+((block.contentSize.width+padding)*i);
block.position = ccp(xOffset, 250);
block.tag = 2;
[self addChild:block];
// Create block body
b2BodyDef blockBodyDef;
// blockBodyDef.type = b2_dynamicBody;
blockBodyDef.position.Set(xOffset/PTM_RATIO, 400/PTM_RATIO);
blockBodyDef.userData = block;
b2Body *blockBody = _world->CreateBody(&blockBodyDef);
// Create block shape
b2PolygonShape blockShape;
blockShape.SetAsBox(block.contentSize.width/PTM_RATIO/8,
block.contentSize.height/PTM_RATIO/8
);
// Create shape definition and add to body
b2FixtureDef blockshapeDef;
blockshapeDef.shape = &blockShape;
blockshapeDef.density = 0.0;
blockshapeDef.friction = 10.0;
blockshapeDef.restitution = 0.1f;
blockBody->CreateFixture(&blockshapeDef);
}
[self addChild:ball];
// Create contact listener
_contactListener = new MyContactListener();
_world->SetContactListener(_contactListener);
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:#"background-music-aac.caf"];
[self schedule:#selector(tick:)];
}
return self;
}
- (void)tick:(ccTime) dt {
// bool blockFound = false;
_world->Step(dt, 10, 10);
for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {
if (b->GetUserData() != NULL) {
CCSprite *sprite = (CCSprite *)b->GetUserData();
if (sprite.tag == 1) {
static int maxSpeed = 20;
b2Vec2 velocity = b->GetLinearVelocity();
float32 speed = velocity.Length();
// When the ball is greater than max speed, slow it down by
// applying linear damping. This is better for the simulation
// than raw adjustment of the velocity.
if (speed > maxSpeed) {
b->SetLinearDamping(0.2);
} else if (speed < maxSpeed) {
b->SetLinearDamping(0.0);
}
}
sprite.position = ccp(b->GetPosition().x * PTM_RATIO,
b->GetPosition().y * PTM_RATIO);
sprite.rotation = 1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
}
}
}
Plz help me for detect correct collsion detection near corner of the diamond shape....

I'm not sure i understood but it seems you are not making a diamond, you are making a square/box:
blockShape.SetAsBox(block.contentSize.width/PTM_RATIO/8,block.contentSize.height/PTM_RATIO/8);
maybe that's why you're saying collision is not working.
If your body is a square and your sprite is a diamond there are three options:
Or diamond has equal width and height and it's only necessary to rotate the square. Should be no problem with collision this way.
Or other shapes collide on the (invisible)corner of square when is not supposed to be there (when sprite/diamond fits inside square and square has no rotation or diamond has width != height needing a customized shape)
Or other shapes are not colliding with diamond sprite corner (when square is smaller and fits inside diamond sprite).
There are other methods to create customized shapes like:
void b2PolygonShape::Set(const b2Vec2* vertices, int32 count)
void b2PolygonShape::SetAsEdge(const b2Vec2& v1, const b2Vec2& v2)

Related

Collision detection (tutorial: How to make a platform game like Super Mario Brothers)

guys!
I am following this tutorial How to make a platform game like Super Mario Brothers to create a simple platform game. In the tutorial, the character sprite's size is a about a size of a single tile, and collision detection is calculated for 8 surrounding tiles. I modified the sprite's size to be equal about 4 tiles (2x2) and calculate collisions with 12 surrounding tiles. It works fine for the bottom and right sprite's edges, but the left and upper edges a little bit overlap with an obstacle before collision occurs, that is obviously wrong.
I feel that I have some mistakes in there, but as I am quite new in Spritekit I cannot spot them. I would really appreciate if someone could help me with it. Thanks in advance.
Here is the update function for the character:
- (void)update:(NSTimeInterval)delta
{
CGPoint gravity = CGPointMake(0.0, -450.0);
CGPoint gravityStep = CGPointMultiplyScalar(gravity, delta);
CGPoint forwardMove = CGPointMake(800.0, 0.0);
CGPoint forwardMoveStep = CGPointMultiplyScalar(forwardMove, delta);
CGPoint backwardMove = CGPointMake(-800.0, 0.0);
CGPoint backwardMoveStep = CGPointMultiplyScalar(backwardMove, delta);
if (self.forwardMarch)
{
self.velocity = CGPointAdd(self.velocity, forwardMoveStep);
}
if (self.backwardMarch)
{
self.velocity = CGPointAdd(self.velocity, backwardMoveStep);
}
self.velocity = CGPointAdd(self.velocity, gravityStep);
self.velocity = CGPointMake(self.velocity.x * 0.9, self.velocity.y);
// setup minimum and maximum limits for the motion speed
CGPoint minMovement = CGPointMake(0.0, -450);
CGPoint maxMovement = CGPointMake(0.0, 0.0);
// forward motion
if (self.velocity.x >= 0)
{
minMovement = CGPointMake(0.0, -450);
maxMovement = CGPointMake(120.0, 250.0);
}
// backward motion
if (self.velocity.x < 0)
{
minMovement = CGPointMake(-120.0, -450);
maxMovement = CGPointMake(0.0, 250.0);
}
self.velocity = CGPointMake(Clamp(self.velocity.x, minMovement.x, maxMovement.x), Clamp(self.velocity.y, minMovement.y, maxMovement.y));
CGPoint velocityStep = CGPointMultiplyScalar(self.velocity, delta);
self.newPosition = CGPointAdd(self.position, velocityStep);
}
This function find the bounding box of the sprite
- (CGRect)collisionBoundingBox
{
CGPoint diff = CGPointSubtract(self.newPosition, self.position);
return CGRectOffset(self.frame, diff.x, diff.y);
}
And the function where I handle collisions
- (void)handleObstacleCollisionsForPlayer:(Player *)player forLayer:(TMXLayer *)layer
{
NSInteger indices[12] = {13, 14, 1, 2, 4, 8, 7, 11, 0, 3, 12, 15};
player.onGround = NO;
for (NSUInteger i = 0; i < 12; i++)
{
NSInteger tileIndex = indices[i];
CGRect playerRect = [player collisionBoundingBox];
CGPoint playerCoord = [layer coordForPoint:player.newPosition];
NSInteger tileColumn = tileIndex % 4;
NSInteger tileRow = tileIndex / 4;
CGPoint tileCoord = CGPointMake(playerCoord.x + (tileColumn - 1), playerCoord.y + (tileRow - 1));
NSInteger gid = [self tileGIDAtTileCoord:tileCoord forLayer:layer];
if (gid)
{
CGRect tileRect = [self tileRectFromTileCoords:tileCoord];
if (CGRectIntersectsRect(playerRect, tileRect))
{
CGRect intersection = CGRectIntersection(playerRect, tileRect);
if (tileIndex == 13 || tileIndex == 14)
{
//tile is below
player.newPosition = CGPointMake(player.newPosition.x, player.newPosition.y + intersection.size.height);
player.velocity = CGPointMake(player.velocity.x, 0.0);
player.onGround = YES;
}
else if (tileIndex == 1 || tileIndex == 2)
{
//tile is directly above
player.newPosition = CGPointMake(player.newPosition.x, player.newPosition.y - intersection.size.height);
}
else if (tileIndex == 4 || tileIndex == 8)
{
//tile is left
player.newPosition = CGPointMake(player.newPosition.x + intersection.size.width, player.newPosition.y);
}
else if (tileIndex == 7 || tileIndex == 11)
{
//tile is right
player.newPosition = CGPointMake(player.newPosition.x - intersection.size.width, player.newPosition.y);
}
}
}
}
player.position = player.newPosition;
}
inside your GameViewController.m enable debug physics mode
that will draw a thick outline around every box and give u exact information about collision tiles may i think either your hero or tile sizes are differ than the tutorial you following
skView.showsPhysics= YES;

How do I use tile coordinates instead of the regular coordinates in cocos2d

I am still new to cocos2d and I have this problem that when I am using the ccp coordinates it stays at same position even when I move to the next screen. I came to the conclusion it must be that I am not using the tiles coordinates. So I need some help converting.
- (CGPoint)tileCoordForPosition:(CGPoint)position {
float x = floor(position.x / map.tileSize.width);
float levelHeightInPixels = map.mapSize.height * map.tileSize.height;
float y = floor((levelHeightInPixels - position.y) / map.tileSize.height);
return ccp(x, y);
}
-(CGRect)tileRectFromTileCoords:(CGPoint)tileCoords {
float levelHeightInPixels = map.mapSize.height * map.tileSize.height;
CGPoint origin = ccp(tileCoords.x * map.tileSize.width, levelHeightInPixels - ((tileCoords.y + 1) * map.tileSize.height));
return CGRectMake(origin.x, origin.y, map.tileSize.width, map.tileSize.height);
}
that is what i use to translate regular coordinates into tile map coordinates.
now I am trying to spawn an enemy at a certain position specifically (0,0)
-(void)addsaw{
CCSprite *saw = [CCSprite spriteWithFile:#"saw.png"];
// Determine where to spawn the target along the Y axis
CGSize winSize = [[CCDirector sharedDirector] winSize];
int minY =saw.contentSize.height/2;
int maxY = winSize.height - saw.contentSize.height/2;
int rangeY = maxY - minY;
int actualY = ( rangeY) + minY;
//THIS IS THE PROBLEM RIGHT HERE
saw.position = ccp(0,0);
[self addChild:saw];
// Create the actions
id actionMove = [CCMoveTo actionWithDuration:actualDuration
position:ccp(-saw.contentSize.width/2, actualY)];
id actionMoveDone = [CCCallFuncN actionWithTarget:self
selector:#selector(spriteMoveFinished2:)];
[saw runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];
saw.tag = 1;
[_saws addObject:saw];
}
How do I change the ccp of my addsaw so I can have it spawn at the tile (0,0) rather than the cocos2d regular (0,0)
you should add your sprite to your cctmxtiledmap instead of the layer!

Detecting collision of two bodies, each one has different shape

I want to detect collision between two bodies, one body has circle shape and another one has convex body (by using vertexes and number of vertexes), and I use contact listener class to detect collision between bodies. I tried detect collision between two circle shapes and it succeeded, but when I tried to detect collision between two bodies (one has convex shape and another one has circle shape) and it failed! even I use tags to identify these bodies.
How can I detect two bodies which have two different shapes (circle and convex)?
ContactListener.h code:
#import "Box2D.h"
class ContactListener : public b2ContactListener
{
private:
void BeginContact(b2Contact* contact);
void EndContact(b2Contact* contact);
};
ContactListener.mm code:
#import "ContactListener.h"
#import "SimpleAudioEngine.h"
#import "cocos2d.h"
void ContactListener::BeginContact(b2Contact* contact) {
b2Body* bodyA = contact->GetFixtureA()->GetBody();
b2Body* bodyB = contact->GetFixtureB()->GetBody();
CCSprite* spriteA = (CCSprite*)bodyA->GetUserData();
CCSprite* spriteB = (CCSprite*)bodyB->GetUserData();
if (spriteA != NULL && spriteB != NULL) {
if (spriteA.tag == 1 && spriteB.tag == 50) { // this is work (two shapes are circles
[[SimpleAudioEngine sharedEngine] playEffect:#"Pin.wav"];
NSLog(#"Contact With Pin is Beginning");
}
if (spriteA.tag == 1 && spriteB.tag == 51) { // this is not work (one is circle shape and another one is convex shape)
[[SimpleAudioEngine sharedEngine] playEffect:#"Jump.wav"];
NSLog(#"Contact With Barrier is Beginning");
}
}
}
void ContactListener::EndContact(b2Contact* contact) {
b2Body* bodyA = contact->GetFixtureA()->GetBody();
b2Body* bodyB = contact->GetFixtureB()->GetBody();
CCSprite* spriteA = (CCSprite*)bodyA->GetUserData();
CCSprite* spriteB = (CCSprite*)bodyB->GetUserData();
if (spriteA != NULL && spriteB != NULL) {
}
}
Tags on above code:
tag of ball (main sprite of game) shape is 1
tag of circle shape is 50
tag of convex shape is 51
Edit:
This is code of object's bodies creation:
#define pinSpriteTag 50
#define barrierSpriteTag 51
#define ballSpriteTag 1
// Creating ball of game (has b2CircleShape)
-(void) createBallOfGameWithPositionX:(int)x yPosition:(int)y radius:(float)radius {
// Put ball on screen
ballOfGameSprite = [CCSprite spriteWithFile:#"GameBall-1-ipad.png"];
ballOfGameSprite.position = ccp(x, y);
[self addChild:ballOfGameSprite z:1 tag: ballSpriteTag];
// Create body of ball
b2BodyDef ballOfGameBodyDef;
ballOfGameBodyDef.type = b2_staticBody;
ballOfGameBodyDef.position.Set(ballOfGameSprite.position.x/PTM_RATIO, ballOfGameSprite.position.y/PTM_RATIO);
ballOfGameBodyDef.userData = ballOfGameSprite;
ballOfGameBody = world->CreateBody(&ballOfGameBodyDef);
// Create Physics properties of ball
b2CircleShape ballOfGameShape;
ballOfGameShape.m_radius = radius/PTM_RATIO;
// Create fixture of ball
b2FixtureDef ballOfGameFixtureDef;
ballOfGameFixtureDef.shape = &ballOfGameShape;
ballOfGameFixtureDef.density = 0.9f;
ballOfGameFixtureDef.friction = 1.0f;
ballOfGameFixtureDef.restitution = 0.9f;
ballOfGameBody->CreateFixture(&ballOfGameFixtureDef);
}
// Creating pin of game (has b2CircleShape)
-(void) createNormalPinOnScreenWithPositionX:(int)x yPosition:(int)y radius:(float)radius {
// Put ball on screen
CCSprite *pin = [CCSprite spriteWithFile:#"GamePinNormal-ipad.png"];
pin.position = ccp(x, y);
[self addChild:pin z:2 tag:pinSpriteTag];
// Create body of ball
b2BodyDef pinBodyDef;
pinBodyDef.type = b2_staticBody;
pinBodyDef.position.Set(pin.position.x/PTM_RATIO, pin.position.y/PTM_RATIO);
pinBodyDef.userData = pin;
b2Body *pinBody = world->CreateBody(&pinBodyDef);
// Create Physics properties of ball
b2CircleShape pinShape;
pinShape.m_radius = radius/PTM_RATIO;
// Create fixture of ball
b2FixtureDef pinFixtureDef;
pinFixtureDef.shape = &pinShape;
pinFixtureDef.density = 0.9f;
pinFixtureDef.friction = 1.0f;
pinFixtureDef.restitution = 0.85f;
pinBody->CreateFixture(&pinFixtureDef);
}
// Creating Barrier of game (has b2PolygonShape)
-(void) createBarrierOnScreenPositionX:(int)x yPosition:(int)y imageName:(NSString *)imageName verts:(b2Vec2*)verts verNum:(int)verNum {
CCSprite *BarrierOfGameSprite = [CCSprite spriteWithFile:imageName];
BarrierOfGameSprite.position = ccp(x, y);
[self addChild:BarrierOfGameSprite z:1 tag:barrierSpriteTag];
b2BodyDef BarrierOfGameBodyDef;
BarrierOfGameBodyDef.type = b2_staticBody;
BarrierOfGameBodyDef.position.Set(BarrierOfGameSprite.position.x/PTM_RATIO, BarrierOfGameSprite.position.y/PTM_RATIO);
BarrierOfGameBodyDef.userData = BarrierOfGameSprite;
b2Body *BarrierOfGameBody = world->CreateBody(&BarrierOfGameBodyDef);
b2PolygonShape BarrierOfGameShape;
BarrierOfGameShape.Set(verts, verNum);
b2FixtureDef BarrierOfGameFixtureDef;
BarrierOfGameFixtureDef.shape = &BarrierOfGameShape;
BarrierOfGameFixtureDef.density = 0.9f;
BarrierOfGameFixtureDef.friction = 1.0f;
BarrierOfGameFixtureDef.restitution = 0.0f;
BarrierOfGameBody->CreateFixture(&BarrierOfGameFixtureDef);
}
Do not detect the collision between two shape.
The best way is to set the object as the userData of the body.
#implemention RigidBody
-(id)init{
...
self.body->userData = self;
}
And then compare the class and address
void beginContact(b2contact contact){
RigidBody *A = (RigidBody*)contact->GetFixtureA()->GetBody()->GetUserData();
if([A isKindOfClass:[RigidBody class]]){
}
// Or
if(self == A){
}
I hope it 's helpful for you.

Creating a array of Box2D body

i have a ball which is a box2d body and moves around the screen... i want to create multiple balls of similar type and they should also collide among them self..
the code i am using now is
ballcount = [[levelData objectForKey:#"ballcount"]intValue] ;
ballarray = [[NSMutableArray arrayWithCapacity:ballcount]init] ;
for (int j=0; j<ballcount; j++) {
ball = [CCSprite spriteWithFile:#"ball.png"];
[ballarray insertObject:ball atIndex:j];
[self createBallBoundingBox:(CCSprite *)[ballarray objectAtIndex:j]];
[[ballarray objectAtIndex:j]setPosition:ccp(arc4random() % 480 , arc4random() % 320)];
[self addChild:[ballarray objectAtIndex:j]];
}
There is only one sprite appearing on the screen when i run it ? any suggestions what am i doing wrong ... it works perfectly when only one ball is there
Thanks
ok i have got the above code to work but now i am having a problem with the movement in the tick method .. the box2d shapes are moving but the CCSprite shapes are not getting attached to the box2d bodies here is my code
world->Step(dt, 10, 10);
for(b2Body *b = world->GetBodyList(); b; b=b->GetNext()) {
if (b->GetUserData() != NULL) {
CCSprite *sprite = (CCSprite *)b->GetUserData();
if ([sprite isKindOfClass:[Ball class]])
{
b2Vec2 Vel = b->GetLinearVelocity();
float32 angle = atan2(Vel.y, Vel.x);
angle += -M_PI/2;
b->SetTransform(b->GetPosition(),angle);
sprite.position = ccp(b->GetPosition().x * PTM_RATIO,
b->GetPosition().y * PTM_RATIO);
sprite.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
}
Ok guys here is my createBoundingBox definition
-(void)createBallBoundingBox:(Ball *)ballSprite{
b2BodyDef BallBodyDef;
ballBodyDef.type = b2_dynamicBody;
ballBodyDef.position.Set(ballSprite.position.x/PTM_RATIO, ballSprite.position.y/PTM_RATIO);
ballBodyDef.userData = ballSprite;
ballBody = world->CreateBody(&ballBodyDef);
/// test circle shape on ballbody
b2CircleShape BallCircleShape;
BallCircleShape.m_radius = 10/PTM_RATIO;
// Create shape
/*
b2PolygonShape ballShape;
int num = 7;
b2Vec2 verts[] = {
b2Vec2(0.0f / PTM_RATIO, 19.2f / PTM_RATIO),
b2Vec2(-10.7f / PTM_RATIO, 15.2f / PTM_RATIO),
b2Vec2(-6.7f / PTM_RATIO, -3.2f / PTM_RATIO),
b2Vec2(-1.7f / PTM_RATIO, -18.0f / PTM_RATIO),
b2Vec2(7.7f / PTM_RATIO, 0.5f / PTM_RATIO),
b2Vec2(10.5f / PTM_RATIO, 14.7f / PTM_RATIO),
b2Vec2(0.2f / PTM_RATIO, 19.0f / PTM_RATIO)
};
ballShape.Set(verts,num);
*/
// Create shape definition and add to body
b2FixtureDef ballFixtureDef;
ballFixtureDef.shape = &ballCircleShape;
ballFixtureDef.density = 1.0f;
ballFixtureDef.friction = 0.0f;
ballFixtureDef.restitution = 1.0f;
ballFixture = ballBody->CreateFixture(&ballFixtureDef);
b2Vec2 direction(5,2);
direction.Normalize();
float force = 1.0f;
b2Vec2 position = ballBody->GetPosition();
//Apply linear velocity
ballBody->ApplyLinearImpulse(force*direction,ballBody->GetPosition());
b2Vec2 Vel = ballBody->GetLinearVelocity();
float32 angle = atan2(Vel.y, Vel.x);
angle += -M_PI/2;
ballBody->SetTransform(ballBody->GetPosition(),angle);
}
any ideas ? Let me know
Thanks a lot guys
inside your method
[self createBallBoundingBox:(CCSprite *)[ballarray objectAtIndex:j]];
//OR NOW
[self createMiceBoundingBox:tempmice];
did you forgot to attach the Ball as UserData to your b2Body
- (void) createMiceBoundingBox: (Ball*) ball
{
//...
b2Body body = ...
body.SetUserData(ball);
//..
}
if not show us the code of the method
EDIT: if you use Mice instead of Ball make sure you update tick method to
if ([sprite isKindOfClass:[Mice class]])

Updating emitter.gravity on a CCParticleSystemQuad on touch event

I've created a simple cocos2d iPhone game that has a CCParticleSystemQuad emitter that emits particles across the scene that are shaped like leaves, to give the illusion of wind.
Right now, the wind (gravity) is blowing across the scene from right to left. I am currently stuck trying to figure out how to update the emitter.gravity to switch from (-500, 80) to (500, 80), hopefully without removing the particles that have already been drawn.
In this example, I'd like the switch to occur on a touch event that happens anywhere on the screen.
What should my touch event look like?
How do I detect a touch that happens any where on the screen?
I've also never implemented a scheduled update loop. Is this the direction I should be thinking? And I suppose a more basic question is, am I going about this the right way?
Here's the code I've got so far:
My init:
-(id) init
{
if( (self=[super init])) {
CCSprite * sky = [CCSprite spriteWithFile:#"sky.png"];
[self addChild:sky z:0 tag:1];
windDirection = -200;
[self leaveEmitters];
}
return self;
}
My leaveEmiiters function
-(void) leaveEmitters{
NSLog(#"The wind is :%i", windDirection);
CCParticleSystemQuad * emitter;
emitter = [[CCParticleSystemQuad alloc] initWithTotalParticles:100];
emitter.texture = [[CCTextureCache sharedTextureCache] addImage: #"particlesLeaves.png"];
emitter.emitterMode = kCCParticleModeGravity;
emitter.duration = -1;
emitter.gravity = ccp(windDirection, -80);
emitter.angle = 0;
emitter.angleVar = 360;
emitter.speed = 10;
emitter.speedVar = 100;
emitter.radialAccelVar = 0;
emitter.tangentialAccel = 0;
emitter.tangentialAccelVar = 0;
emitter.life = 10;
emitter.lifeVar = 0;
emitter.startSpin = 0;
emitter.startSpinVar = 360;
emitter.endSpin = 0;
emitter.endSpinVar = 720;
ccColor4F startColorVar = {255, 100, 0, 0};
ccColor4F startColor = {0, 240,0, 255};
emitter.startColor = startColor;
emitter.startColorVar = startColorVar;
emitter.endSize = emitter.startSize;
emitter.startSize = 60.0f;
emitter.emissionRate = 3;
emitter.blendAdditive = NO;
emitter.position = ccp(500,250);
[self addChild: emitter z:10];
emitter.autoRemoveOnFinish = YES;
CCParticleSystemQuad * emitter2;
emitter2 = [[CCParticleSystemQuad alloc] initWithTotalParticles:100];
emitter2.texture = [[CCTextureCache sharedTextureCache] addImage: #"particlesLeaves.png"];
emitter2.emitterMode = kCCParticleModeGravity;
emitter2.duration = -1;
emitter2.gravity = ccp(windDirection, 0);
emitter2.angle = 0;
emitter2.angleVar = 360;
emitter2.speed = 10;
emitter2.speedVar = 100;
emitter2.radialAccelVar = 0;
emitter2.tangentialAccel = 0;
emitter2.tangentialAccelVar = 0;
emitter2.life = 10;
emitter2.lifeVar = 0;
emitter2.startSpin = 0;
emitter2.startSpinVar = 360;
emitter2.endSpin = 0;
emitter2.endSpinVar = 720;
emitter2.startColor = startColor;
emitter2.endSize = emitter.startSize;
emitter2.startSize = 60.0f;
emitter2.emissionRate = 3;
emitter2.blendAdditive = NO;
emitter2.position = ccp(-500,250);
[self addChild: emitter2 z:10];
emitter2.autoRemoveOnFinish = YES;
}
And finally, my ccTouchesBegan function, which isn't working at all. Why?
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
int x = location.x;
int y= location.y;
CCSprite * sky = (CCSprite *) [self getChildByTag:1];
sky.anchorPoint = ccp(0, 0);
CGRect skyHitBox = CGRectMake(sky.position.x, sky.position.y, 500, 500);
if (CGRectContainsPoint(skyHitBox, location)) {
NSLog(#"touch accepted: x: %i y:%i", x, y);
}
}
Any help, feedback, or suggested learning direction would be GREATLY appreciated. Thanks so much!
EDIT: I answered my own questions like 20 seconds after I posted this...
If someone wants to post their own answers, I'll leave this up for 7 more hours.
you just can keep a CCParticleSystemQuad * emitter as private instance member so you can modify emitter.gravity inside the ccTouchesBegan
i didnt get exactly what kind of wind effect you'r trying to achieve. If you want to simply switch from one 500,80 to -500,80 its pretty easy
emitter.gravity = ccp(-emitter.gravity.x, emitter.gravity.y);
If you want to change the wind direction based on witch side of the skybox you hit, with hitting the center equal no wind, and more your touch is far from the center the stronger the wind will get.
I didnt try this but should be a good starting point.
You first need to get get the touched point in skybox coordinates
CGPoint skyTouchedPoint = ccp( x - sky.position.x, y - sky.position.y)
then you can define the center point of you sky box as
CGPoint skyCenterPoint = ccp(sky.size.width/2, sky.size.height/2);
and get a vector pointing to the right side, if you touch the right half side of the skybox and pointing left if you touch the left half side of the skybox
CGPoint windDirection = ccpSub(skyTouchedPoint, skyCenterPoint);
this vector module is higher the more far from the center you touch happened, you should now increase this vector module by a factor that suites for you with
windDirection = ccpMult(windDirection, 10.f); //try different values here
now you can use only the x component of this vector if you want the wind to use always the same y (as you posted the value of 80)