[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4762: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3897)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4764: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3897)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4765: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3897)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4766: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3897)
Chipmunk Physics • View topic - Adding objects during execution (randomly)

Adding objects during execution (randomly)

Official forum for the .

Adding objects during execution (randomly)

Postby xissburg » Mon Nov 22, 2010 3:00 pm

I'm working with chipmunk+cocos2d in an iPad application.

I want to add objects to the world while the physics simulation is running.

One approach would be to add a kind of 'pre step callback' to be run in every iteration of the main loop of the physics simulation, but afaik there's no such functionality in chipmunk, or am I wrong? Another similar one would be to add a post step callback, which exists but I can't add a post step callback that will be executed forever, they're ran just once and removed.

Other approach is to use another thread which randomly adds these objects, which is what is partially working now. In this thread I add one post step callback and then sleep for a few random seconds. In this callback I just add one more object to the world. But at one time I get a EXC_BAD_ACCESS, and my app crashes, and its is probably a concurrency problem.

Then, what is the right way to do this?


Thanks in advance.
xissburg
 
Posts: 10
Joined: Mon Nov 22, 2010 2:50 pm

Re: Adding objects during execution (randomly)

Postby slembcke » Mon Nov 22, 2010 3:14 pm

I'm not quite sure why you would want a pre-step callback. You can just add objects to the space before calling cpSpaceStep(). You don't really need any special functionality to do it. The problem comes when Chipmunk is iterating one of its data structures and you add or remove things from it.

If you want to use threads, you need to put lock on your accesses to the space, but honestly you don't want to use threads.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Re: Adding objects during execution (randomly)

Postby xissburg » Mon Nov 22, 2010 3:41 pm

Hm I see...since I'm using SpaceManager I don't manage the physics main loop, I don't call cpSpaceStep() myself, thats why I was talking about a pre step call, which is what I'd like to do. Using threads may be a good approach, I think there won't be many attempts to access the critical section at the same time by the two threads because I won't add objects frequently, but I don't want this additional overhead, its unnecessary.

Do you know whether this is easy to do in SpaceManager?

Thanks.
xissburg
 
Posts: 10
Joined: Mon Nov 22, 2010 2:50 pm

Re: Adding objects during execution (randomly)

Postby slembcke » Mon Nov 22, 2010 5:27 pm

Just schedule a Cocos2D timer to add the objects then. Like I said, the only time you need to worry about adding or removing objects is when you are in a callback from Chipmunk. Any other time is 100% safe because Cocos2D is not multithreaded and you will never get a timer callback that happens while cpSpaceStep() is running.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Re: Adding objects during execution (randomly)

Postby mobilebros » Tue Nov 23, 2010 12:07 am

slembcke is right, you should have no problems. I do the exact thing you're describing all the time (with SpaceManager).
mobilebros
 
Posts: 90
Joined: Tue Aug 04, 2009 9:53 am

Re: Adding objects during execution (randomly)

Postby xissburg » Tue Nov 23, 2010 12:10 pm

Wow that is neat, works perfectly ! Thank you guys.
xissburg
 
Posts: 10
Joined: Mon Nov 22, 2010 2:50 pm


Return to Chipmunk2D Physics

Who is online

Users browsing this forum: No registered users and 0 guests

cron