سؤال

I am using Cocos2d v3 with Chipmunk for physics in my game. My game has a ball that falls down onto a platform with a trap door in it. The platform and the doors are static objects so they will not fall down from the balls velocity or from gravity. The player is in control of opening the trap door, so if the ball is set to rest on the platform or doors it will eventually fall asleep. Once the ball is set to sleeping, when the trap door is opened it won't recognize that an object is no longer under the ball so the ball won't move.

Right now I have a function being called every 0.2 seconds to check on the ball. If it is asleep it sets its velocity to (0, 0) which wakes the ball up. The only problem is that this way reduces the frame rate of the game from 60 to 40-50. (I can have up to 30 different balls in the level at once which slows it down that much)

Is there an easy way to keep the ball from falling asleep or to wake it up when the trap door opens? Or is there a way to keep my trap doors dynamic and not static but not have them move when the ball collides with it.

هل كانت مفيدة؟

المحلول

If you need to wake up the body you can try use the activate method of the ChipmunkBody which is used internally by CCPhysicsBody.

I haven't tried this, just found this option by looking into code of cocos2d. Here is some pseudo-code, that might do the trick:

#import "CCPhysics+ObjectiveChipmunk.h"

//Let's pretend this method is called when you open your trap door
-(void)openTrap
{

   // Get the body of your ball somehow
   CCPhysicsBody *ballBody = ... 

   // Get the body property of CCPhysicsBody, 
   // (which is only available if you import the header at the top)
   // this will return a ChipmunkBody body, which has an 
   // activate function to wake up your ball
   [ballBody.body activate];

}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top