Rotate static body

Official forum for the Chipmunk2D Physics Library.
Post Reply
Odin
Posts: 1
Joined: Mon Oct 17, 2011 9:10 am
Contact:

Rotate static body

Post by Odin »

Hi,

I'm experiencing a problem: i've created a body an i want to rotate it when user rotates the device.

Body creation code:

Code: Select all

        space = cpSpaceNew();
	cpSpaceResizeStaticHash(space, 400.0f, 40);
	cpSpaceResizeActiveHash(space, 100, 600);
        
        space->gravity = ccp(0, -100);
	space->elasticIterations = space->iterations;
        
        CGFloat x = screenSize.width / 2;
        CGFloat y = screenSize.height / 2;
        
#define ratio 32
        
        staticBody = cpBodyNew(INFINITY, INFINITY);
        cpShape *shape;

        
        shape = cpSegmentShapeNew(staticBody, ccp(x + -3.4*ratio, y + 3.4*ratio), ccp(x + 3.4*ratio, y + 3.4*ratio), 2.f);
        shape->e = 1.0f; shape->u = 1.0f;
	cpSpaceAddStaticShape(space, shape);
        
        shape = cpSegmentShapeNew(staticBody, ccp(x + 3.4*ratio, y + 3.4*ratio), ccp(x + 3.4*ratio, y + -3.4*ratio), 2.f);
        shape->e = 1.0f; shape->u = 1.0f;
	cpSpaceAddStaticShape(space, shape);
        
        shape = cpSegmentShapeNew(staticBody, ccp(x + 3.4*ratio, y + -3.4*ratio), ccp(x + 1.2*ratio, y + -3.4*ratio), 2.f);
        shape->e = 1.0f; shape->u = 1.0f;
	cpSpaceAddStaticShape(space, shape);
        
        shape = cpSegmentShapeNew(staticBody, ccp(x + 1.2*ratio, y + -3.4*ratio), ccp(x + 1.2*ratio, y + 1.07*ratio), 2.f);
        shape->e = 1.0f; shape->u = 1.0f;
	cpSpaceAddStaticShape(space, shape);
         
        shape = cpSegmentShapeNew(staticBody, ccp(x + 1.2*ratio, y + 1.07*ratio), ccp(x + -3.3*ratio, y + 1.07*ratio), 2.f);
        shape->e = 1.0f; shape->u = 1.0f;
	cpSpaceAddStaticShape(space, shape);
        
        shape = cpSegmentShapeNew(staticBody, ccp(x + -3.3*ratio, y + 1.07*ratio), ccp(x + -3.4*ratio, y + 1.15*ratio), 2.f);
        shape->e = 1.0f; shape->u = 1.0f;
	cpSpaceAddStaticShape(space, shape);
        
        shape = cpSegmentShapeNew(staticBody, ccp(x + -3.4*ratio, y + 1.15*ratio), ccp(x + -3.4*ratio, y + 3.4*ratio), 2.f);
        shape->e = 1.0f; shape->u = 1.0f;
	cpSpaceAddStaticShape(space, shape);
And that's how i'm trying to rotate this body:

Code: Select all

-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
    rollingX = (acceleration.x * kFilteringFactor) + (rollingX * (1.0 - kFilteringFactor));
    
    rollingY = (acceleration.y * kFilteringFactor) + (rollingY * (1.0 - kFilteringFactor));
    
    rollingZ = (acceleration.z * kFilteringFactor) + (rollingZ * (1.0 - kFilteringFactor));
    
    float accelX = acceleration.x - rollingX;
    float accelY = acceleration.y - rollingY;
    float accelZ = acceleration.z - rollingZ;
    
    CGFloat angleAcceleration = atan2(accelY, -accelX);

    cpBodySetAngVel(staticBody, angleAcceleration);
}
Thank you in advance,
Looking forward for your advice!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Rotate static body

Post by slembcke »

It's probably helpful to read this section of the docs first:
http://chipmunk-physics.net/release/Chi ... ougeStatic

A bit over a year ago before I added the sleeping feature, you would create static shapes by creating an infinite mass body, not adding it to the space (so it wouldn't be simulated), and use cpSpaceAddStaticShape() to attach static shapes to it.

Since adding the sleeping feature I needed to be able to tell the difference between really static bodies (ones that rarely if ever change) and "rogue" bodies (user controlled bodies that aren't added to the space). So now what you would do is to use cpBodyNewStatic() to create your static body or just use cpSpace.staticBody. They have a special flag set on them so that when you call cpSpaceAddShape() it knows it should be a static shape.

BUT! You don't actually want a static body. What you want a rogue body so that you can control it manually. The "Tumble" demo that comes with Chipmunk sounds like it shows you exactly what you want to do:
https://github.com/slembcke/Chipmunk-Ph ... o/Tumble.c

I create a rogue body with an infinite mass and moment of inertia, set the angular velocity I want, then call cpBodyUpdatePosition() manually before I call cpSpaceStep().
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 9 guests