Box2D Forums

It is currently Sat May 25, 2013 2:04 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Wed Apr 18, 2012 10:21 am 
Offline

Joined: Wed Aug 11, 2010 5:18 am
Posts: 10
I want to test for collisions between two shapes. I have a semi-working method, but it misses sometimes. This is what I do:

Code:
private static bool CollideA(Box2DX.Dynamics.Body b1, Box2DX.Dynamics.Body b2, bool testAll)
        {
            Box2DX.Collision.PolygonShape s1 = (Box2DX.Collision.PolygonShape)b1.GetShapeList();
            Box2DX.Collision.PolygonShape s2 = (Box2DX.Collision.PolygonShape)b2.GetShapeList();

            Box2DX.Common.XForm playerXForm = b1.GetXForm();
            Box2DX.Common.XForm oForm = b2.GetXForm();

            while (s1 != null)
            {
                s2 = (Box2DX.Collision.PolygonShape)b2.GetShapeList();
               
                while (s2 != null)
                {
                    Box2DX.Common.Vec2[] vertices = s2.GetVertices();

                    foreach (Box2DX.Common.Vec2 v in vertices)
                    {
                        if (s1.TestPoint(playerXForm, b2.GetWorldPoint(v)))
                            return true;
                    }

                    s2 = (Box2DX.Collision.PolygonShape)((Box2DX.Collision.PolygonShape)s2).GetNext();
                }
                s1 = (Box2DX.Collision.PolygonShape)((Box2DX.Collision.PolygonShape)s1).GetNext();
            }

            return false;
        }


I think the problem is that I do s1.TestPoint( ). The points might not be inside the rectangle, but there might still be collision. Is there a ready solution provided by the library? It seems too complex to use. I know that this project has been abandoned, but I need a solution because I am making a project and I can't change the physics library now.

Thank you.


Top
 Profile  
 
PostPosted: Wed Apr 18, 2012 12:02 pm 
Offline

Joined: Sun Oct 25, 2009 3:28 am
Posts: 242
There is a b2ContactListener you can use to extend from (override the needed functions), then do world.SetContactListener(customContactListener). A collision happens when BeginContact is called.


Top
 Profile  
 
PostPosted: Thu Apr 19, 2012 11:37 am 
Offline

Joined: Wed Aug 11, 2010 5:18 am
Posts: 10
Great, so I went to the source code of Box2DX, found the "ContactListener" class. It's an interface actually.

These comments lie above the interface's definition:
/// Implement this class to get contact information. You can use these results for
/// things like sounds and game logic. You can also get contact results by
/// traversing the contact lists after the time step
. However, you might miss
/// some contacts because continuous physics leads to sub-stepping.
/// Additionally you may receive multiple callbacks for the same contact in a
/// single time step.
/// You should strive to make your callbacks efficient because there may be
/// many callbacks per time step.
/// @warning You cannot create/destroy Box2DX entities inside these callbacks.

I thought: "Great, I can use that list to avoid using this". So I tried doing "world." for the auto completion to pop up. Nothing about GetContacts(), GetContactLists(), etc. Only GetContactCount() - useless for what I want to do. I checked the source code, there's a line in the World class:

// Do not access
internal Contact _contactList;

:?

So I tried to inherit. There were 4 methods to implement:
Code:
void BeginContact(Contact contact);
void EndContact(Contact contact);
void PreSolve(Contact contact, Manifold oldManifold);
void PostSolve(Contact contact, ContactImpulse impulse);


So I wrote it like this:
Code:
/// Called when two fixtures begin to touch.
        public void BeginContact(Contact contact)
        {
            Console.WriteLine("contact");
        }

        public void EndContact(Contact contact)
        {
        }

        public void PreSolve(Contact contact, Manifold oldManifold)
        {
        }

        public void PostSolve(Contact contact, ContactImpulse impulse)
        {
        }


Guess what? Compiler error!!!
Error 8 The type or namespace name 'ContactImpulse' could not be found (are you missing a using directive or an assembly reference?) C:\Users\Minas\Documents\Visual Studio 2010\Quest of Antheia\Quest of Antheia\Quest of Antheia\src\MyContactListener.cs 33

I was not missing any using directive. I don't know about the assembly reference (what is it anyway?).

I deleted the PostSolve() method, and did world.setContactListener(new MyContactListener());
That should print "contact" every time there's a contact (I have the console terminal enabled).
Nothing is printed though...

What is wrong here?


Top
 Profile  
 
PostPosted: Thu Apr 19, 2012 3:19 pm 
Offline

Joined: Sun Oct 25, 2009 3:28 am
Posts: 242
Sorry if you've already done this, but did you try to get the contactListener in a variable (preferably a member variable so it doesn't disappear when it goes out of scope)? Just trying to see what the problem is.

If that doesn't work, can you try doing a print-to-console thing to the other functions (EndContact, PreSolve) to see if anything works?

Also, you can get the contact list by doing contact.GetNext(), which returns the next contact on the list (mind you, the list is the entire contact list of the world).

If you need that PostSolve, you will need to include Box2D.Dynamics.ContactImpulse (by using a using directive) if you haven't already.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group