Bug with GhostControl getOverlapping method?

Hi all. I have a Ghost Control named ghostEnemy attached to an enemy Oto spatial. In simpleUpdate, ghostEnemy.getOverlappingCount() gives a value of at least 2 each time, and ghostEnemy.getOverlappingObjects() corrects lists all objects each time. However, ghostEnemy.getOverlapping(0) which is supposed to return the first overlapping object does not work and I get the message IndexOutOfBoundsExption: Index 0, Size 0. Could there be a bug in this method or am I not using getOverlapping right? Any help is greatly appreciated!

Instead of posting the exact same post again (it didn’t work the first time, did it?), try making a test case (thats a self-contained application class that shows your issue and only contains code to do that).
http://www.mikeash.com/getting_answers.html

I put
public void simpleUpdate(float tpf)
{
System.out.println(ghostEnemy.getOverlappingCount());
System.out.println(ghostEnemy.getOverlappingObjects());
System.out.println(ghostEnemy.getOverlapping(0));
}

These 3 lines are all I have in simpleUpdate.

If I comment out the 3rd line, then I get 2 for the first method call which is correct and I get a CharacterControl and a RigidBodyControl for my second method call which is also correct. But when I execute the third method call, I get IndexOutOfBoundsException: Index 0, Size 0. This shouldn’t be because getOverlappingCount() already returns a value of 2, so Size should be 2 and not 0. I’m not sure why this happens!

I tried with the ghost control attached to my player spatial. Same problem. Anyone know why getOverlapping(int index) results in a Size of 0 when getOverlappingCount() results in 2 overlapping objects counted?

Make. A. Test. Case.

The test is simple enough. Only 3 lines in simpleUpdate.

System.out.println(ghostEnemy.getOverlappingCount());
System.out.println(ghostEnemy.getOverlappingObjects());
System.out.println(ghostEnemy.getOverlapping(0));

1st line: how many overlapping objects. 2 - check
2nd line: what are the overlapping objects. A characterControl and a rigidBodyControl - check
3rd line: return the overlapping object at index 0. IndexOutOfBoundsException: Index 0, Size 0. This is wrong since Size should be 2 and not 0.

Perhaps I’m not using ghostEnemy.getOverlapping(index i) correctly?

When I put these three lines in a simpleUpdate I get "Cannot find symbol: “ghostEnemy”. :roll:

I don’t think you can just call these methods like that. Wouldn’t there be race conditions since physics runs on another thread?

@Momoko_Fan said: I don’t think you can just call these methods like that. Wouldn’t there be race conditions since physics runs on another thread?
I completely can't say what he does as he doesn't post any code and refuses to do a test case, I am convinced if he'd put it in a test case the problem would magically disappear. And if not we could find out whats the issue but I use the GhostControl in MonkeyZone and the test etc. without problems. Anyway update() should normally be safe, also in parallel threading mode.

Here’s the code of my test.

[java]package jme3test.helloworld;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.bullet.control.GhostControl;

public class Test extends SimpleApplication
{
private Spatial avatarPlayer;
private BulletAppState bulletAppState;
private CharacterControl player;
private GhostControl ghostPlayer;

public static void main(String[] args)
{
    Test app = new Test();
    app.start();
}

public void simpleInitApp()
{
    /** Set up Physics */
    bulletAppState = new BulletAppState();
    stateManager.attach(bulletAppState);

    // We set up player
    CapsuleCollisionShape capsuleShapePlayer = new CapsuleCollisionShape(1f, 6f, 1);
    player = new CharacterControl(capsuleShapePlayer, 0.05f);
    player.setJumpSpeed(20);
    player.setFallSpeed(30);
    player.setGravity(30);

    // initialize the avatar of the player
    avatarPlayer = (Node) assetManager.loadModel("Models/Oto/Oto.mesh.xml");
    avatarPlayer.setLocalScale(0.5f);
    avatarPlayer.setLocalTranslation(new Vector3f(0f, 5f, 0f));
    avatarPlayer.addControl(player);
    rootNode.attachChild(avatarPlayer);
    
    // ghost for the avatar of the player
    ghostPlayer = new GhostControl(new CapsuleCollisionShape(1f, 6f, 1));
    avatarPlayer.addControl(ghostPlayer);
    bulletAppState.getPhysicsSpace().add(ghostPlayer);
    
    // attach the player to the rootNode and the physics space
    bulletAppState.getPhysicsSpace().add(player);
}

public void simpleUpdate(float tpf)
{    
    System.out.println(ghostPlayer.getOverlappingCount());
    System.out.println(ghostPlayer.getOverlappingObjects());
    //System.out.println(ghostPlayer.getOverlapping(0));
}

}[/java]

In simpleUpdate(float tpf), if I comment out the third line, the first line gives the correct result of 1 and the second line gives the corrects result of a CharacterControl. If I run the third line, I get indexOutOfBoundsException: Index 0 Size 0.

1 Like

So, the problem is you do this every frame, at some point the count is 0 and line 3 crashes, to avoid the crash in the test change simpleUpdate like this:
[java] public void simpleUpdate(float tpf)
{
int count = ghostPlayer.getOverlappingCount();
System.out.println(count);
System.out.println(ghostPlayer.getOverlappingObjects());
if(count>0)
System.out.println(ghostPlayer.getOverlapping(0));
}
[/java]

Was that so hard? Will you make a test case directly next time please? :slight_smile: Also please use the java code tags, I added them to your post now.

My test code below.

package jme3test.helloworld;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.bullet.control.GhostControl;

public class Test extends SimpleApplication
{
private Spatial avatarPlayer;
private BulletAppState bulletAppState;
private CharacterControl player;
private GhostControl ghostPlayer;

public static void main(String[] args)
{
    Test app = new Test();
    app.start();
}

public void simpleInitApp()
{
    /** Set up Physics */
    bulletAppState = new BulletAppState();
    stateManager.attach(bulletAppState);

    // We set up player
    CapsuleCollisionShape capsuleShapePlayer = new CapsuleCollisionShape(1f, 6f, 1);
    player = new CharacterControl(capsuleShapePlayer, 0.05f);
    player.setJumpSpeed(20);
    player.setFallSpeed(30);
    player.setGravity(30);

    // initialize the avatar of the player
    avatarPlayer = (Node) assetManager.loadModel("Models/Oto/Oto.mesh.xml");
    avatarPlayer.setLocalScale(0.5f);
    avatarPlayer.setLocalTranslation(new Vector3f(0f, 5f, 0f));
    avatarPlayer.addControl(player);
    rootNode.attachChild(avatarPlayer);
    
    // ghost for the avatar of the player
    ghostPlayer = new GhostControl(new CapsuleCollisionShape(1f, 6f, 1));
    avatarPlayer.addControl(ghostPlayer);
    bulletAppState.getPhysicsSpace().add(ghostPlayer);
    
    // attach the player to the rootNode and the physics space
    bulletAppState.getPhysicsSpace().add(player);
}

public void simpleUpdate(float tpf)
{    
    int count = ghostPlayer.getOverlappingCount();
    //System.out.println(ghostPlayer.getOverlappingObjects());
    
    if(count > 0)
    {
        System.out.println(ghostPlayer.getOverlapping(0));
    }
}

}

In simpleUpdate, if I have the uncomment the second line, I get the correct result from getOverlapping(0) which is a CharacterControl. Otherwise, I get IndexOutOfBoundsException: Index 0, Size 0. I don’t get it. Anyone can help me explain why? Many thanks!

You’re welcome. You have to call getOverlappingObjects at least once to fill the array with the links of the actual jME physics objects.

Oh my! Works now! Many thanks, normen! Couldn’t have done it without ya XD