Default ShouldCollide Method in Box2d ProblemBlitzMax Modules Forums/Brucey's Modules/Default ShouldCollide Method in Box2d Problem
| Sorry - I seem to be needing a lot of help lately. What should the default ShouldCollide method be?. I want to implement my own Contact Filter. I wanted to use the standard(default) 'ShouldCollide' method, but add an extra condition to it - of my own. To start with I am just trying to replicate the existing method. I want to get this working before I add anything. I am using this (from the documentation)|
Local filter1:b2FilterData = shape1.GetFilterData() Local filter2:b2FilterData = shape2.GetFilterData() If filter1.GetGroupIndex() = filter2.GetGroupIndex() And filter1.GetGroupIndex() <> 0 Return filter1.GetGroupIndex() > 0 End If Local collide:Int = (filter1.GetMaskBits() & filter2.GetCategoryBits()) <> 0 And (filter1.GetCategoryBits() & filter2.GetMaskBits()) <> 0 Return collide
However I get an ''unhandled' memory exception error' I tried it with commenting bits out and the first thing that makes this happen is the filter2.GetGroupIndex(). The equivalent with filter1 works fine. Can someone help me with this? Thank you.
| I still have this problem. It is definitely a problem with Shape2 - I can't get this to work the TestBed examples either, so I don't think it is me doing something wrong. |
It doesn't crash doing this. (this is just an example to show the problem)
Method ShouldCollide:Int(shape1:b2Shape, shape2:b2Shape) Local bd:b2Body=shape1.GetBody() Return False EndMethod
but if i try to do anthing with Shape2 it doesn't like it
Method ShouldCollide:Int(shape1:b2Shape, shape2:b2Shape) Local bd:b2Body=shape1.GetBody() Local bd2:b2Body=shape2.GetBody() Return False EndMethod
I now get an unhandled memory exception.
Does anyone else get this?
| I added this to the collisionfiltering example :|
Type myfilter Extends b2ContactFilter Method ShouldCollide:Int(shape1:b2Shape, shape2:b2Shape) DebugLog "shape1 : " + (shape1 <> Null) + " : shape2 " + (shape2 <> Null) Local filter1:b2FilterData = shape1.GetFilterData() Local filter2:b2FilterData = shape2.GetFilterData() If filter1.GetGroupIndex() = filter2.GetGroupIndex() And filter1.GetGroupIndex() <> 0 Return filter1.GetGroupIndex() > 0 End If Local collide:Int = (filter1.GetMaskBits() & filter2.GetCategoryBits()) <> 0 And (filter1.GetCategoryBits() & filter2.GetMaskBits()) <> 0 Return collide ' Return True End Method End Type
with this line after the Init(20, 20)
and it gave me this output :
Executing:collisionfiltering.debug DebugLog:shape1 : 1 : shape2 1 DebugLog:shape1 : 1 : shape2 1 DebugLog:shape1 : 1 : shape2 1 DebugLog:shape1 : 1 : shape2 1 DebugLog:shape1 : 1 : shape2 1 DebugLog:shape1 : 1 : shape2 1 DebugLog:shape1 : 1 : shape2 1
and no crashing... on MacOS with the latest from SVN.
From what I can see looking through the source, shape2 should never be null. If it was, then you'd get an exception in the internal code.
One thing you might try is removing the .bmx folders from the module and doing a Build Modules. (or doing something like bmk makemods -a bah.box2d from the console), or Rebuild Modules (except that will do them all).
I'll try the same code on Win32 tomorrow morning.
| I have done exactly the same thing that you did Brucey with the CollisionFiltering example, and I got exactly the same results as you. |
I then copied exactly the same code into my program, and i got the same error as before. At least I know it must be something I'm doing now. I've no idea what (yet). The thing is - is that shape2 isn't coming up as NULL. I can do shape2.GetFilterData() without an error, but I can't do shape2.GetFilterData().GetGroupIndex() without an error for example. In fact I can't seem to do anything else on Shape2.
Thank you for your help Brucey. I will let you know what was wrong when I find out.
| Is the filter from Shape2 Null or not?|
If not, then there's perhaps some corruption somewhere - although you'd expect to see that problem in my posted code above also, if there was.
If you can put a very small example together, it might help track it down.
| Hi Brucey! |
I have found out what causes the problem. It is when shapes overlap - at least when they are first created. I got an 'unhandled memory exception' in the collisionFiltering example by setting the large box's position so that it overlapped the smaller box.
This error never occurs if you don't override the ShouldCollide Method, so somethings going wrong.
Also in the CollisionFiltering example I edited it so that if you press a key then a square box shape appears at the mouse cursor position. If you make this shape appear over an existing shape then the 'unhandled memory exception' error occurs. This is a problem because it seems like the shapes themselves don't have to be overlapping for this error to occur, they just need to be very close - so maybe its the bounding boxes overlapping that causes the problem?
Do you know if there is a way to use the ShouldCollide method so that things behave normally? The filterdata is coming up as not Null.
Thanks Brucey for the help - Rico.
| Brucey - Yes there is definitely a problem. Can you check to see if you also have the same problem. I wonder if is a problem with Box2D or the Blitz wrapper?|
Here is what I am having trouble with
1. If you create a new body and its shape is overlapping another body's shape then I get an unhandled memory exception' error. This only happens if you implement your own Contact Filter. If you use the defauly system shapes can happily overlap on creation
2. if you create a shape+body and it overlaps another objects AABB then the same error occurs. This means you can't create a shape next to a slope of a triangle for instance - even if the actual shapes aren't actually overlapping. Again this only happens when implementing a ContactFilter.
Please note these problem only seem to happen when the object is first created, not at any other time. I could see a solution would be to create an object offscreen and then manually place it at a seperate position. But this is not really satisfactory. Would you mind checking to see if you also have the above problems?
Thank you very much - Rico
| Hi Rico, this is a modified collisionfiltering example :|
Press SPACE to create a small box under the mouse location.
I can seemingly create them anywhere I like without any problems.
Does this work for you too, or do you get a crash somewhere?
Or, modify it appropriately and put the crashing version here :-)
| If I try to create a box on top of another box it crashes every time. |
| I've also tried my above example on Win32 also (originally tested on Mac), and it seems to work for me still. |
| Yes I'm afraid it crashes for me - in the same way I described before. Hmm, do you think its my version? I am using the most recent release version, not the most recent SVN one.|
I might try installing it again - I didn't before - because I got your previous example working, so I assumed that everything was ok.
| Strange. It might well be something in the latest version that is fixed in box2d.|
But I'll do a rebuild of the module and try it again, just in case it was a fluke for me ;-)
| I also am using the most recent release (non-SVN) version on a Mac. It could be this release build, as I assume you are using the latest SVN version Brucey. ;) |
| Okay. So we'll assume that the latest code is fine. I'm just adding some new types/methods to it, and then I'll hopefully have a new release out shortly. |
| Box2D 1.03 release is now available from the Downloads area.|
* Updated to box2d svn (rev 172)
* Added b2CircleShape and b2PolygonShape types.
* Added b2OBB type.
* Added b2Segment type.
* Added b2World Raycast(), RaycastOne() and InRange() methods.
* Added b2Body.GetWorld() method.
* Added raycast example.
Please let me know if that resolves your problem.
Note that you should perform a non-Quick compile of your application after building the new module to ensure your binary is up-to-date.
| Thanks Brucey - that looks good. I downloaded the new module, but I am having trouble building it. It no longer has .bmx code in it like before. I always used to go to the command prompt, set the 'bin' directory and type 'bmk makemods bah.box2d' and it would compile the files. What do I need to do now?|
I have never been able to use the Build Modules option on the IDE - because its always ghosted out.
| I just downloaded it and it looks fine from here. there is a box2d.bmx in the box2d.mod folder.|
That folder goes in place of the one in the BlitzMax/mod/bah.mod folder.
I expect that option is ghosted out because you don't have the MINGW environment variable set (to the folder where you have mingw).
| Thanks Brucey! I'm having fun messing around with Box2D; currently deciding whether to use it, Chipmunk, or Farseer for my actual project. :) |
| Yes. Its fine for me now. I have no idea how that happened! I downloaded it again and it was fine!|
I did set the MINGW environment variable in the control panel area somewhere (you can tell I am not very technical), when I first installed it. It is probable I set it wrong though - so I will check it out.
Thank you very much Brucey - for all the help - and the new version. I reallt needed Raycasts, for line-of-sight tests, so I am very happy! Thank you!