Collision result List has more then it should

I have created a SpawnerControl that creates Nodes (Model)

I have created a DestroyerControl.
The way I want it to behave is that if anyting collides with it then that object will be destroyed. I thought the way to do this would be to use detachChild()

For some reason instead of just detaching just one object it detaches them all. Any Ideas?

Thanks
-Greg

[java]
package com.scriptblocks.flappymonkey.controller;

import com.jme3.app.SimpleApplication;
import com.jme3.bounding.BoundingVolume;
import com.jme3.collision.CollisionResults;
import com.jme3.export.InputCapsule;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.export.OutputCapsule;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
import com.jme3.scene.control.Control;
import java.io.IOException;

public class DestroyerControl extends AbstractControl {

SimpleApplication app;

@Override
protected void controlUpdate(float tpf) {
    CollisionResults results = new CollisionResults();
    BoundingVolume bv = spatial.getWorldBound();
    for (Spatial child : spatial.getParent().getChildren()) {
        if (!"Destroyer Box".equals(child.getName())) {
            child.collideWith(bv, results);
            if (results.size() > 0) {
                System.out.println("Collide " + child.getName() + " Class:" + child.getClass());
                spatial.getParent().detachChildNamed(child.getName()); 
            }
        }
    }
}

…
}
[/java]

[java]
public class SpawnerControl extends AbstractControl {

public float speed = .5f;
private float timeSenceLastCreation = 0;
public Node spawnTemplate;
public int blocksSpawned;



@Override
protected void controlUpdate(float tpf) {
    timeSenceLastCreation = timeSenceLastCreation + tpf;
    if (timeSenceLastCreation > speed && spawnTemplate!=null) {
        System.out.println("Create");
        spawnBlock(new Vector3f(0, 0, 0));
        timeSenceLastCreation = 0;
    }
}

protected Node getParentNode() {
    //assuming parent is roodNode
    return spatial.getParent();
}

…
public void spawnBlock(Vector3f loc) {
blocksSpawned++;
Spatial clone = spawnTemplate.clone();
if(clone instanceof Node){
Node block = (Node) spawnTemplate.clone();
block.setName("Block "+ blocksSpawned);
block.setLocalTranslation(loc);
block.addControl(new BlockControl());
getParentNode().attachChild(block);
} else if(clone instanceof Geometry){
Geometry block = (Geometry) spawnTemplate.clone();
block.setName("Block "+ blocksSpawned);
block.setLocalTranslation(loc);
block.addControl(new BlockControl());
getParentNode().attachChild(block);
}
}

public float getSpeed() {
    return speed;
}

public void setSpeed(float speed) {
    this.speed = speed;
}

public Node getSpawnTemplate() {
    return spawnTemplate;
}

public void setSpawnTemplate(Node spawnTemplate) {
    this.spawnTemplate = spawnTemplate;
}

[/java]

I little debugging on your part would go a looooong way.

Off the top of my head, possibilities:
-you are really removing many when you think you are only removing one (lots of results)
-or, you are removing a node that they all happen to be attached to.

Really can’t be too many other things. In either case, a couple printlns or stepping through in the debugger will tell you in just a few seconds.

One thing is for sure, detachChild() only detaches one child.

@pspeed
You are correct debugging is showing that every child is showing a collision and is getting detached. I just cant figure out why. As soon as the first spawned block hits Destory Block all the others show up as colliding with the Destroy Block.

I added a break; after the spatial.getParent().detachChildNamed(child.getName()); call , to break out of the for loop. That gets rid of my issue, but I still want to know why I was getting that result.

Thanks for your help as always. I will keep trying different test until I figure out why it behaves this way.

-Greg

You never clear your collision results.

Edit: which might have been clear if you’d looked at them in the debugger. I read code like Sherlock so I spotted it. :slight_smile:

1 Like

@pspeed. Damn your good. that was it. I was assuming the “child.collideWith(bv, results);” was creating a new results List when it was called. I should of know better since the result was always the same size.

Thanks again.

-Greg