Getting rising edge of a GhostObject and BetterCharacterControl

Hi Monkeys,

I’m currently facing a little problem concerning Physics. And ask for your help :smiley:.

Here is the situation :

I have one GhostObject and a BetterCharacterControl and I want to get a rising edge when the character enter the area and reset it when he leave it. So I implemented it so :

public class BoxTriggerListener extends GhostControl {

    private final TriggerType type;
    private boolean actionDone = false;

    public BoxTriggerListener(Node n) {
        super();
        BoundingBox box = (BoundingBox) n.getWorldBound();
        BoxCollisionShape b = (new BoxCollisionShape(new Vector3f(box.getXExtent(), box.getYExtent(), box.getZExtent())));
        this.setCollisionShape(b);        
    }

    @Override
    public void update(float tpf) {
        super.update(tpf);
                if (this.getOverlappingCount() > 0) {
                    if (!actionDone) {
                        System.out.println("Event_"+FastMath.nextRandomInt());
                    }
                    actionDone = true;
                } else if (this.getOverlappingCount() == 0) {
                    actionDone = false;
                }
    }
}

But more than half on the time 2 event are fired when the character enter the ghost area. And I can’t find the issue because in debug the behavior seems to be ok. (I cleaned up the code)

I just found a barbaric solution. I just realised that since in debug the problem doesn’t show up it’s because each frame is separated with each other. So… I just put some delay into the Ghost update loop like that :

private float lastCol = 0f;
private float threshold = 0.5f;

public void update(float tpf) {
    super.update(tpf);
    lastCol += tpf;
    if (lastCol > threshold) {
        if (this.getOverlappingCount() > 0) {
            if (!actionDone) {
                for (GhostAction action : actions) {
                    action.action();
                }
            }
            actionDone = true;
        } else if (this.getOverlappingCount() == 0) {
            actionDone = false;
        }
        lastCol = 0f;
    }
}

This idea came from tonegod in this Thread if people with the same problem show up in this Topic.

Link to the Topic