Cocos2D setPosition moving entire space!

Official forum for the Chipmunk2D Physics Library.
GarrettVD
Posts: 44
Joined: Fri Jul 22, 2011 7:18 pm
Location: Brantford, ON, Canada
Contact:

Cocos2D setPosition moving entire space!

Post by GarrettVD »

Hi all,
Just trying to wrap my head around an issue I am experiencing with setting the position of individual bodies in an existing Chipmunk space. Here is the scenario in greater detail:

I am working on a skeletal animation + ragdoll creation engine that can be used in lots of different applications. To create a new character of type CCNode, I do the following:

Code: Select all

// Gameplay.m
...
Character* ninja = [Character characterFromConfigFile:@"ninja-v1.xml"];
// Pass in the space I wish to add the ragdoll to (ragdoll.m)
[ninja createRagdollWithSpace:space];
[ninja setPosition:ccp(200,200)];
// Ninja is a CC2d node, add it to the scene in Gameplay.m
[self addChild:ninja];
...
// Create the floor / platform
cpBody* floorBody = cpBodyNewStatic(1000.0, INFINITY);
floorBody->p = ccp(0, 0);
cpVect verts[] = { cpv(0.0, 50.0), cpv(400.0, 50.0), cpv(480.0, 0.0), cpvzero };  
cpShape* floorShape = cpPolyShapeNew(floorBody, 4, verts, cpvzero);
floorShape->collision_type = GROUND_COLLISION_TYPE;
cpSpaceAddBody(space, floorBody);
cpSpaceAddStaticShape(space, floorShape);
...
Now, in the ragdoll file, that's where I will add the shapes and bodies to the space, as well as create constraints...

Code: Select all

// For each body part specified in the config XML file, I loop through this code snippet:
-(void) createRagdollWithSpace:(cpSpace*) _space {
...
cpBodySetAngle(body, -CC_DEGREES_TO_RADIANS(rot));
cpSpaceAddBody(_space, body);
cpSpaceAddShape(_space, shape);
...
}
Now, that's all fine and dandy and works as it should. The problem comes when I try to change the position of the ninja CCNode, but maintain the position the floorShape / body. When I do the following from GamePlay.m...

Code: Select all

[ninja setPosition:ccp(200,200)];
... Not only does it move the ninja 200 x 200 from the cpvzero position, but the floor as well! See the attached screenshot.

Any ideas on how to fix this?

Thanks!
-G.
Attachments
The white lines are a debug so I can see the outline of the shapes + their respective pivot points.  You can see the character of course, but the outline of the floor. NOTE that the center of gravity for the character is it's red lower Torso block, and it's center of gravity appears to overlap the floor body's.
The white lines are a debug so I can see the outline of the shapes + their respective pivot points. You can see the character of course, but the outline of the floor. NOTE that the center of gravity for the character is it's red lower Torso block, and it's center of gravity appears to overlap the floor body's.
screenshot.jpg (32.58 KiB) Viewed 8331 times
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Cocos2D setPosition moving entire space!

Post by slembcke »

First of all, are you certain that the position of the drawn shapes match their actual positions? Probably about than half of the time people have similar problems is because the graphics are wrong and the physics is right. There are just a lot more ways to get the graphics wrong when you are working with hierarchical transformations like with CCNodes.

I'm not sure if I've seen enough code to make any guesses otherwise. Are you drawing the collision shapes from inside the ninja's Node?

You might want to grab the ChipmunkDebugNode class from here:
http://chipmunk-physics.net/tutorials/A ... pmunks.tgz

It's written for Objective-Chipmunk, but it should be trivial to modify to use with vanilla Chipmunk.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
GarrettVD
Posts: 44
Joined: Fri Jul 22, 2011 7:18 pm
Location: Brantford, ON, Canada
Contact:

Re: Cocos2D setPosition moving entire space!

Post by GarrettVD »

Hi Slembcke,
I am creating the collision shapes within Ragdoll.m, but I am adding them to the space that I passed in from GamePlay.m.

I actually figured out how to move the character independent from the floor, by calling a "setNewPosition" from Gameplay.m, like this:

Code: Select all


// GamePlay.m
...
[ninja setNewPositino:cpv(200,200)];
...

// Ragdoll.m
-(void) setNewPosition:(CGPoint)_newPosition
{
    for (NSString *elStr in self.childrenTable) {

        // Get the CCSprite character
        CharacterSpriteElement *el = [self getChildByName:elStr];
        el.position = ccpAdd(_newPosition, el.position);
            
        // If there are collision shapes...
        // Note that the "floor" is not in this "ShapeTable" dictionary because it is not part of the ragdoll.
        if ([shapeTable count] > 0) {
            cpShape *shape = (cpShape*)[[shapeTable objectForKey:elStr] pointerValue];
            cpBodySetPos(shape->body, el.position);
        }
    }
}
The above code will move the "ninja" independent from the floor; but now, the floor also because subject to gravity within the space as well for some reason, and begins to fall at the same rate as the "ninja" character.


Hmmm... You are probably correct about this being a graphics issue; just trying to figure exactly what is going on here. :S

The debug script I was using is from here: http://doodleblast.blogspot.com/2010/04 ... odies.html

I had to update it to work with CP6. But I'll check out the debug class you sent me! EDIT: Just tried the debug class and got it working; but I still get the same results! Myabe a movie clip of what I am talking about would help? EDIT 2: Video is here: http://www.youtube.com/watch?v=YEcBOoU2ei4

-G.
Last edited by GarrettVD on Tue Sep 13, 2011 1:16 pm, edited 1 time in total.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Cocos2D setPosition moving entire space!

Post by slembcke »

If your ground shape is falling, it's because you didn't attach it to a static body or you accidentally added a static body to the space (the latter will be a hard error in 6.0.2).
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
GarrettVD
Posts: 44
Joined: Fri Jul 22, 2011 7:18 pm
Location: Brantford, ON, Canada
Contact:

Re: Cocos2D setPosition moving entire space!

Post by GarrettVD »

slembcke wrote:If your ground shape is falling, it's because you didn't attach it to a static body or you accidentally added a static body to the space (the latter will be a hard error in 6.0.2).
That's what I thought too; but I made sure to create a static body. Is there something else I need to do?

Code: Select all

// Create the floor / platform
cpBody* floorBody = cpBodyNewStatic(1000.0, INFINITY);
floorBody->p = ccp(0, 0);
cpVect verts[] = { cpv(0.0, 50.0), cpv(400.0, 50.0), cpv(480.0, 0.0), cpvzero }; 
cpShape* floorShape = cpPolyShapeNew(floorBody, 4, verts, cpvzero);
floorShape->collision_type = GROUND_COLLISION_TYPE;
cpSpaceAddBody(space, floorBody);
cpSpaceAddStaticShape(space, floorShape);
Here is my eachShape method, if that helps any.

Code: Select all

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

Ps. I created a Youtube video of the issue above if you wanted to see first-hand.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Cocos2D setPosition moving entire space!

Post by slembcke »

slembcke wrote:If your ground shape is falling, it's because you didn't attach it to a static body or you accidentally added a static body to the space (the latter will be a hard error in 6.0.2).
;)

I guess that code was in the first post too, I just missed it. This might clear up some of the confusion and explain why Chipmunk works the way it does: http://chipmunk-physics.net/release/Chi ... ougeStatic
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
GarrettVD
Posts: 44
Joined: Fri Jul 22, 2011 7:18 pm
Location: Brantford, ON, Canada
Contact:

Re: Cocos2D setPosition moving entire space!

Post by GarrettVD »

Ooops, must have missed that! Had a look at the documentation. From my understanding, this should work, correct?

Code: Select all

// Create the floor / platform
cpBody* floorBody = [b]cpBodyNewStatic(1000.0, INFINITY);[/b]
floorBody->p = ccp(0, 0);
cpVect verts[] = { cpv(0.0, 50.0), cpv(400.0, 50.0), cpv(480.0, 0.0), cpvzero };
cpShape* floorShape = cpPolyShapeNew(floorBody, 4, verts, cpvzero);
floorShape->collision_type = GROUND_COLLISION_TYPE;
cpSpaceAddBody(space, floorBody);
[b]cpSpaceAddShape(space, floorShape);[/b]
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Cocos2D setPosition moving entire space!

Post by slembcke »

Not quite:
1) cpBodyNewStatic() doesn't take any parameters. I'm not sure how that isn't a compiler error.
2) Don't add the static body to the space. You don't want to simulate it (gravity, forces) at all, and in 6.0.2 this will actually trigger an error.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
GarrettVD
Posts: 44
Joined: Fri Jul 22, 2011 7:18 pm
Location: Brantford, ON, Canada
Contact:

Re: Cocos2D setPosition moving entire space!

Post by GarrettVD »

slembcke wrote:Not quite:
1) cpBodyNewStatic() doesn't take any parameters. I'm not sure how that isn't a compiler error.
2) Don't add the static body to the space. You don't want to simulate it (gravity, forces) at all, and in 6.0.2 this will actually trigger an error.

Of course!! Can't believe I didn't think about removing not adding the body itself to the space. That's what hours of coding does to the brain, I suppose.

So that worked great! Now my character makes contact with the floor, with comical results! (See: http://www.youtube.com/watch?v=f7Bkd2RE7yk). Now maybe I am on the wrong train-of-thought here, but I thought that the pivot joint would just create a pivot between two bodies and apply gravity to both of them, regardless of whether the shapes are in the same "group" property? Ideally I want the upper arm to pivot at the shoulder position, upper torso to pivot at the lower, in a manner that would simulate someone dropping to the ground. Know what I mean?

If the shapes have to be in different groups, how can I allow them to overlap a bit to make them appear as if they are connected?


Thanks for your help so far!! Love the CP6 engine.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Cocos2D setPosition moving entire space!

Post by slembcke »

I'm guessing that you have your pivots set up wrong and/or the center of gravity of the bodies. Can you paste some of the relevant code from createRagdollWithSpace:?

Also, I noticed that your eachShape method isn't setting the rotation of the sprites from the rotations of the bodies. (remember to convert from radians to degrees AND negate the angle as Cocos2D's angles are backwards)
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 29 guests