Friendly Fire? Rigid Body Collision Question

I am looking through information on rigid body controls and collision listener and tick listener and I am confused about how to achieve a goal. The goal is to allow collisions to happen or not happen based on certain things.

Here’s an example:
I have a space ship, it has a shotgun on it.
The shotgun shoots a bunch of little balls, which for convenience sake I want to spawn in the same place.
I’d like to give them rigid body controls, but then it wont like it if I spawn them in the same place, and also I don’t want them accidentally hitting the ship on their way out of the gun.
The way I did this before using jmonkey was have a list of things the object is colliding with, with different possible things happening at the start of a collision and during and at the end of a collision (if for instance an object can pierce another object somehow)
I would like to create my bullets with a list including the ship and sub-objects, as well as the other bullets, and right before it decides a collsion is happening, I would like to say “Is this object I am colliding with one of these other objects? No collision then!”
Also I would be implementing tick listener to say “is the object now not in the bounds of one of the objects in the collision list? Okay remove the object from the list” so that the bullets could collide with eachother or the ship after the initial launch (if they got reflected or swung around by gravity etc).

So my question is, what is the best way to implement this?
I guess the collision group could have a “friendly” bucket, which the bullets get removed from after they leave the ship, but I’d rather be able to have a more detailed bunch of information available.

Colission groups is what you need:
https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:physics_listeners

You can set your own ship to group 1, enemies to group 2 and the bullets to group 3. Then, own ship setcollidewithgroups(2), enemies setcollidewithgroups(1 | 2 | 3) and bullets setcollidewithgroups (2). This should make the bullets collide with only the enemy, the enemies to collide with your ship, other enemies and your bullets and your bullets to collide with only the enemy (assuming you do not want the bullets to collide with themselves)

[EDIT] I just read you don’t want to have the bullets collide with each other indeed. Also, when using these commission groups, there is no need to keep track of a list. The collision(PhysicsCollisionEvent event) is only working for when two parts within the same commission group collide and in this function you define what to do after the collision is detected.

2 Likes
@husky said: Colission groups is what you need: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:physics_listeners

You can set your own ship to group 1, enemies to group 2 and the bullets to group 3. Then, own ship setcollidewithgroups(2), enemies setcollidewithgroups(1 | 2 | 3) and bullets setcollidewithgroups (2). This should make the bullets collide with only the enemy, the enemies to collide with your ship, other enemies and your bullets and your bullets to collide with only the enemy (assuming you do not want the bullets to collide with themselves)

[EDIT] I just read you don’t want to have the bullets collide with each other indeed. Also, when using these commission groups, there is no need to keep track of a list. The collision(PhysicsCollisionEvent event) is only working for when two parts within the same commission group collide and in this function you define what to do after the collision is detected.

Well, I do want the bullets to collide with eachother and the ship, just not when I spawn more than one in the same spot.

Is it possible to access the area where the collision group information is located, and if so what would I need to extend and modify to do so?

When I tried to look at the source for RigidBodyControl it said all this stuff about compiled source, which lead me to believe it wasn’t something I could access easily.

I have other segments of my code which would work well enough with collision groups, for instance I have material objects, immaterial objects (which collide with material but not eachother).

The other stuff I am trying to do is a bit too individualized for collision groups I think.

So can anyone tell me where these collision groups are checked?

The following is how I guess I would implement this using collision groups:
Start bullets in a non-collision collisiongroup
Tick listen to see if they are colliding with the ship or any of it’s parts
Once they aren’t, switch them to regular collision group

Something like that?

How about not using a rigidbody, but a ghostControl for the bullets instead? Problem is you can’t have overlapping physical objects. You can also try to apply the rigidbody later: Spawn the bullets and move them apart in the first few frames. After that, apply the rigidbodycontrol. I am not sure if this is possible, though.

Why exactly do you need the bullets to spawn at the same time and at the same location? What are you trying to achieve? Do you need a rigidbodycontrol for the bullets?

@yuxemporos have a look at this code of spaceship example:
http://code.google.com/p/jme-simple-examples/source/browse/#hg%2FJMESimpleExamples%2Fsrc%2Fcom%2Fspaceship

The ship shoots with bullets. Rigidbody was used.
Possibly it will help you.

You will see something like this:
[video]http://www.youtube.com/watch?v=uvfu5SLugG8&feature=player_embedded[/video]

The solution is simple…just don’t spawn them all from the same point…

After all that’s what guns in the real world do :wink:

@mifth
This does not seem to be his problem. I believe @yuxemporos wants to create a gun which shoots multiple bullets at once (in several different directions, maybe). When he spawns them, they all occupy the same space so there physics body’s are overlapping, making them fly all over the place.

If this is the case, I think he needs to consider spawning them in a slightly different spot in the direction they are going to travel.

It’s just as @zarch is saying, or changing the locations of spawning, or not using physic bodies.

@husky said: How about not using a rigidbody, but a ghostControl for the bullets instead? Problem is you can't have overlapping physical objects. You can also try to apply the rigidbody later: Spawn the bullets and move them apart in the first few frames. After that, apply the rigidbodycontrol. I am not sure if this is possible, though.

Why exactly do you need the bullets to spawn at the same time and at the same location? What are you trying to achieve? Do you need a rigidbodycontrol for the bullets?

@zarch said: The solution is simple...just don't spawn them all from the same point....

After all that’s what guns in the real world do :wink:

Well, in the real world, the pellets in a shotgun would all be shot inside of a tube and then bounce around off the sides and whatnot but still come out of the front going fairly straight, as this is a simulation and I don’t think jmonkey/jbullet has a hollow cylinder type… you get my point?

I want the bullets produced randomly for my shotgun shot but I guess I could spread them evenly over the spread angle and then randomly wiggle them somewhat but not enough that they would intersect after they leave the ship’s radius… Or just use a ghost radius until they are not hitting anything and then switch to rigid. I am just worried that two bullets might get in a situation where they are close together and stay uncolliding until they hit/move past the enemy.

Also the way my models work I don’t always want to spawn the bullets “just outside the end of the gun” or wherever they would be spawned traditionally. That’s why I want them to activate their collision after they leave the ship radius.

Yeah, don’t try to simulate the real world here. Although it is possible: create a hollow cylinder in blender and attach the correct collision shape (mesh or hull, I think). Most likely this will require some intense calculations from the physics engine as soon as you fire, though.