SetContactListener() public method

Register a contact event listener
public SetContactListener ( ContactListener listener ) : void
listener ContactListener
return void
Example #1
0
        public PhysicsWorld(Box2DX.Common.Vec2 gravity, Box2DX.Common.Vec2 worldLowerBound, Box2DX.Common.Vec2 worldUpperBound)
        {
            AABB bound = new AABB();

            bound.LowerBound.Set(worldLowerBound.X, worldLowerBound.Y);
            bound.UpperBound.Set(worldUpperBound.X, worldUpperBound.Y);
            world = new World(bound, gravity, true);
            Engine.Physics.ContactListener listener = new Engine.Physics.ContactListener();
            world.SetContactListener(listener);
        }
Example #2
0
        public InGameState()
        {
            //position the score display centered at the top
            ScoreDisplay.CharacterSize = 90;
            ScoreDisplay.DisplayedString = InGameState.WinCount0.ToString();
            float leftSize = ScoreDisplay.GetLocalBounds().Width;
            ScoreDisplay.DisplayedString += " : ";
            float midSize = ScoreDisplay.GetLocalBounds().Width - leftSize;
            ScoreDisplay.DisplayedString += InGameState.WinCount1.ToString();
            ScoreDisplay.Origin = new Vector2(leftSize + 0.5f * midSize, 0f);
            ScoreDisplay.Position = new Vector2(Constants.windowSizeX / 2 , Constants.windowSizeY / 14);
            ScoreDisplay.Color = new SFML.Graphics.Color(200, 255, 200);

            AABB aabb = new AABB();
            aabb.LowerBound.Set(0.0f, 0.0f);
            aabb.UpperBound.Set(800, 600/*Constants.worldSizeX * Constants.screenRatio*/);

            physicsWorld = new World(aabb, new Vec2(0.0f, -9.81f), false);

            contactManager = Physics.ContactManager.g_contactManager;
            physicsWorld.SetContactListener(contactManager);

             // Set new Players and appending dekoHands
            ResetPlayers();
            setDekoFlags();
            //0xF0A58A4
            groundPolygonAct = new Actors.PolygonActor(physicsWorld, new Vec2(0.0f, 15.0f), 0xFBA58A4, Actors.FunctionType.GradientNoise, 4);

            BackgroundBackSprite = new Sprite(AssetManager.getTexture(AssetManager.TextureName.InGameBackGroundBack));
            BackgroundBackSprite.Scale = new Vector2(Constants.windowSizeX / (float)BackgroundBackSprite.TextureRect.Width, Constants.windowSizeY / (float)BackgroundBackSprite.TextureRect.Height);//0.5F * Vector2.One;
            BackgroundFrontSprite = new Sprite(AssetManager.getTexture(AssetManager.TextureName.InGameBackGroundFront));
            BackgroundFrontSprite.Scale = BackgroundBackSprite.Scale;

            //left and right borders of the map
            BodyDef bodydef = new BodyDef();
            bodydef.Position = new Vector2(0,0);
            bodydef.Angle = 0.0f;
            PolygonDef box = new PolygonDef();
            box.SetAsBox(1f, Constants.worldSizeY);

            Body leftEdge = physicsWorld.CreateBody(bodydef);
            contactManager.addNonLethalShape(leftEdge.CreateShape(box));

            bodydef.Position = new Vector2(Constants.worldSizeX-1, 0);
            Body rightEdge = physicsWorld.CreateBody(bodydef);
            contactManager.addNonLethalShape(rightEdge.CreateShape(box));

            bodydef.Position = new Vector2(0, Constants.worldSizeY);
            box.SetAsBox(Constants.worldSizeX, 1f);
            Body topEdge = physicsWorld.CreateBody(bodydef);
            contactManager.addNonLethalShape(topEdge.CreateShape(box));
        }
Example #3
0
        /// <summary>
        /// ワールドを初期化
        /// </summary>
        /// <param name="worldRect">適用範囲</param>
        /// <param name="gravity">重力</param>
        public World(RectF worldRect, Vector2F gravity)
        {
            physicsCollider     = new List <PhysicsColliderNode>();
            collisionController = new CollisionController(this);
            AABB aabb = new AABB();

            aabb.LowerBound = worldRect.Position.ToB2Vector();
            aabb.UpperBound = (worldRect.Position + worldRect.Size).ToB2Vector();
            B2World         = new Box2DX.Dynamics.World(aabb, gravity.ToB2Vector(false), true);
            B2World.SetContactListener(collisionController);
            B2World.SetContactFilter(new ContactFilter());
            TimeStep           = 1.0f / 60.0f;
            VelocityItetions   = 8;
            PositionIterations = 1;
        }
Example #4
0
 public PhysicsWorld(Box2DX.Common.Vec2 gravity, AABB worldBoundaries)
 {
     world = new World(worldBoundaries, gravity, true);
     Engine.Physics.ContactListener listener = new Engine.Physics.ContactListener();
     world.SetContactListener(listener);
 }
Example #5
0
        /// <summary>
        /// Called to setup the arena boundaries and bind to all of the physics stuff.
        /// </summary>
        public virtual void Bind()
        {
            float width = dimensions.X, height = dimensions.Y;

            worldAABB = new AABB();
            worldAABB.LowerBound.Set(-width / 2 - edgeTolerance, -edgeTolerance);
            worldAABB.UpperBound.Set(width / 2 + edgeTolerance, height + edgeTolerance);

            Vec2 gravity = new Vec2(this.gravity.X, this.gravity.Y);
            bool doSleep = true;
            world = new World(worldAABB, gravity, doSleep);
            world.SetContactFilter(new ContactFilter());

            BodyDef groundBodyDef = new BodyDef();
            groundBodyDef.Position.Set(0.0f, 0.0f);

            Body groundBody = world.CreateBody(groundBodyDef);

            // Bottom
            AddBoundaryBlock(groundBody, 0, -(boundaryThickness / 2), width + boundaryThickness * 2, boundaryThickness);
            // Top
            AddBoundaryBlock(groundBody, 0, height + boundaryThickness / 2, width + boundaryThickness * 2, boundaryThickness);
            // Left
            AddBoundaryBlock(groundBody, -(width / 2) - boundaryThickness / 2, height / 2, boundaryThickness, height + boundaryThickness * 2);
            // Right
            AddBoundaryBlock(groundBody, +(width / 2) + boundaryThickness / 2, height / 2, boundaryThickness, height + boundaryThickness * 2);

            DebugDraw draw = new OpenTKDebugDraw();
            draw.Flags = DebugDraw.DrawFlags.Shape;
            if (Program.DebugDraw)
            {
                world.SetDebugDraw(draw);
            }

            world.SetContactListener(this);

            // Old code for reference

            /*
             *

             * _worldAABB = new AABB();
            _worldAABB.LowerBound.Set(-30.0f, -20.0f);
            _worldAABB.UpperBound.Set(30.0f, 40.0f);
            Vec2 gravity = new Vec2();
            gravity.Set(0.0f, -10.0f);
            bool doSleep = true;
            _world = new World(_worldAABB, gravity, doSleep);
            _world.SetContactFilter(new ContactFilter());

            BodyDef groundBodyDef = new BodyDef();
            groundBodyDef.Position.Set(0.0f, 0.0f);

            Body groundBody = _world.CreateBody(groundBodyDef);

            AddBlock(groundBody, 0, -5, 40, 10);
            AddBlock(groundBody, -20, 20, 1, 40);
            AddBlock(groundBody, 20, 20, 1, 40);
            AddBlock(groundBody, 0, 30, 40, 10);

            DebugDraw draw = new OpenTKDebugDraw();
            draw.Flags = DebugDraw.DrawFlags.Shape;
            if (Program.DebugDraw) {
                _world.SetDebugDraw(draw);
            }

            _world.SetContactListener(this);
             */
        }
Example #6
0
        /// <summary>
        /// Initialise world with the specified SimulationParameters.
        /// </summary>
        /// <param name="simParams"></param>
        protected void InitSimulationWorld(SimulationParameters simParams)
        {
            _simParams = simParams;
            _world = CreateBox2DWorld();

            // Allow physics calcs to use values from previous timestep.
            _world.SetWarmStarting(_simParams._warmStarting);

            // Enable additional collision detection for high speed objects (that might not ever contact each other at a given timestep due to speed).
            _world.SetContinuousPhysics(_simParams._continuousPhysics);

            // Put stuff in the world.
            PopulateWorld();

            // Create contact listener.
            ContactListener contactListener = CreateContactListener();
            if(null != contactListener) {
                _world.SetContactListener(contactListener);
            }
        }
Example #7
0
        public bool SetupPhysics(Vector2 gravity, Vector2 maxVertex, Vector2 minVertex)
        {
            if (_physicsSetUp)
                return false;

            AABB bounds = new AABB();
            bounds.LowerBound = minVertex;
            bounds.UpperBound = maxVertex;
            _physicsWorld = new Box2DX.Dynamics.World(bounds, gravity, true);

            if (_bThreadedPhysics)
            {
                _physicsThread = new Thread(new ThreadStart(
                    delegate()
                    {
            #if XBOX
                        Thread.CurrentThread.SetProcessorAffinity(4);
            #endif
                        while (_bThreadedPhysics)
                        {
                            _runPhysics.WaitOne();
                            if (_bThreadedPhysics)  // If this is now false, we're shutting down
                            {
                                RunPhysics(_lastDt);
                                _waitPhysics.Set();
                            }
                        }
                    }
                ));
                _physicsThread.Start();
            }

            // TODO: Physics debug draw, console variables, etc.
            //_physicsWorld->SetListener(this);

            //_physicsDebugDraw = new /*Physics*/DebugDraw();
            //_physicsWorld->SetDebugDraw(_physicsDebugDraw);

            //CONSOLE_DECLAREVAR(phys_gravity);
            //if (phys_gravity->HasVal())
            //{
            //    Vector2 grav = phys_gravity->GetVector2Val();
            //    _physicsWorld->m_gravity = b2Vec2(grav.X, grav.Y);
            //}

            _physicsWorld.SetContactListener(_collisionManager);

            _physicsSetUp = true;
            return true;
        }
Example #8
0
        public Test()
        {
            Vec2 gravity = new Vec2();
            gravity.Set(0.0f, -10.0f);
            bool doSleep = true;
            _world = new World(gravity, doSleep);
            _bomb = null;
            _textLine = 30;
            _mouseJoint = null;
            _pointCount = 0;

            _destructionListener.test = this;
            _world.SetDestructionListener(_destructionListener);
            _world.SetContactListener(this);
            _world.SetDebugDraw(_debugDraw);

            _bombSpawning = false;

            _stepCount = 0;

            BodyDef bodyDef = new BodyDef();
            _groundBody = _world.CreateBody(bodyDef);
        }
Example #9
0
		public Test()
		{
			_worldAABB = new AABB();
			_worldAABB.LowerBound.Set(-200.0f, -100.0f);
			_worldAABB.UpperBound.Set(200.0f, 200.0f);
			Vec2 gravity = new Vec2();
			gravity.Set(0.0f, -10.0f);
			bool doSleep = true;
			_world = new World(_worldAABB, gravity, doSleep);
			_bomb = null;
			_textLine = 30;
			_mouseJoint = null;
			_pointCount = 0;

			_destructionListener.test = this;
			_boundaryListener.test = this;
			_contactListener.test = this;
			_world.SetDestructionListener(_destructionListener);
			_world.SetBoundaryListener(_boundaryListener);
			_world.SetContactListener(_contactListener);
			_world.SetDebugDraw(_debugDraw);
		}