Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8666717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:47:58+00:00 2026-06-12T17:47:58+00:00

I am using Box2D for the first time seriously in a medium sized Flash

  • 0

I am using Box2D for the first time seriously in a medium sized Flash Game that I am working on. My current experience with Box2D is limited to creating a world, bodies and adding those bodies to the world in a functional manner.

I’m finding it easy enough to integrate Box2D into my game environment, maintaining well-written code and have completed a few tutorials that walk through dealing with collisions. The issue that I’m facing now is that my game will have many bodies, each interacting with other bodies in different ways, and I’m finding it hard to write my own b2ContactListener subclass without it getting extremely messy.

Based off a tutorial I used, I have created my own subclass of b2ContactListener and added an override of the BeginContact() method. The argument that BeginContact() receives when it is called will reference an instance of b2Contact, through which I can access two b2Fixture instances (the two instances that have collided). I am then able to access the b2Body instance associated with each of those b2Fixtures.

  • Problem: Currently I have a roundabout way of finding out what two things collided (i.e. whether they’re a wall and a missile, or the player and a tree, etc) which uses GetUserData() and looks like this as an example:

    var f1Player:Boolean = contact.GetFixtureA().GetBody().GetUserData() is Player
    var f2Player:Boolean = contact.GetFixtureB().GetBody().GetUserData() is Player
    var f1Tree:Boolean = contact.GetFixtureA().GetBody().GetUserData() is Tree
    var f2Tree:Boolean = contact.GetFixtureB().GetBody().GetUserData() is Tree
    // ... continutes with all possible combinations.
    
    
    // Example of managing a collision:
    if(f1Player && f2Tree)
    {
        // Player (FixtureA) and Tree (FixtureB)
    }
    
    if(f2Player && f1Tree)
    {
        // Player (FixtureB) and Tree (FixtureA)
    }
    

    As you can see, this is going to end up extremely long and unmanageable. I also have to write each set of actions to perform twice to cater for a certain element being FixtureA or FixtureB, or vice versa (obviously in the form of a function call with the parameters swapped around rather than literally re-written).

This is clearly not the correct approach, but I haven’t been able to locate resources that more thoroughly explain collision detection management.

Does anyone have experience with collision detection management using Box2D that they can share? Also, is using SetUserData( entityThatOwnsTheBody ); the correct way to be using that method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:47:59+00:00Added an answer on June 12, 2026 at 5:47 pm

    I’ve come up with something much nicer than the original.

    Firstly, I just have my Being class (which owns a b2Body) set itself as its bodies’ UserData. This class will also contain an onContact() method and look similar to the below:

    public class Being
    {
    
        private var _body:b2Body;
    
    
        public function Being()
        {
            // Define the body here.
            // ...
    
            _body.SetUserData(this);
        }
    
    
        public function onCollision(being:Being = null):void
        {
            //
        }
    
    }
    

    Then in my own b2ContactListener implementation, I simply pass the colliding Being (or null, if there is no Being assigned to the colliding b2Body‘s UserData) to the opposing Being‘s onCollision():

    override public function BeginContact(contact:b2Contact):void
    {
        var bodyA:b2Body = contact.GetFixtureA().GetBody();
        var bodyB:b2Body = contact.GetFixtureB().GetBody();
    
        var beingA:Being = bodyA.GetUserData() as Being || null;
        var beingB:Being = bodyB.GetUserData() as Being || null;
    
        beingA && beingA.onCollision(beingB);
        beingB && beingB.onCollision(beingA);
    }
    

    And finally in each of my subclasses of Being, I can easily prepare logic appropriate for a collision between other Beings of a certain type:

    class Zombie extends Being
    {
        override public function onCollision(being:Being = null):void
        {
            if(being && being is Bullet)
            {
                // Damage this Zombie and remove the bullet.
                // ...
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Salut, I'm currently working on a pool game in flash, using box2d for physics
I'm using AndEngine/Box2d to develop a game. I have a ball that bounces around
I'm working on a platformer for the iPhone that is using Box2D and cocos2D.
i'm using box2d for the first time, and i've set up my shapes via
I am using Box2D with Cocos2D in my game. I am aware that Cocos2D
I'm using Box2d for a Bike Physics Game, Box2d lets you have fixtures that
I'm using Box2D (the AS3/Flash version, if it matters) and I'm trying to calculate
I am using Box2d for a topdown game. The ground is a series of
I'm using AndEngine and Box2d to develop a game. I'm applying a force to
I'm writing an XNA game in C# using the XNA port of Box2d -

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.