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

  • SEARCH
  • Home
  • 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 7180391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:20:15+00:00 2026-05-28T17:20:15+00:00

I’m playing around with box2djs . I’m trying to find a way to write

  • 0

I’m playing around with box2djs. I’m trying to find a way to write an ‘onCollision()’ callback function, but the documentation is sparse and I can’t find an obvious way to do it.

Thanks!

  • 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-05-28T17:20:15+00:00Added an answer on May 28, 2026 at 5:20 pm

    Call back for box2djs is actually referenced as a collision “filter.” Here is how you can implement it. I’ll also show what I’m doing instead which maybe is a little slower but since my other approach is outside the step() I can destroy objects and stuff:

    // Called whenever a collision occurs in the world
    //
    
    var JellyCollisionCallback = function()
        {
        // Required function - this is the function the gets called when b2ContactManager registers a collision between two bodies
        //
        this.ShouldCollide = function( shape1, shape2 )
            {
            // These are the two bodies…
            //
            var cBody1 = shape1.m_body;
            var cBody2 = shape2.m_body;
    
            // I'm setting userData when I create the body object
            //
            var jellyObject1 = cBody1.GetUserData();
            var jellyObject2 = cBody2.GetUserData();
    
    
            // This is the code from the default collision filter
            //
            if (shape1.m_groupIndex == shape2.m_groupIndex && shape1.m_groupIndex != 0)
                {
                return shape1.m_groupIndex > 0;
                }
    
            var collide = (shape1.m_maskBits & shape2.m_categoryBits) != 0 && (shape1.m_categoryBits & shape2.m_maskBits) != 0;
    
            return collide;
            }
    
        return this;
        }
    
    
    function createWorld()
        {
        var world = new b2World(worldAABB, gravity, doSleep);
    
        myCollisionCallback = new JellyCollisionCallback();
    
        world.SetFilter(myCollisionCallback );
        }
    

    probably not as nice as a true callback, but I had originally had trouble trying to get a callback to work so I wrote this approach and ended up keeping it instead. I do this in my main loop that calls the world.step()

    // Find collisions between selected objects
    //
    // (world is the main b2World object)
    //
    var aContact;
    for ( aContact = world.m_contactList; aContact != null; aContact = aContact.m_next )
        {
        var cBody1 = aContact.m_shape1.m_body;
        var cBody2 = aContact.m_shape2.m_body;
    
        // I'm setting userData when I create the body object
        //
        var jellyObject1 = cBody1.GetUserData();
        var jellyObject2 = cBody2.GetUserData();
    
        // Not one of my controlled Objects
        //
        if ( typeof(jellyObject1) != "object" || jellyObject1 == null )
            continue;
        if ( typeof(jellyObject2) != "object" || jellyObject2 == null )
            continue;
    
        // Call my collision event for the colliding objects
        //
        jellyObject1.dink();
        jellyObject2.dink();
        }
    

    documentation is pretty much unavailable from what I found, but I really like box2djs and have finally figured out everything I need to accomplish some simple hobby projects. Here are some examples that extend the original box2djs demo jellyrobotics box2djs project

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I have a French site that I want to parse, but am running into
I am trying to render a haml file in a javascript response like so:

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.