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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:26:06+00:00 2026-05-20T01:26:06+00:00

I have a basic class for detecting collisions but I can’t figure out how

  • 0

I have a basic class for detecting collisions but I can’t figure out how to see what bodies are colliding to trigger the appropriate event. Lets say I have a pong game and in it a ballBody and topwallBody. How would I figure out if these are colliding or not. Here is the class I’m using just to give you an idea.

class MyListener : public b2ContactListener
{
    void BeginContact(b2Contact* contact)
    {
        b2Fixture* fixtureA = contact->GetFixtureA();
        b2Fixture* fixtureB = contact->GetFixtureB();
        b2Body* body1 = fixtureA->GetBody();
        b2Body* body2 = fixtureB->GetBody();
        cout << "started";
    }
    void EndContact(b2Contact* contact)
    {
        cout << "ended\n";
    }
};
MyListener listener;
world.SetContactListener(&listener);

It looks like I can get the bodies in the pointers but I have no idea how to compare them to other bodies.

  • 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-20T01:26:06+00:00Added an answer on May 20, 2026 at 1:26 am

    When you create the bodies, set the userdata to something meaningful, but be consistent 🙂 A good tip is to always have the same kind and type of data in there, an entity id or reference to an actor.

    Straight from the documentation:

    b2BodyDef bodyDef;
    
    bodyDef.userData = &myActor;
    

    So if you went this road you would get the actor from the b2Body and inform it of the collision, or do something else.

    Also from the docs:

    b2Fixture* fixtureA = myContact->GetFixtureA();
    
    b2Body* bodyA = fixtureA->GetBody();
    
    MyActor* actorA = (MyActor*)bodyA->GetUserData();
    

    In the code above you would have the actor/entity and could do whatever you would like… actorA.explode().

    Being consistent will likely save you from insanity. If one sticks all kinds of data in there it’ll become really hard knowing what data is in what body. Plus you can’t really handle the contacts in any generic way.

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

Sidebar

Related Questions

i have a basic ruby class: class LogEntry end and what i would like
Currently, i have basic C++ and PHP skills. But, i want to switch to
I have a basic understanding of mock and fake objects, but I'm not sure
I have a basic cs-major understanding of multi-threading but have never had to do
I have a basic class 'HistoryItem' like so: public class HistoryItem private Date startDate;
In my project I have implemented basic class CVector. This class contains float* pointer
Let say I have a basic 2D vector class something like class vector2 {
I have this property in a visual basic class. NET 2008, the property in
I have a class called AString . It is pretty basic: class AString {
What are containers/adapters? I have basic knowledge of C++ and its sub-topics like (class/templates/STL).

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.