Switching Worlds

Hello, I have a pinball game I've been working on for a couple of weeks. It's running like a dream (with some very helpful advice from these forums). I'm trying to simulate an upper level in the pinball game. Basically a sensor detects the ball is about to enter the access point for the second level then I want the ball to move about the second level until the exit sensor is triggered and the ball is placed back in the lower level.

Plan A : Second level is a caged rail, define the shape in physicseditor and disable it at the start of the game. When the sensor is hit, I wanted to disable the shapes on the lower level, activate the second level, fire the ball around the upper level cage shape then when it hits the exit sensor, disable the level 2 rail and enable the level 1 board. This worked perfectly except all of the flipper fixtures on level 1 flew off to space when I deactivated the level 1 board.

Plan B: Define the level 2 cage rail as a separate shape in physics editor, create a second world and add the level 2 caged rail to it. When the ball in level 1 (world 1) hits the enter rail sensor destroy the ball in world 1, recreate it in world 2, fire it around the level 2 caged rail until it trips the exit rail sensor, destroy the world 2 level 2 ball and recreate it in level 1 etc. This also worked perfectly except a phantom version of the balls is still kicking around the level 1 board despite my research into destroying balls, not destroying them mid render but instead queing their destruction for deletion before next render.. still getting invisible balls roaming the game as you move from caged rail and back to the base board.

I'm happy to post code, let me know what you need to see. This is driving me wonderfully nuts 🙂

Thanks in advance.
Update : having 2 worlds and switching between them is working, the ball hits the 'enter rail' sensor, the ball is que'd for destruction, a new ball is created in world 2, fired, hits 'exit rail' sensor and is que'd for destruction, a new ball is made in world 1 with the velocity from the ball in world 2. This all works fine the first time, the second time around when world 1 ball hits 'enter rail' sensor the world step fails alla :

Code: Select all

TypeError: Error #1009: Cannot access a property or method of a null object reference.
   at Box2D.Collision::b2DynamicTree/GetFatAABB()
   at Box2D.Collision::b2DynamicTreeBroadPhase/TestOverlap()
   at Box2D.Dynamics::b2ContactManager/Collide()
   at Box2D.Dynamics::b2World/Step()
   at Main/update()


Not sure what to look at to get this sorted. Balls that are que'd for destruction are added to an array which is detected in the world step 'update' function. If the array length is greater than 0 then the bodies within it are destroyed before calling 'step'.
Update : depending on whether the ball is in world 1 or world 2 I step THAT world only. When the ball moves from world 1 to world 2 and back to world 1 it works fine, the second time around, when the ball leaves world 1 to world 2, the world 2 'step' fails :

Code: Select all

onCageEnter : entering cage
delete_body.length = 1, [object b2World],[object b2Body]
deleting world [object b2Body]
delete body complete, length is : 0
update : stepping cageWorld
TypeError: Error #1009: Cannot access a property or method of a null object reference.
   at Box2D.Collision::b2DynamicTree/GetFatAABB()
   at Box2D.Collision::b2DynamicTreeBroadPhase/TestOverlap()
   at Box2D.Dynamics::b2ContactManager/Collide()
   at Box2D.Dynamics::b2World/Step()
   at Main/update()


I've heard that there can be nasty consequences to destroying objects at random times in your code, my system destroys them in sequence before the next render... I believe that is the safest way. Here is the code that switches the balls between the worlds when the 'enter rail' sensor in world 1 is hit

Code: Select all

      private function onCageEnter(e:Event):void
      {
         if (!inCage)
         {
            trace('onCageEnter : entering cage')
            var tmpArray:Array = new Array(world, wheelBody)
            _delete_body.push(tmpArray);
            cageWheelBody = cageWorld.CreateBody(wheelBodyDef);
            cageWheelBody.CreateFixture(wheelFixtureDef);
            cageWheelBody.SetPosition(new b2Vec2(18 / ptm_ratio, 212 / ptm_ratio))
            move_to_top(PBCageGraphic)
            move_to_top(pbBall)
            var startingVelocity:b2Vec2 = new b2Vec2(0, -30)
            cageWheelBody.SetLinearVelocity(startingVelocity);
            pbSoundEngine.playSound(12, 0.1);
            activeWheel = 'cageWheelBody';
            inCage = true
         }
      }
      
      private function onCageExit(e:Event):void
      {
         if (inCage)
         {
            trace('onCageExit : exiting cage');
            var newWheelX:Number = cageWheelBody.GetPosition().x
            var newWheelY:Number = cageWheelBody.GetPosition().y
            var newWheelPosition:b2Vec2 = new b2Vec2(newWheelX, newWheelY);
            var newWheelLV:b2Vec2 = cageWheelBody.GetLinearVelocity()
            var newWheelAV:Number = cageWheelBody.GetAngularVelocity()
            var tmpArray:Array = new Array(cageWorld, cageWheelBody)
            _delete_body.push(tmpArray);
            wheelBody = world.CreateBody(wheelBodyDef);
            wheelBody.CreateFixture(wheelFixtureDef);
            wheelBody.SetPosition(newWheelPosition);
            wheelBody.SetLinearVelocity(newWheelLV);
            wheelBody.SetAngularVelocity(newWheelAV)
            move_to_top(pbBall)
            move_to_top(PBCageGraphic)
            activeWheel = 'wheelBody';
            pbSoundEngine.playSound(12, 0.1);
            inCage = false
         }
      }
SOLVED: Conceptually everything I described as far as cloning the ball to the upper world 2 with inherited velocity then performing the same action to return a clone with identical velocity to world 1 on 'exit sensor' contact was perfect.. the problem was in my delete que'd bodies before render function.. I didn't specify the world to delete from. Everything is working now, very excited. Thanks to Box2D forums for letting me ramble this out on the screen which helped me find the solution 🙂. If anyone has any questions regarding this pinball app or how I handled pinball game elements, sounds etc. between physicseditor, Flash and Box2D feel free to ask here.
This reply is too late for you, but I'll post anyway in case it helps somebody else...

It's easier to just change the collision filter mask bits of the ball instead of making two (or more) separate worlds. For example, the walls in the lower level would be given category bits of 0x1, and the upper level would be 0x2. Changing the mask bits of the ball between 0x1 and 0x2 will dictate which set of walls it should collide with. Deciding when to change the mask bits would be done in the same way as you are already doing, by placing some sensors at the entry and exit to regions where the level should switch.
For more info on collision filtering in general this page might help: http://www.iforce2d.net/b2dtut/collision-filtering
To see some source code (C++) showing an implementation of a simple pinball table with 2 levels, you could look at either the cocos2d or SFML samples on this page: https://www.iforce2d.net/rube/?panel=loaders

Anyhow, good to see you got it sorted!

Original thread: https://box2d.org/forum/viewtopic.php?t=9419