Sticky Objects

Still working through tutorials for JME and learning a lot.

I’ve been working with the HelloPhysics tutorial, and was wondering how I might modify it so that cannons stick to other bodies, either bricks, the floor, or each other, rather than bouncing off by default. I can’t seem to find out how this might be accomplished in the documentation.

Would I need to use a collision detector? Do I need to dynamically generate a joint between the objects upon collision? Simply changing the properties of the objects (such as friction, restitution, etc.) seems to adjust the amount they bounce off one another or slide on floors, but not whether they do at all.

Just set the physics control to kinematic when it hits something?

1 Like

And remember also to nullify its speeds (linear / angular). Otherwise, it will continue right through the wall… but not affected by forces anymore.

Nah, kinematic should freeze it right where it is, the forces will be dropped :slight_smile:

I did as normen suggested, added a PhysicsCollisionListener to listen for collisions, then set the RigidBodyController of the object to kinetic= true. The behavior of the first launched ball is the desired one, i.e. the ball flies through the air until it collides with another body, then fixes in place.

The problem is that that collision continues to be detected by the listener, so the toggle for kinetic= true stays on, making all subsequent balls kinetic when generated, which means they aren’t thrown, but stuck in place upon spawning, directly in front of the player.

Surely this comes up in cases where you want, for example, a sound to be played when a projectile hits a target. You only want the sound played once, not multiple times. Is there an example of this somewhere in the docs? If not, how do you just detect the first collision between two bodies?

Why do you apply collisions of some other ball to the new balls? Your code is flawed somewhere, you don’t separate the collision events.

Oops sorry. I forgot my first trials simply set mass to 0… Then I had to remove speeds… My bad.

@polycelf: If you want to remove collisions, I can see two ways :

  1. remove the physics control
  2. change its collision_group so that it does not interact with the wall anymore

However I can’t see why it would affect new balls ???

I’m modifying the Hello Physics tutorial like so:

I have HelloPhysics implement PhysicsCollisionListener. I register the listener with the following line:

[java] bulletAppState.getPhysicsSpace().addCollisionListener(this);[/java]

Then I add the collision method:

[java]
public void collision(PhysicsCollisionEvent event) {
if (event.getNodeA().getName().equals(“cannon ball”) || event.getNodeB().getName().equals(“cannon ball”)) {
ball_phy.setKinematic(true);
}
}
[/java]

why aren’t you using the controls which are part of the collision event? i.e inside your if statement, something like:

[java]
// very horrible long line
RigidBodyControl control = event.getNodeA().getName().equals(“cannon ball”) ? event.getNodeA().getControl (RigidBodyControl.class) : event.getNodeB().getControl(RigidBodyControl.class);
control.setKinematic (true);
// use suggestion above to change collisiongroup or other[/java]

If you want to reuse the same RigidBodyControl, you just have to detach it from your ball and next attach it to the next one. No need for kinematic then.

But that also means you can have only one (1) ball flying at any time… And you must enforce that.

The other way to go is to follow @wezrule directions, and create a new RigidBodyControl for each ball.

Edit : This is what is already done in the tutorial. Just follows @wezrule directions…