Sprite with touchesMoved

Official forum for the Chipmunk2D Physics Library.

Sprite with touchesMoved

Postby evana » Tue Jan 26, 2010 2:34 am

Hey all i m new in chipmunk.
I have try to do something like dynamic object created with gravity.
i want to move the object using touches moved.. bt i cant...
Here is my full code..

Code: Select all
#import "HelloWorldScene.h"

enum {
   kTagAtlasSpriteManager = 1,
};

void updateShape(void* ptr,void* unused)
{
   cpShape* shape = (cpShape*)ptr;
   Sprite* sprite = shape->data;
   if(sprite)
   {
      cpBody* body = shape->body;
      [sprite setPosition:cpv(body->p.x,body->p.y)];

   }

}

// HelloWorld implementation
@implementation HelloWorld

+(id) scene
{
   // 'scene' is an autorelease object.
   Scene *scene = [Scene node];

   // 'layer' is an autorelease object.
   HelloWorld *layer = [HelloWorld node];

   // add layer as a child to scene
   [scene addChild: layer];

   // return the scene
   return scene;
}

-(void) addNewSprite
{
   //int posx, posy;
   currentBallTag++;
   CGRect rect;
   switch(random()%3)
   {
      case 0: rect = CGRectMake(10,0,45,45); break;
      case 1: rect = CGRectMake(64,0,45,45); break;
      case 2: rect = CGRectMake(125,0,45,45); break;
   }
   cpInitChipmunk();
   if(gravityValue <= 1000.0f)
   {
      gravityValue = gravityValue + 10.0f;
   }
   space->gravity = cpv(0,gravityValue);

   AtlasSpriteManager *mgr = (AtlasSpriteManager*) [self getChildByTag:kTagAtlasSpriteManager];

   AtlasSprite *sprite = [AtlasSprite spriteWithRect:rect spriteManager:mgr];
   int xPos = 0;
   xPos = arc4random()%320;

   if(xPos < 20)
   {
      xPos = 25;
   }
   else if(xPos > 300)
   {
      xPos = 300;
   }
   [sprite setPosition:cpv(xPos,-20)];   

   [mgr addChild: sprite z:1];

   cpBody *body = cpBodyNew(INFINITY, INFINITY);
   body->p = cpv(xPos,-20);
   cpSpaceAddBody(space, body);

   cpShape* shape = cpCircleShapeNew(body,20.0,cpv(xPos,-20));
   shape->e = 0.5f; shape->u = 0.5f;
   shape->data = sprite;
   shape->collision_type = 1;
   cpSpaceAddShape(space, shape);

}
-(id) init
{
   if( (self=[super init])) {

      self.isTouchEnabled = YES;
      self.isAccelerometerEnabled = YES;

      CGSize wins = [[Director sharedDirector] winSize];
      cpInitChipmunk();

      //cpBody *staticBody = cpBodyNew(INFINITY, INFINITY);
      space = cpSpaceNew();

      AtlasSpriteManager *mgr = [AtlasSpriteManager spriteManagerWithFile:@"All.png" capacity:3000000];
      [self addChild:mgr z:0 tag:kTagAtlasSpriteManager];
      gravityValue = 10.0f;
      currentBallTag = 1;
      [self addNewSprite];
      newBallValue = 1;
      [self schedule:@selector(step:) interval: 1.0f/60.0f];
   }

   return self;
}

-(void) onEnter
{
   [super onEnter];

   [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / 60)];
}
-(void) step: (ccTime) delta
{
   cpSpaceStep(space, 1.0f/30.0f);
   cpSpaceHashEach(space->activeShapes, &updateShape, nil);

   if((newBallValue % 30) == 0)
   {

      [self addNewSprite];
   }
   newBallValue ++;
   NSLog(@"%f",gravityValue);

   //NSLog(@"%i",newBallValue);

}
- (void)ccTouchesMoved:(NSSet*)touches withEvent:(UIEvent*)event{
   UITouch *myTouch =  [touches anyObject];
   CGPoint location = [myTouch locationInView: [myTouch view]];
   location = [[Director sharedDirector] convertCoordinate: location];
   //move the nouse to the click
   cpMouseMove(mouse, cpv(location.x, location.y));
   if(mouse->grabbedBody == nil){
      //if there's no associated grabbed object
      // try get one
      cpMouseGrab(mouse, 0);
   }
}

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   UITouch *myTouch =  [touches anyObject];
   CGPoint location = [myTouch locationInView: [myTouch view]];
   location = [[Director sharedDirector] convertCoordinate: location];
   mouse = cpMouseNew(space);
   cpMouseMove(mouse, cpv(location.x, location.y));
   printf("%f,%f/n",location.x,location.y);
   cpMouseGrab(mouse, 0);
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
   [self ccTouchesCancelled:touches withEvent:event];
}
- (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
   cpMouseDestroy(mouse);
}
@end


pls help me pls...
evana
 
Posts: 1
Joined: Sat Nov 21, 2009 11:09 pm

Re: Sprite with touchesMoved

Postby slembcke » Tue Jan 26, 2010 9:53 am

What version of chipmunk are you using and what is the problem exactly?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm


Return to Chipmunk2D Physics

Who is online

Users browsing this forum: No registered users and 0 guests

cron