Hy,
sorry I come back on the subject because i didn t found yet the way to do it
I would like for exemple to listen the collision of 3 spheres (a,b & c)
when the touch each other i would like to know, “a” touch “b”, or “b” touch “c” etc…
CollisionResult ?
Sorry for being slow on this subject !
Zoff
like how to know with this code, who’s touching who…
[java]package jme3test.helloworld;
import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Sphere;
public class ThreeBallsCollision extends SimpleApplication{
private Geometry a, b, c;
private int cpt = 0;
public static void main(String[] args){
ThreeBallsCollision app = new ThreeBallsCollision();
//app.setShowSettings(false);
app.start();
}
public void simpleInitApp() {
cam.setLocation(new Vector3f(0,-20,0));
cam.lookAt(Vector3f.ZERO, new Vector3f(0, 1,0));
Sphere sphereA = new Sphere(32, 32, 1);
a = new Geometry(“sphere A (blue)”, sphereA);
Material matA = new Material(assetManager, “Common/MatDefs/Misc/WireColor.j3md”);
matA.setColor(“m_Color”, ColorRGBA.Blue);
a.setMaterial(matA);
rootNode.attachChild(a);
Sphere sphereB = new Sphere(32, 32, 1);
b = new Geometry(“sphere B (red)”, sphereB);
Material matB = new Material(assetManager, “Common/MatDefs/Misc/WireColor.j3md”);
matB.setColor(“m_Color”, ColorRGBA.Red);
b.setMaterial(matB);
rootNode.attachChild(b);
Sphere sphereC = new Sphere(32, 32, 1);
c = new Geometry(“sphere C (green)”, sphereC);
Material matC = new Material(assetManager, “Common/MatDefs/Misc/WireColor.j3md”);
matC.setColor(“m_Color”, ColorRGBA.Green);
c.setMaterial(matC);
rootNode.attachChild©;
}
public void simpleUpdate(float tpf) {
a.setLocalTranslation(5FastMath.cos(cpt/1000.0f), 0, 0);
b.setLocalTranslation(0, 0, 5FastMath.cos(cpt/1000.0f));
c.setLocalTranslation(5FastMath.cos(cpt/500.0f), 0, 5FastMath.sin(cpt/500.0f));
rootNode.updateGeometricState();
cpt++;
}
}[/java]
I tried something… but i got an exeption
here is the code :
[java]package jme3test.helloworld;
import com.jme3.app.SimpleApplication;
import com.jme3.bounding.BoundingSphere;
import com.jme3.collision.CollisionResults;
import com.jme3.font.BitmapText;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Ray;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Sphere;
public class ThreeBallsCollision extends SimpleApplication{
private Spatial a, b, c;
private int cpt = 0;
public static void main(String[] args){
ThreeBallsCollision app = new ThreeBallsCollision();
//app.setShowSettings(false);
app.start();
}
public void simpleInitApp() {
cam.setLocation(new Vector3f(0,-20,0));
cam.lookAt(Vector3f.ZERO, new Vector3f(0, 1,0));
Sphere sphereA = new Sphere(32, 32, 1);
a = new Geometry(“sphere A (blue)”, sphereA);
Material matA = new Material(assetManager, “Common/MatDefs/Misc/WireColor.j3md”);
matA.setColor(“m_Color”, ColorRGBA.Blue);
a.setMaterial(matA);
//rootNode.attachChild(a);
Sphere sphereB = new Sphere(32, 32, 1);
b = new Geometry(“sphere B (red)”, sphereB);
Material matB = new Material(assetManager, “Common/MatDefs/Misc/WireColor.j3md”);
matB.setColor(“m_Color”, ColorRGBA.Red);
b.setMaterial(matB);
//rootNode.attachChild(b);
Sphere sphereC = new Sphere(32, 32, 1);
c = new Geometry(“sphere C (green)”, sphereC);
Material matC = new Material(assetManager, “Common/MatDefs/Misc/WireColor.j3md”);
matC.setColor(“m_Color”, ColorRGBA.Green);
c.setMaterial(matC);
//rootNode.attachChild©;
shootables = new Node(“Shootables”);
rootNode.attachChild(shootables);
shootables.attachChild(a);
shootables.attachChild(b);
shootables.attachChild©;
}
Node shootables;
public void simpleUpdate(float tpf) {
a.setLocalTranslation(5FastMath.cos(cpt/1000.0f), 0, 0);
b.setLocalTranslation(0, 0, 5FastMath.cos(cpt/1000.0f));
c.setLocalTranslation(5FastMath.cos(cpt/500.0f), 0, 5FastMath.sin(cpt/500.0f));
rootNode.updateGeometricState();
CollisionResults results = new CollisionResults();
a.collideWith(b, results);
a.collideWith(c, results);
b.collideWith(c, results);
if (results.size()!=0){
System.out.println("
Collisions? " + results.size() + "
");
}
cpt++;
}
}
[/java]
here is the console error
Oct 12, 2010 4:44:24 PM com.jme3.app.Application handleError
SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
com.jme3.collision.UnsupportedCollisionException
at com.jme3.collision.bih.BIHTree.collideWith(BIHTree.java:479)
at com.jme3.scene.Mesh.collideWith(Mesh.java:478)
at com.jme3.scene.Geometry.collideWith(Geometry.java:232)
at jme3test.helloworld.ThreeBallsCollision.simpleUpdate(ThreeBallsCollision.java:79)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:209)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:144)
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:133)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:198)
at java.lang.Thread.run(Thread.java:619)
The error in my file is notge at the line 79 but 69 (i removed some coments at the last post)
a.collideWith(b, results);
i posted an exemple of the same kind of test, but with physics http://hub.jmonkeyengine.org/groups/contribution-depot-jme3/forum/topic/exemple-of-collision-detection-with-ghost-wphysics/
I still do not know how to do without them
This example uses the class is in jme3, for I have seen several classes that can not be used anymore? I have such CollideWIth accesiblesolo the method. Long ago I’m trying to capture the collision of two cubes of type geometry but I fail, I get the same exception you mention.
The section of code is something like this:
private void createPlayer(){
Box b = new Box(new Vector3f(0f,0f,0f), 1, 1, 1);
player = new Geometry(“Player”, b);
Material mat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
//mat.setColor(“Color”, ColorRGBA.Blue);
mat.setTexture(“ColorMap”, assetManager.loadTexture(“Blender/2.4x/WoodCrate_lighter - Height Map.png”));
//mat.setTexture(“ColorMap”, assetManager.loadTexture(“Textures/ColoredTex/Monkey.png”));
player.setMaterial(mat);
rootNode.attachChild(player);
}
private void createBox(){
Box b1 = new Box(new Vector3f(1f,1f,0f), 1, 1, 1);
box = new Geometry(“Box”, b1);
Material mat1 = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
//mat.setColor(“Color”, ColorRGBA.Blue);
mat1.setTexture(“ColorMap”, assetManager.loadTexture(“Blender/2.4x/WoodCrate_lighter.png”));
box.setMaterial(mat1);
rootNode.attachChild(box);
}
CollisionResults cr=new CollisionResults();
player.collideWith(box, cr);
System.out.println(cr.size());
If it’s spheres you can simply check their distance to each other. If a two spheres are 1 meter in diameter each and their centers are closer than 1 meter then they are touching.