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 6584363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:31:42+00:00 2026-05-25T16:31:42+00:00

I’ve been working on an iOS project, using Cocos2D 1.0 and Box2D, and I’ve

  • 0

I’ve been working on an iOS project, using Cocos2D 1.0 and Box2D, and I’ve run into a bit of a problem.

What I need to be able to do is determine the orientation of a surface my player has hit. For example, if we have a rectangular platform, and the player collides with it, I need to know whether the player has hit the left, right, top, or bottom face of it. ALL the objects in the game are square, and the ONLY one moving is the player.

I’m currently using a b2ContactListener in Box2D (well, my own subclass of one, anyway), and have been playing around with the local normal of the manifold from the contact in BeginContact. The main problem I have is that that normal seems to be affected by the rotation of the player body (e.g. the player has rotated 90 degrees, OR the player is spinning wildly on impact – both situations are giving me trouble), and I seem to end up with ambiguity (i.e. collisions with different faces that give the same normal…) if I try to allow for that – although of course I could just be doing something horribly wrong. Now, I don’t understand manifolds very well, so it’s possible that my problem stems from that, or maybe I’m missing something obvious.

Any suggestions?
I would prefer to do this in the cleanest and least ugly manner possible. Bear in mind that the main categorisation I care about is “player is landing on something from above” vs “everything else”, but I may end up needing the exact
If you need more information or clarification about anything, just ask.

EDIT: Just to clarify, I am aware that the normal points from A to B (in a collision between A and B) by convention in Box2D, and my code does check to see which one is the player and takes this into account before doing any calculations to determine which face has been hit.

  • 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-25T16:31:42+00:00Added an answer on May 25, 2026 at 4:31 pm

    So, I feel a little awkward about answering my own question, but apparently it’s officially encouraged.

    Anyway, the problem with the way I was approaching things was twofold. Firstly, I was using the contact manifold’s local normal instead of the world normal. Secondly, my code for reversing the object transformations was buggy (I would never have needed to do this if I had been using the world manifold).

    The world manifold takes into account object transformations and sizes and as such contains data more easily applicable to the world co-ordinate system.

    By convention in Box2d, the collision normal (for both the world manifold and the contact manifold) points from A to B – this has to be taken into account for some uses, since the normal from A to B is the inverse of the normal from B to A, so you can’t just assume that one body will always be A.

    So, the solution is to use get the world manifold for each collision, examine its normal, and then make whatever decisions you want to make.

    For example, in the BeginContact method of a b2ContactListener subclass (if you have no idea what I’m talking about then check out part 2 of this tutorial):

    void ContactListener::BeginContact(b2Contact* contact)
    {
        b2WorldManifold worldManifold;
        contact->GetWorldManifold(&worldManifold); // this method calls b2WorldManifold::Initialize with the appropriate transforms and radii so you don't have to worry about that
        b2Vec2 worldNormal = worldManifold.normal;
        // inspect it or do whatever you want based on that...  
    }
    

    Since you’ll likely need to check what bodies are colliding, and which one is A and which one is B, you may want to keep a vector of structs containing the fixtures that collided (as in that tutorial) and the normal, and iterate over the vector in your tick() method or similar. (You can get these out of the contact with contact->GetFixtureA() and contact->GetFixtureB().)

    Now, you could get the point data from the world manifold, and make your decisions based on that, but why would you when the normal is already available, since in this particular case the normal (combined with which shapes the normal points from and to) is all that is needed.


    Edit (for @iBradApps):

    First, I’m assuming here that you have followed the tutorial I linked to and have a contact listener set up. If you haven’t, follow it because Ray explains it in depth quite well.

    Second, I want to point out that there is no absolute guarantee which object is A and which is B (well, it depends on what kind of Box2D objects they are; suffice to say if they can both move, you can’t guarantee the ordering, at least as far as I know), so in my case I wanted to see if the player object had hit something, so I created a class variable (b2Fixture *playerF) in my contact listener that stored a reference to the player object so I could determine whether contact A or contact B was the player.

    You asked about detecting a collision where something else collided with the top of B. Something like the following should work, although I haven’t had a chance to test it for you:

    In your ContactListener.h:

    public:
        b2Fixture *playerF;
        // along with the vector etc mentioned in Ray's tutorial
        // and anything else you want
    

    When you make the ContactListener in your init() (assuming you called it _contactListener):

    _contactListener->playerF = playerFixture; // or whatever you called the player body fixture
    

    BeginContact method:

    void ContactListener::BeginContact(b2Contact* contact)
    {
        b2WorldManifold worldManifold;
        contact->GetWorldManifold(&worldManifold); // this method calls b2WorldManifold::Initialize with the appropriate transforms and radii so you don't have to worry about that
        b2Vec2 worldNormal = worldManifold.normal; // this points from A to B
    
        if (playerF == contact->GetFixtureA()) {
    
            // note that +ve y-axis is "up" in Box2D but down in OpenGL and Cocos2D
            if (worldNormal.y < -0.707) { // use a constant for performance reasons
    
                // if the y component is less than -1/sqrt(2) (approximately -0.707),
                // then the normal points more downwards than across, so A must be hitting B 
                // from roughly above. You could tune this more towards the top by increasing 
                // towards -1 if you want but it worked fine for me like this last time and 
                // you might run into issues with missing hits
    
    
                NSLog(@"Player (A) hit B roughly on the top side!");
    
                // here you can set any class variables you want to check in 
                // your update()/tick(), such as flags for whether the player has died from
                // falling or whatever
        }
    
        } else if (playerF == contact->GetFixtureB()) {
    
            if (worldNormal.y > 0.707) {
    
                NSLog(@"Player (B) hit A roughly on the top side!");
            }
    
        } else {
        // it's something else hitting something else and we don't care about it
        }
    }
    

    As for doing it in your tick() method instead, yes, you can. I actually did all my stuff in PostSolve in the contact listener because I needed to know how hard the player hit, but all I cared about beyond that was whether the player had hit hard enough to kill them, so I didn’t need or want to iterate over all the contacts in my tick() – I just set a flag in the contact listener that said the player had suffered a fatal impact.

    If you want to do this all in the update method, then starting from what Ray has, add a b2Vec2 to the MyContact struct, and in BeginContact, add both the two fixtures (like Ray does) and get the collision normal (as I do) and add it too.

    The modified MyContact struct:

    struct MyContact {
        b2Fixture *fixtureA;
        b2Fixture *fixtureB;
        b2Vec2 normal;
        bool operator==(const MyContact& other) const
        {
            return (fixtureA == other.fixtureA) && (fixtureB == other.fixtureB);
        }
    };
    

    The new BeginContact method:

    void MyContactListener::BeginContact(b2Contact* contact) {
        b2WorldManifold wordManifold;
        contact->GetWorldManifold(&worldManifold);
    
        MyContact myContact = { contact->GetFixtureA(), contact->GetFixtureB(), worldManifold.normal };
        _contacts.push_back(myContact);
    }
    

    This will give you all the information you need to do the checking I initially described in your tick().


    Edit again:
    Your tick() method might contain something like this if you want to do the processing there, assuming you have called the player fixture (or ball fixture, like in the tutorial, or whatever it is you’re interested in) _playerFixture, that you’ve got a contact listener with the same name as in the tutorial, that you added the b2Vec2 normal to the MyContact struct, that you are adding contacts to the vector (as above) in BeginContact, and that you are deleting contacts from the vector in the EndContact (as shown in the tutorial – it’s probably fine as is):

    std::vector<MyContact>::iterator pos;
    for(pos = _contactListener->_contacts.begin(); pos != _contactListener->_contacts.end(); ++pos) {
        MyContact contact = *pos;
        if (_playerFixture == contact.fixtureA && contact.normal.y < -0.707) {
                NSLog(@"Player (A) hit B roughly on the top side!");
        } else if (_playerFixture == contact.fixtureB && contact.normal.y > 0.707) {
                NSLog(@"Player (B) hit A roughly on the top side!");
        } else {
        // it's something else hitting something else and we don't care about it
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am currently running into a problem where an element is coming back from
I have thousands of HTML files to process using Groovy/Java and I need to
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a jquery bug and I've been looking for hours now, I can't
That's pretty much it. I'm using Nokogiri to scrape a web page what has
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.