How I can change the trigger once in passing by a ghost control?

I have this code :

[java]public class PareGhost extends GhostControl implements PhysicsCollisionListener {

public PareGhost(BulletAppState bas, Vector3f dimensiones, VehiculoPlayer vehiculo) {
    super(new BoxCollisionShape(dimensiones));

}

@Override
public void collision(PhysicsCollisionEvent event) {       
    synchronized (event) {
        if ((event.getNodeA().getName().equals("NODO VEHICULO_PLAYER") || event.getNodeB().getName().equals("NODO VEHICULO_PLAYER"))) {
            System.out.println("SI|>>>>>>>>>>|");

        } else {
            System.out.println("NO|>>>>>|");
        }
    }
}[/java]

This show a :

  • When not colliding at ghostcontrol
    NO|>>>>>|
    NO|>>>>>|
    NO|>>>>>|
    NO|>>>>>|
    NO|>>>>>|
    NO|>>>>>|
    NO|>>>>>|
    NO|>>>>>|
    …
  • When this colliding to ghostcontrol

NO|>>>>>|
NO|>>>>>|
NO|>>>>>|
NO|>>>>>|
NO|>>>>>|
NO|>>>>>|
NO|>>>>>|
NO|>>>>>|
SI|>>>>>>>>>>|
SI|>>>>>>>>>>|
SI|>>>>>>>>>>|
SI|>>>>>>>>>>|
SI|>>>>>>>>>>|
SI|>>>>>>>>>>|
SI|>>>>>>>>>>|
SI|>>>>>>>>>>|
SI|>>>>>>>>>>|
SI|>>>>>>>>>>|
NO|>>>>>|
NO|>>>>>|
NO|>>>>>|
NO|
…
I understand that two threads are running and that something happens. (RenderThread and MainThread).
I want to go through the ghostcontrol only print just a “SI”.

Waiting for an early reply, thanks =D =D

@fdiaz said: I want to go through the ghostcontrol only print just a "SI".
Just remove line 15 in your code example, then it will only print "SI". You already posted the solution yourself.

haha @normen, that was very funny :smiley: . But his question was “just A Si” (i think that “si” means yes). so, the solution is to either remove this control when you detect the vehicule the first time (if you want to use it for only one vehicle) or use an “already triggered vehicle” collection and add the vehicle in it when you detect it the first time. For the second case, if you have a very fast “car cycle” (i.e. you create and delete a lot of car every second) you can also add a value in the userdata of the spatial, so you’ll not have a growing collection and a memory leak.

About nothing, you should either chose to write all your code in english or in your own native language, but a mix of it is kind of ugly. And you should use english if you want help.

I also, sometimes, put french in my code, but i try to remove it as much as i can.

you would want to use a weak hashmap if you wanted to do a collection like that. but i wouldnt go that route…

another route is to just do a check on whether it’s effect has already happened.

for instance if colliding in to the ghost control is what calls carControl.explode(); then inside of explode() do a check to make sure the car isnt already exploding when explode() is called (and if it is just dont process the rest of the method…) if carControl.explode() gets called 20 times in a row so what? a small if statement has no real bearing on anything.

You might want to implement that sort of check any way since you might have various things that could make your car explode for example, and if they both happen at the same time you dont want the car to explode twice.

Yeah I think he means that he only wants to deal with the collision event once, in which case it would be the easiest solution to calculate the time between collisions and deem, for example, a 200ms gap a new collision, else ignore it.

(@normen) I’m sorry, I do not make myself clear.

Specifically:
I have a car, to go through this ghostcontrol should increase points of a counter. (I put that “YES” and “NO” to simplify understanding).
Until now when the vehicle collides and remains there, the counter would go to infinity! The idea is that if you are within ghostcontrol add one point, when you leave there, and if colliding again add another point.

@bubuche consider what to write in only one language, preferably do it in English to explain it better and for cleaner code.

@icamefromspace I understand your idea, but what happens if instead, want to increase counter score.

The collision reports happen on the physics tick. So if you check if theres a collision each physics tick that means you still collide with the object. If a physics tick passes without a collision report the objects don’t collide anymore.

[java]
boolean contact = false;

public void collision(PhysicsCollisionEvent event) {
synchronized (event) {
if ((event.getNodeA().getName().equals(“NODO VEHICULO_PLAYER”) || event.getNodeB().getName().equals(“NODO VEHICULO_PLAYER”))
&& !contact) {
System.out.println(“SI|>>>>>>>>>>|”);
contact = true;
} else {
System.out.println(“NO|>>>>>|”);
contact = false;
}
}
}
[/java]

Or some such similar crap. Or am I missing something here? This actually only reports the collision once… but whatever.

Thanks to @t0neg0d by example. What I can not achieve is to change the contact to false, if the vehicle has already passed the ghostcontrol and this out of this.

Does collision(PhysicsCollisionEvent event) recent an update whether or not a collision happens? From the output of your original post, it seems as if it does. Or is it receiving updates from other objects colliding? That would weigh heavily on how I would proceed.

If it is, everything can be handled in the collision method.

Otherwise, you’ll have to use the suggestion above and track elapsed time since the last collision in an update loop. If it reaches whatever threshold you set up for it, then reset and wait for the next collision:

[java]
private float lastCol = 0f;
private float threshold = 0.5f;

public void update(float tpf) {
if (contact) {
lastCol += tpf;
if (lastCol >= threshold) {
// Do something
contact = false;
lastCol = 0f;
}
}
}
[/java]

The boolean contact is from the previous post.

@normen said: The collision reports happen on the physics tick. So if you check if theres a collision each physics tick that means you still collide with the object. If a physics tick passes without a collision report the objects don't collide anymore.
1 Like
@normen said:

Hey… cool! This will definitely be useful to know :wink:

@normen, Do you’re talking about PhysicsTickListener?
If so … it considers the forces, is it necessary? …

can you give me an example, please?