Page 1 of 1

constraints

Posted: Fri Nov 02, 2012 6:22 am
by savo89
Hi!
It's the first time that I have to use constraints, but I get an error when I add the joint to the space.
Can you please help me to find what's wrong!?
Thank you!

Code: Select all

///METHODS TO CREATE BODIES
- (void)createDinamicBody:(cpBody*)body AtLocation:(CGPoint)location {
    
    float boxSize = 60.0;
    float mass = 1.0;
    
    body = cpBodyNew(mass, cpMomentForBox(mass, boxSize, boxSize));
    body->p = location;
    cpSpaceAddBody(space, body);
    
    shapeDinamic = cpBoxShapeNew(body, boxSize, boxSize);
    shapeDinamic->e = 1.0;
    shapeDinamic->u = 1.0;
    shapeDinamic->data = box_img;
    cpSpaceAddShape(space, shapeDinamic);
    
    NSLog(@"DINAMIC");
    
    
    
}

- (void)createStaticBody:(cpBody*)body AtLocation:(CGPoint)location{
    
    body = cpBodyNew(INFINITY, INFINITY);
    body->p = location;
    shapeStatic = cpBoxShapeNew(body, 60, 40);
    shapeStatic->data = slider_img;
    cpSpaceAddStaticShape(space, shapeStatic);
  
    
    NSLog(@"STATIC");
   
    
}

///MAIN, NOTE: I POST ONLY THE INTERESTING CODE
       [self createDinamicBody:box AtLocation:ccp(240, 160)];
       [self createStaticBody:slider AtLocation:ccp(240, 250)];
    
        cpConstraint* joint = cpGearJointNew(box, slider, 0.5, 0.5);
        cpSpaceAddConstraint(space, joint); /// THIS IS THE LINE OF THE ERROR!!!!!
------
i get this error:

/// Returns true if the body has not been added to a space.
static inline cpBool cpBodyIsRogue(const cpBody *body)
{
	return (body->CP_PRIVATE(space) == ((cpSpace*)0));
}
...but I've added bodies to the space!!! The code is ok, and all goes right if I remove that line!

I've also another question: can I move manually the static body if I've decleared it in this way?? ...I cant understand the difference between static and rogue body...

Than you again!

Re: constraints

Posted: Fri Nov 02, 2012 11:04 am
by slembcke
So C is pass by value. Google for "pass by value" if you want more information.

The short version is that the "body" variable in the createDinamicBody: method is not the same variable as where you call the method. So you are passing an invalid pointer to cpGearJointNew(), instead of the one you used inside createDinamicBody:.

Re: constraints

Posted: Sat Nov 03, 2012 6:11 am
by savo89
@slembcke thankyou! How can I create methods like those, that work fine? I mean, how I can pass the pointer to the cpBody?

Re: constraints

Posted: Sat Nov 03, 2012 3:25 pm
by slembcke
The usual way is to return the body from the method instead. ex:

Code: Select all

-(cpBody *)createBody
{
  cpBody *body = cpBodyNew(...)
  // more code here
  
  return body;
}
I'm guessing that you are new to Objective-C and C? You might want to go through some tutorials on C's language basics. Objective-C is built straight on top of C, so you'll need to fair number of low level things about how pointers, function arguments, the stack and the like work.

Re: constraints

Posted: Sun Nov 04, 2012 4:26 am
by savo89
@slembcke
thank you for helping me! ...I'm not very new to C & OBC, I sent 3 apps on the AppStore! :p but sometimes I forget something about pointers! :D