Move terrain up

Official forum for the Chipmunk2D Physics Library.
Post Reply
rudra
Posts: 7
Joined: Sat Feb 25, 2012 11:49 am
Contact:

Move terrain up

Post by rudra »

Hello. I'm new to Chipmunk and trying to use autogenerated terrain like in cloud bomber. But when I try to move this generated body to the top of the screen then it looks fine but somehow all bodies collide upside down or not colliding but not cut the terrain at all. Looks like something bad with collision coordinates. My terrain is not the same as the screen size, just an image with small amount of empty space.

Code: Select all

_terrain = [[CCSprite alloc] initWithFile:@"terrain.png"];
_terrain.anchorPoint = CGPointZero;
_terrain.position = ccp(10, 220);
Thanks for the help.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Move terrain up

Post by slembcke »

Could you post a screenshot of the issue with the debug drawing enabled? I'm not sure I understand.

It sounds like you are moving the sprite without moving any of the Chipmunk stuff with it so they are just misaligned.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
rudra
Posts: 7
Joined: Sat Feb 25, 2012 11:49 am
Contact:

Re: Move terrain up

Post by rudra »

Here is a screenshot. As you can see points where ball collides with target object is not the same as places where geometry has holes. Also there is only one left hole with a border. If I just move up by changing position of the initial terrain from your bomber example then there is no holes at all. But everything is ok if I make my target 320*480 size just with transparency.
Attachments
Image 10.04.13 at 19.37.png
Image 10.04.13 at 19.37.png (61.07 KiB) Viewed 4814 times
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Move terrain up

Post by slembcke »

Oh I see.

So an issue with the CloudBomber thing was that it was set up to do a pretty specific screen size. Originally I thought it was pretty easy to change the size, you'd just need to change a few of the parameters. Then I actually listed out the steps for somebody and realized that it was more involved than I thought. Change the size of the render buffer, change a couple matrices, etc.

That was one of the reasons for the Gemeralds Pinball example. It has a newer version of the render buffer sampler class that is trivial to set the bounds to whatever size you want. You should take a look at that too:
https://github.com/slembcke/Gemeralds/b ... rSampler.h
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
rudra
Posts: 7
Joined: Sat Feb 25, 2012 11:49 am
Contact:

Re: Move terrain up

Post by rudra »

Great! Thanks a lot, Slembcke. Will look at Gemeralds.
rudra
Posts: 7
Joined: Sat Feb 25, 2012 11:49 am
Contact:

Re: Move terrain up

Post by rudra »

Hi Slembcke. I'm still trying to solve this problem. Here is what I have after I used some code from Gemeralds.

Here is how I add the object. This is a CCPhysicsSprite with ChipmunkObject protocol added.

Code: Select all

CGSize pixels = [CCDirector sharedDirector].winSizeInPixels;
_target = [[AutoGeometrySprite alloc] initWithFile:@"ball_0.png"];
_target.position = ccp(pixels.width / 2.0f, pixels.height / 2.0f);
_target.space = _space;
[self addChild:_target];
As you can see target(that blue head) should be in the center of the screen.

Here is init:

Code: Select all

- (id)initWithFile:(NSString *)filename {
    if((self = [super init])) {
        CGSize pixels = [CCDirector sharedDirector].winSizeInPixels;
        
        if(self.isStatic)
			self.chipmunkBody = [ChipmunkBody staticBody];
		else
			self.chipmunkBody = [ChipmunkBody bodyWithMass:INFINITY andMoment:INFINITY];

        mutableSprite = [CCSprite spriteWithFile:@"ball_5.png"];
        mutableSprite.anchorPoint = ccp(0, 0);
        
        renderTexture = [[CCRenderTexture alloc] initWithWidth:pixels.width height:pixels.height pixelFormat:kCCTexture2DPixelFormat_RGBA8888];
        renderTexture.position = ccp(0, 0);
        renderTexture.sprite.anchorPoint = ccp(0.5, 0.5);
        [self addChild:renderTexture z:0];
        
        sampler = [[ChipmunkGLRenderBufferSampler alloc] initWithXSamples:pixels.width ySamples:pixels.height];
        sampler.renderBounds = (CGRect){CGPointZero, pixels};
        sampler.borderValue = 0.0;
    }
    
    return self;
}
And this is how I add holes(copied from bomber):

Code: Select all

- (void)addHoleAtLocation:(CGPoint)location {
    CGPoint loc = [mutableSprite convertToNodeSpace:location];
    CCSprite *scorch = [CCSprite spriteWithFile:@"Scorch.png"];
    scorch.position = loc;
    scorch.rotation = 360.0*CCRANDOM_0_1();
    scorch.scale = 1.0;
    scorch.blendFunc = (ccBlendFunc){GL_ZERO, GL_SRC_COLOR};
    [mutableSprite addChild:scorch];
    
    [self updateTexture];
}
Rendering method:

Code: Select all

- (void)updateTexture {
    [renderTexture beginWithClear:0 g:1 b:0 a:1];
    [mutableSprite visit];
    [renderTexture end];
    
    [sampler renderInto:^{
        [mutableSprite visit];
    }];
    
    for(ChipmunkShape *seg in _chipmunkObjects)
        [_space remove:seg];

    _chipmunkObjects = [self buildPoly];
}
And finally poly creation method:

Code: Select all

- (NSArray *)buildPoly {
    cpFloat mass = 0.0;
	cpFloat moment = 0.0;
    cpFloat downsample = 1;
    ChipmunkBody *body = self.chipmunkBody;
    NSMutableArray *polyObjects = [@[] mutableCopy];
    
    CGAffineTransform transform = CGAffineTransformIdentity;
    transform = CGAffineTransformTranslate(transform, 0, 0);
    transform = CGAffineTransformRotate(transform, -body.angle);
	transform = CGAffineTransformScale(transform, downsample, downsample);
    
    for(ChipmunkPolyline *polyline in [sampler marchAllWithBorder:TRUE hard:FALSE]){
		cpFloat area = polyline.area;
        
		if(area <= 0.0)
            continue;
        
        for(ChipmunkPolyline *hull in [polyline toConvexHulls_BETA:1]) {
            int count = hull.count - 1;
            cpVect transformed[hull.count];
            
            for(int i = 0; i < count; i++)
                transformed[i] = CGPointApplyAffineTransform(hull.verts[i], transform);
            
            cpFloat density = 1;
            cpFloat m = area * density;
            mass += m;
            moment += cpMomentForPoly(m, count, transformed, cpvzero);
            
            ChipmunkShape *shape = [ChipmunkPolyShape polyWithBody:body count:count verts:transformed offset:cpvzero];
            shape.friction = 1;
            shape.elasticity = 1;
            shape.layers = COLLISION_LAYERS_TERRAIN;
            shape.collisionType = PhysicsIdentifier(COLLISION_TYPE_TERRAIN);
            [polyObjects addObject:shape];
            [_space add:shape];
        }
	}
    
    if(!body.isStatic) {
        body.mass = mass;
        body.moment = moment;
    }
    
    return polyObjects;
}
From screenshots you can see that body created successfully the first time but not in the center. Sprite at one side, body at another. Also scratches are on their place - on the border of the body. Blue background is a parent layer. But unfortunately body doesn't have a correct shape after added holes.
Please advice what can be wrong. Thanks.
Attachments
iOS Simulator Screen shot 14 квіт. 2013 20.25.04.png
iOS Simulator Screen shot 14 квіт. 2013 20.25.04.png (50.53 KiB) Viewed 4785 times
iOS Simulator Screen shot 14 квіт. 2013 20.16.09.png
iOS Simulator Screen shot 14 квіт. 2013 20.16.09.png (21.2 KiB) Viewed 4785 times
rudra
Posts: 7
Joined: Sat Feb 25, 2012 11:49 am
Contact:

Re: Move terrain up

Post by rudra »

Hi Slembcke. Finally done! Have updated to 6.1.4 and automagically it works! Thanks for the help.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Move terrain up

Post by slembcke »

Sorry I wasn't around yesterday.

6.1.4 fixed it? Hmm. What version were you using before? Looking over the release notes, I don't see what would have caused that. I'm interested to know if there is something I still need to fix.
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 22 guests