Move static shapes, or another method?

Official forum for the Chipmunk2D Physics Library.
mrTortex
Posts: 51
Joined: Sat Jul 24, 2010 9:38 am
Contact:

Re: Move static shapes, or another method?

Post by mrTortex »

That's cool, I'm happy to move the shape - I just cant work out how. I can't find the position property, or x and y properties of the shape.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Move static shapes, or another method?

Post by slembcke »

I should clarify my previous statement. You can move shapes by moving the body they are attached to. When you change the position of a body that hasn't been added to a Chipmunk space you have to make sure to keep the position changes and velocity in sync or objects will act strange when touching it's shapes. They will either bounce off the surface or mush into it.

There is also the chipmunk_unsafe.h header that you can #include separately from chipmunk.h to change some shape parameters after creating them. In general, the functions in that header don't really do what you want and will cause mushy collisions which is why the header isn't loaded by chipmunk.h.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
mobilebros
Posts: 90
Joined: Tue Aug 04, 2009 9:53 am
Contact:

Re: Move static shapes, or another method?

Post by mobilebros »

Yeah, the body is still there, even though you removed it from the space, you didn't free it and the shape is still dependent on it. In fact try doing a cocos2d move action on it and see what happens ;)

Code: Select all

[node runAction:[MoveBy actionWithDuration:3 position:ccp(30,30)]];
mrTortex
Posts: 51
Joined: Sat Jul 24, 2010 9:38 am
Contact:

Re: Move static shapes, or another method?

Post by mrTortex »

Hello.

OK, forgive me for being really stupid. I create a shape in smgr, remove the body from the smgr space, add a cpCCSprite to the shape and add that to my layer and all is good.

However, I now want to lock the position of that collision shape and the sprite to a sprite within a parallax layer. In my tick method I'm doing:

myShape->body->p = mySprite.position;

This compiles, and runs, but the body just sits still. My body refuses to move anywhere, it's stubborn!
mrTortex
Posts: 51
Joined: Sat Jul 24, 2010 9:38 am
Contact:

Re: Move static shapes, or another method?

Post by mrTortex »

Or, is it my shape that's not moving? The sprite moves, but the collision shape is just sitting where I put it. So I don't think the shape is moving with the body. Damn this is confusing.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Move static shapes, or another method?

Post by slembcke »

There are 3 issues that I can think of:
Are you sure the shape is being added as a normal non-static shape and not getting removed somewhere?
Are you sure the line where you are moving the body is actually getting called?
Are you sure that myShape is the correct shape?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
mrTortex
Posts: 51
Joined: Sat Jul 24, 2010 9:38 am
Contact:

Re: Move static shapes, or another method?

Post by mrTortex »

Hello.

I'm trying it using mobilebros' example code (I've also done this exact same thing with standard Chipmunk syntax, same behavior):

Code: Select all

//Create the rect
cpShape *shape = [smgr addRectAt:pos mass:mass width:width height:height rotation:0];

//Remove body if you want a "static-like" moveable object
cpSpaceRemoveBody(smgr.space, shape->body);

//If you want a drawn shape attached
cpShapeNode *node = [cpShapeNode nodeWithShape:shape];

//Blue's a fun color
node.color = ccBLUE;

//Assuming you're within a Layer or other CCNode type
[self addChild:node];
then in my tick method:

Code: Select all

shape->body->p = someSprite.position;
The sprite moves as it's in a parallax layer, but the shape and collisions stay where they were initially put, in this case (160, 10).
mrTortex
Posts: 51
Joined: Sat Jul 24, 2010 9:38 am
Contact:

Re: Move static shapes, or another method?

Post by mrTortex »

Hello.

OK, so the first part of my code now is this:

Code: Select all

ppShape = [smgr addRectAt:ccp(160, 100) mass:INFINITY width:92 height:12 rotation:0];
	cpSpaceRemoveBody(smgr.space, ppShape->body);
	ppSprite = [cpCCSprite spriteWithShape:ppShape file:@"level1Pad.png"];
and to move the body I'm trying:

Code: Select all

ppShape->body->p = ccp(_level1S.position.y + 10, _level1S.position.x + 160);
_level1s is a sprite within a parallax node. The problem I'm having is the ppShape body sits wherever I put it initially, so here it sits at 160,100. I need to have that body, and the cpCCSprite attached to it, move relative to something else - ideally I want it to stay still in X, but move in Y along with a background sprite which moves in a parallax node.

I know this is something simple, I'd post the entire project if it would help. If I could just move the body I'd be happy, I don't mind using the same line of code several times to do the same move to a sprite and shape and body.

Thanks.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 7 guests