Box2D rocket launcher woes

BlitzMax Forums/Brucey's Modules/Box2D rocket launcher woes

Samichan(Posted 2009) [#1]
I'm trying to make a player shoot a rocket and have the rocket not collide with the shooting player.

The rocket's b2Body starts inside the player's b2Body and that causes problems if they collide.


Instead of using Box2D's own collision filter systems with the groups n categories and all, I made a simple one like this:

Type cm2physfilter Extends b2ContactFilter
	Method ShouldCollide:Int(shape1:b2Shape, shape2:b2Shape)
		...
		if one shape is rocket and other shape is player then return false
                ...
		Return True
	End Method
End Type


This one ignores all other collisions as it should, but creates one collision as the rocket is created.

How can I get rid of that one collision when the rocket is created?


I tried to use the b2World.ReFilter() function but it didn't help.


Pix!
Failure rocket
Proper rocket

Download alpha version + source here http://www.mediafire.com/download.php?lmnmomwzzwh (You'll need various modules to compile that)


ps. this forum needs a preview button


Armitage 1982(Posted 2009) [#2]
You should consider the collision filter system of Box2d.
It's faster and help you to delimit every specific object on different "layer".
It's also easier to deal with the collisionPoint that way.

But if you really want to stick with your idea why not trying to disable the filterData MaskBits ($0000:Short) when created then set it to a full mask ($FFFF:Short) when needed ?

Look at this in the documentation :
B2Shape.GetFilterData().SetCategoryBits()
B2Shape.GetFilterData().SetGroupIndex()
B2Shape.GetFilterData().SetMaskBits()

HTH


Brucey(Posted 2009) [#3]
Hi :-)

Looking like a nice little game! The effects are great too!

I have the collision stuff working now :-)

It requires some "tweaks" to your code, and for you to start using the "userData" storage in the bodies. As a side-effect this also improves any tests you need to make, since you no longer have to iterate your lists.

Your ShouldCollide() method can be changed to this :

		If THumanoid(shape1.getbody().GetUserData()) Then
			If cm2Rocket(shape2.getbody().GetUserData()) Then
				Return False
			End If
		Else If THumanoid(shape2.getbody().GetUserData()) Then
			If cm2Rocket(shape1.getbody().GetUserData()) Then
				Return False
			End If
		End If

		Return True

No loops :-)

In order to use this, we need to make some changes elsewhere.
I'm only posting changes rather than the full source, because it's easier for you to see what I've done.

You need to add a new parameter to LoadBodyFromMemory() - data:Object
In that method, you should call this :
		bd.SetUserData(data)

before you call CreateBody().

There are two calls to it :
In PhysDebree.Create(), pass in "debree" as the last parameter to LoadBodyFromMemory().
In cm2Rocket.Create(), pass in "rocket" as the last parameter to LoadBodyFromMemory().

And finally, in Newb2dPlayerBody(), call
		bd.setUserData(player)

somewhere before the call to CreateBody().

You may also want to apply the same to cm2Bullet, by setting the userdata with the parent object.


Let me know how you get on with that :-)


Armitage 1982(Posted 2009) [#4]
That's a nice solution !
I'm too used to no longer take care of that ShouldCollide function thanks to bitMaks.
But that's true, when you use them you need a predetermined list of BYTE and MASK which is sometimes hard to understand I must admit.
Easy with 2 objects, but harder while the list grow.

Then add sensor and soon the new Contact solver system and you are lost :D


Samichan(Posted 2009) [#5]
Thank you Brucey and Armitage for your solution and comments.

I applied Brucey's tweaks and it now works perfectly.
Looks like the core of the problem was that all collisions defaulted to true, in Brucey's code and my modified version
all collisions involving rockets default to false and that solves the initial problem.

Here's what the physfilter code looks like now:


Rockets won't collide with the shooter before the suicidesafe time is up but do collide with everything else all the time.



ps. sorry for making you read my messy code, it's my first OOP project/game and the conversion
to Box2D physics is only halfway done so parts of it might looks sliiightly weird.


Pete Rigz(Posted 2009) [#6]
Cool, nice to know for sure then that timelinefx plays nice with box2d :)

And you can do rocket jumps, nice :)