[SOLVED] Bug adding physics control to physics space

For some reason, my projectiles’ physics controls are not being contained in the physics space, even though I add them. I’ve checked the relating code 5 or 6 times, but I can’t find anything to even remotely cause something like that (but, yeah, it’s probably my code’s fault anyway :stuck_out_tongue_winking_eye:). Could someone please help me with this?

This code is directly responsible for adding the queued projectiles into the physics space, projectile node, and projectile list, as well as adding the projectile as a physics collision listener.

private void updateProjectiles(float tpf) {
	LinkedList<Projectile> buffer = plr.getGun().getProjectileBuffer();
	for (Projectile i : buffer) {
        System.out.println(i.getPhysicsControl()); // output: [email protected]
		bulletState.getPhysicsSpace().add(i.getPhysicsControl());
		bulletState.getPhysicsSpace().addCollisionListener(i);
		projectileNode.attachChild(i.getNode());
		i.getNode().setName(projectileIDs.generateID());
		projectiles.add(i);
	}
	buffer.clear();
	
    // ...
}

Everything but adding the control to the physics space works (except adding listener, not confirmed, doesn’t matter anyway). Since it doesn’t look like anything crazy is happening to the control itself, I’d assume something has happened to bulletState, but nothing looks wrong there either.

I have tried commenting out blocks of code that could be messing with it (like if the projectile removing code went rogue), but nothing changed at all.

I can provide more code if necessary.

Edit: It is probably worth mentioning that I am already adding a different control to the physics space successfully.

1 Like

How are you determining this?

1 Like

PhysicsSpace.add() won’t do much if the control isn’t enabled. Please test i.getPhysicsControl().isEnabled().

1 Like

Yes, the control is enabled.

1 Like

There is only one thing I can tell with certainty from your code: your control is 100% being added to the physics space.

Beware of this common pitfall… when you don’t know why something is wrong then it’s impossible to really know where something is wrong.

Probably your problem is “my physics control doesn’t move”. That’s not really clear from your post. There could be 100 reasons why that happens and you’ve picked the least likely one and posted about it.

It’s easy to confirm though:

System.out.println("space:" + i.getPhysicsControl().getPhysicsSpace());

Edit: note: the other thing I can tell with less certainty from your code is that you probably don’t know how collision listeners work. Every collision listener will be notified about every collision. Most apps will only have one or two.

1 Like

I think not moving is just a symptom. When debugging on for BulletAppState is turned on, the projectiles did not have the wireframe depicting physical presence while other physics object(s) did.
You are correct that the control is being added to the physics space (just checked that using the code you gave me).

Yes, I see what you mean. Admittedly, I haven’t worked with listeners very much… :grimacing:

A possible cause is that it doesn’t have a collision shape or some other setup issue.

We’d have to see the setup code to help you debug setup problems.

Edit: also, if you already have some controls working then what did you do differently this time?

This is the initialization code for all projectiles.

Projectile(AssetManager asset, Gun shooter) {
	this.asset = asset;
	this.shooter = shooter;
	node.setLocalTranslation(this.shooter.getNode().getWorldTranslation());
	node.addControl(control);
}
@Override
public void init() {
	// init physics
	control.setMass(0.5f);
	
	// init models
	Geometry g = new Geometry("bullet geometry", new Box(0.1f, 0.1f, 0.1f));
	Material m = new Material(asset, "Common/MatDefs/Light/Lighting.j3md");
	g.setMaterial(m);
	node.attachChild(g);
}

Where does “control” come from?

…since control is not setup correctly it is important to see how control is setup to see how to fix control.

control = bad. why control bad? Must see control to see why bad.

But you know, considering init() already sets things on control and adds the geometry… I once again wonder how the control ever gets a proper collision shape.

Until we see how control is created and initialized, we will never know.

1 Like

It’s been a while since I used physics in this way, so my apologies if I’m making a bad guess here. But doesn’t the order matter when it comes to setting up a physics control?

It looks strange that you set the physics control to the projectile’s node before attaching the geometry to that node

Unless you have some other code elsewhere that updates the collision shape afterwards, then that could explain why the projectile doesn’t appear to have a proper colision shape, since at the time of calling node.setControl() the node appears to have no children.

So as Paul said, it would be necessary to see more code where you set up the control and its collision shape to be sure, otherwise I’m just guessing

2 Likes

You and @pspeed are correct. I’ve been adding the control to the node before actually attaching a spatial. I switched it around and the problem is fixed.

2 Likes