Sphere Cube Collision

Hello, I am trying to detect if a collision occurs between a sphere and a cube. If a collisions occurs, I want the sphere change direction, and keep it moving inside the cube.

I have the following code in my update loop:



CollisionResults results = new CollisionResults();

sphere.collideWith(cube, results);

if (results.size() > 0) {//collision occured

sphere_phyisic.getLinearVelocity().negate();//change direction of sphere

}



but when I run the program I get the following error:

Jun 21, 2012 7:43:27 PM com.jme3.scene.Node attachChild

INFO: Child (BitmapFont) attached to this node (null)

Jun 21, 2012 7:43:27 PM com.jme3.scene.Node attachChild

INFO: Child (null) attached to this node (Statistics View)

Jun 21, 2012 7:43:27 PM com.jme3.scene.Node attachChild

INFO: Child (Statistics View) attached to this node (Gui Node)

Jun 21, 2012 7:43:27 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:461)

at com.jme3.scene.Mesh.collideWith(Mesh.java:856)

at com.jme3.scene.Geometry.collideWith(Geometry.java:448)

at mySimulator.SphereSimulator.simpleUpdate(SphereSimulator.java:265)

at com.jme3.app.SimpleApplication.update(SimpleApplication.java:244)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:149)

at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:182)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:223)

at java.lang.Thread.run(Thread.java:662)

Jun 21, 2012 7:43:28 PM com.jme3.renderer.lwjgl.LwjglRenderer cleanup

INFO: Deleting objects and invalidating state

Jun 21, 2012 7:43:28 PM com.jme3.scene.Node detachChildAt

INFO: Gui Node (Node): Child removed.

Jun 21, 2012 7:43:28 PM com.jme3.scene.Node detachChildAt

INFO: Gui Node (Node): Child removed.

Jun 21, 2012 7:43:28 PM com.jme3.input.lwjgl.LwjglMouseInput destroy

INFO: Mouse destroyed.

Jun 21, 2012 7:43:28 PM com.jme3.input.lwjgl.LwjglKeyInput destroy

INFO: Keyboard destroyed.

Jun 21, 2012 7:43:28 PM com.jme3.system.lwjgl.LwjglAbstractDisplay deinitInThread

INFO: Display destroyed.

Java Result: -1073741819

BUILD SUCCESSFUL (total time: 20 seconds)



Can any body tell me what I am doing wrong.

Thanks in advanced!!!

http://hub.jmonkeyengine.org/groups/general-2/forum/topic/i-keep-getting-an-unsupportedcollisionexception-and-am-not-sure-why/

@normen said:
You can only collide geometry vs Bounding Volumes or Rays, like rootNode.collideWith(volume/ray).


Also [java]sphere_phyisic.getLinearVelocity().negate();[/java] wont do anything, you'd want
[java]sphere_phyisic.setLinearVelocity(sphere_phyisic.getLinearVelocity().negate());[/java]
2 Likes

Thank you so much for the response

I can detect collisions and respond to them when the objects collide with the walls of my cube. But how can

I make the objects move when they are at the edges or at the corners?

my code to respond to the collisions is something like this:



sphere.collideWith(rightWall.getModelBound(), results);

if(results.size() > 0){

sph_physics.setRestitution(1);

sph_physics.setLinearVelocity(sph_phy1.getLinearVelocity().negate());

}



Thanks !!!