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.
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 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…