Positions of static bodies off (Objective Chipmunk 6.0.3)

Discuss any Chipmunk bugs here.
Post Reply
chad2408m
Posts: 4
Joined: Sun Feb 12, 2012 4:52 pm
Contact:

Positions of static bodies off (Objective Chipmunk 6.0.3)

Post by chad2408m »

Hi there, I am having a very annoying issue with adding static bodies to a space using objective chipmunk. I started by following the Simple-Objective-Chipmunk tutorial, and whenever I use "addBounds:elasticity:friction:..." it seems to be offset by 25 to the right, and 25 down (when using self.view.bounds). So to fix it I simply use CGRectMake(-25, -25, x, y).

However, once adding a static body to the shape, it has a correct x position but the y position is moved 50 up from where I put in the value. And upon adding a second static body inside the space it has a messed up x and y position, so there doesn't seem to be any rhyme or reason to it. Can anyone help me figure out why this would be doing what its doing?

Code for space:

Code: Select all

    world = [[ChipmunkSpace alloc] init]; // Create the space
    [world addBounds:CGRectMake(-25, -25, self.view.bounds.size.width, self.view.bounds.size.height)
           thickness:10.0f 
          elasticity:1.0f
            friction:1.0f
              layers:CP_ALL_LAYERS
               group:CP_NO_GROUP
       collisionType:@"border"];
Code to add a static body:

Code: Select all

       _body = [ChipmunkBody staticBody]; // Creates a static body
        _body.pos = cpv(rect.origin.x, rect.origin.y + 50); // <- The +50 corrects the first displacement
        ChipmunkShape *shape = [ChipmunkPolyShape boxWithBody:_body width:rect.size.width  height:rect.size.height]; // Creates the shape
        shape.friction = 1.0f;
        shape.elasticity = 1.0f;
        
        [view addSubview:_view];        
        [space addStaticShape:shape];
The _view object is basically just a black rectangle thats added to the parent view at (rect). Any help is greatly appreciated!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Positions of static bodies off (Objective Chipmunk 6.0.3

Post by slembcke »

A UIView's bounds are relative to it's own coordinates, it's frame is relative to it's parent. You want to be using frame instead I think.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
chad2408m
Posts: 4
Joined: Sun Feb 12, 2012 4:52 pm
Contact:

Re: Positions of static bodies off (Objective Chipmunk 6.0.3

Post by chad2408m »

I changed it from bounds to frame, but that still didn't fix anything. The issue is that the x/y coordinates between what is being seen (adding to uiview) and used (ChipmunkSpace add:) aren't lining up. And when I update the position of the static body using view.transform = [body affineTransform], it moves way off to the side at a completely off/irrelevant location.

If it helps, I'll attach the full object code creation. I feel someone here will know a lot more than me and be able to help more if able to see the full code.

Code: Select all

- (id)initWithFrame:(CGRect)rect inSpace:(ChipmunkSpace *)space inView:(UIView *)view {
    if (self = [super init]) {
        _view = [[UIView alloc] initWithFrame:rect]; // Creates the view
        _view.backgroundColor = [UIColor blackColor];
        
        
        _body = [ChipmunkBody staticBody];
        _body.pos = rect.origin;
        ChipmunkStaticPolyShape *shape = [ChipmunkStaticPolyShape boxWithBody:_body width:rect.size.width height:rect.size.height]; // Creates the shape
        
        NSLog(@"X = %f   Y = %f", shape.body.pos.x, shape.body.pos.y);
        
        shape.friction = 1.0f;
        shape.elasticity = 1.0f;
        shape.collisionType = [StaticBox class];
        shape.data = self;
        
        [view addSubview:_view];  
        _chipmunkObjects = [[NSArray alloc] initWithObjects:shape, nil];
        [space add:self];
    }
    
    return self;
}
A way for me to temporarily get around it was to use bounds instead (which do line-up properly), however objects with high velocities have gotten stuck in them.. which is no good :)
(The -23, -23 is necessary to get it to line-up properly.. as previously stated for some reason everything is shifted 25 units down and right)

Code: Select all

[space addBounds:CGRectMake(rect.origin.x -23, rect.origin.y -23, rect.size.width - 2, rect.size.height - 2) thickness:1.0f elasticity:1.0f friction:.8f layers:CP_ALL_LAYERS group:CP_NO_GROUP collisionType:@"StaticBox"]
chad2408m
Posts: 4
Joined: Sun Feb 12, 2012 4:52 pm
Contact:

Re: Positions of static bodies off (Objective Chipmunk 6.0.3

Post by chad2408m »

Okay so I've made some peculiar observations about this issue..

Its always offset to the bottom right to some degree, and it only happens on boxes (not spheres). I am using the view.transform = body.affineTransform method as I believe im supposed to, but it still doesn't work. Can anybody help me with this?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Positions of static bodies off (Objective Chipmunk 6.0.3

Post by slembcke »

Ok, sorry for leaving you hanging. I've had this window open all week. :-\

Some things to keep in mind: The origin of a CGRect is not the center, it's the upper right corner. boxWithBody:width:height: creates a box shape centered on the body. So that is one issue. You probably want to use UIView.center as your body's position as that (or the view's layer's anchorPoint) is what the transform works relative to.

You might find this thread useful as well:
http://chipmunk-physics.net/forum/viewt ... 9106#p9106
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
chad2408m
Posts: 4
Joined: Sun Feb 12, 2012 4:52 pm
Contact:

Re: Positions of static bodies off (Objective Chipmunk 6.0.3

Post by chad2408m »

Hey don't worry about it, I'm just happy you replied at all! :D

But sadly my issue still stands :(

I uploaded a video of the problem to youtube, i tried both the self.center and self.layer.anchorPoint and self.frame.origin and nothing seems to work! I don't believe I have anything else wrong, but the code is there for you to glance over (there's not a whole lot). It must be a very elementary mistake because its a very common thing to add boxes haha.

Please if you can take a look and try to help me with what wrong, if you need any more info or source I can provide it. I've been baffled for days on this!

http://youtu.be/9I7m2E7ftIg
usandhar
Posts: 18
Joined: Thu Jun 13, 2013 4:56 pm
Contact:

Re: Positions of static bodies off (Objective Chipmunk 6.0.3

Post by usandhar »

Did you ever reached resolution on this? I am having a similar problem i.e. the space bounds don't seem to completely overlap UIView bounds and therefore the ChipmunkBody objects go partially off the screen. In addition, I have another problem - ChipmunkBody objects when colliding on partially on top of each other. Please help. Thanks.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest