Problem with boxes and triangles

Discuss any Chipmunk bugs here.
Post Reply
ganesha
Posts: 71
Joined: Mon Sep 06, 2010 1:29 pm
Contact:

Problem with boxes and triangles

Post by ganesha »

I have a problem regarding square boxes and triangles. On average every 20:th added object falls thru the 1-2 last added objects occupying the same space as another box (think piling the boxes).

This is big problem with my application because if affects game play significantly.
The problem is not affected by time, i.e. quick or slow adding has the same results. By printing the shape group id I can see that the objects are unique.

Any ideas what is causing this behavior?


Quasi code

Code: Select all

update{
 [self.space step:dt];
 [self loopObjectArrayAndUpdateGraphics];
}

touchScreen {
@synchronized(self){
  // create newObject
  [self addObjectToArray: newObject];
  [self.space addPostStepCallback:self selector:@selector(addObjectToSpace:) key:newObject];
}
}

addObjectToSpace: (id) object {
 [self.space add:object];	
}
Last edited by ganesha on Sat Apr 09, 2011 9:38 am, edited 2 times in total.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Problem with boxes and triangles

Post by slembcke »

Is your touchscreen method being called from a callback? What's with the post-step callback? What does loopAndUpdateObjectArray do? Where does object come from?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
ganesha
Posts: 71
Joined: Mon Sep 06, 2010 1:29 pm
Contact:

Re: Problem with boxes and triangles

Post by ganesha »

Is your touchscreen method being called from a callback?
Yes
What's with the post-step callback?
Isn't that the best preactice to not add objects while possibly doing a space step operation?
What does loopAndUpdateObjectArray do?
Loops the array of objects and updates the graphics with the body position values.
Where does object come from?
Added in the touch method.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Problem with boxes and triangles

Post by slembcke »

I think I'm still missing something.

To be more clear, is the touchscreen method being called from within a collision handler or space query callback? If not, a post-step callback is unnecessary. Also, Chipmunk spaces are not thread safe. You should only be accessing a space from a single thread at a time. If you are hoping that adding a post-step callback from a second thread will avoid crashes if the space is updating on the main thread, this is not the case. If you are using threading that way, it could explain the problem. If you want to be thread safe, you have to use synchronize blocks every time you use the space.

Could you show some more info on how you are constructing the object that you are adding? That could explain some things.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
ganesha
Posts: 71
Joined: Mon Sep 06, 2010 1:29 pm
Contact:

Re: Problem with boxes and triangles

Post by ganesha »

To be more clear, is the touchscreen method being called from within a collision handler or space query callback? If not, a post-step callback is unnecessary.
No I don't use those kind of callbacks. The calls are made asynchronously when the user touches the screen. I have tried with and without the post-step method but still have the same problem.
If you want to be thread safe, you have to use synchronize blocks every time you use the space.
I do, but the error still occurs.

Code: Select all

-(void) addObjectToSpace: (id) object {
    @synchronized(self.space){
        [self.space add:object];
    }
}

Example of problem:

Add 5 boxes A to E vertically in sequence at a low height. They fall down and pile up.

The result should be:

E
D
C
B
A

But is (D & C) occupy the same space without collision:

E
D C
B
A

Off course it is not always C & D.
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests