Collision Detection Problem(Solved)

Hello all. I’m not quite sure if my problem is due to my failed coding or what:



I’m trying to make it so that when the car model (with physics) hits a wall (a simple box with a material) it detaches from the rootNode and then runs the method initWall, which creates a bunch of smaller bricks (so it seems like, when you hit the wall, it breaks into bricks). But I can’t seem to be able to achieve that (by using the .collideWith method).



When I try running it, it says that whatever I use is unsupported (Geometry, bounding box, etc). I skimmed over the forums trying to find something on it. If there is one that I skipped over, I’m sorry for wasting your time, and the URL would be nice to have. Here’s a bit of my code (it’s always handy to have):



[java]public void simpleUpdate(float tpf) {

Ray r = new Ray(player.getPhysicsLocation(), Vector3f.UNIT_X);

CollisionResults res = new CollisionResults();

chasis.collideWith(origWall, res);

if(res.getCollision(1).getGeometry().getName() == “Wall”){

rootNode.detachChild(origWall);

initWall();

}



}[/java]





Thanks.





-NomNom

You have to do rootNode.collideWith() instead of the other way round.

I keep getting an Unsupported Exception… so wait, is there an example of using collision detection that when something hits something else something happens? (like a power up) Because that would be a very useful resource…



So, going back to the error, what can it handle? the origWall is a geometry… should I use a bounding box or something? And with the getPhysicsRotation(), why does it return a Matrix3f, and can I convert it into a Vector3f(for the car, to get it’s rotation… just wondering so I can create a better camera system)? One more thing: is there something like the Java2d Rectangle with the Intersects(Rectangle r) method, except bounding boxes?



And, just to get things straight, with the ray collision, that’s just sending out a ray and seeing what it hits, right? Again, is there a more efficient way to do things? Thanks normen!



-NomNom





Edit: I changed the code to:

[java]public void simpleUpdate(float tpf) {

Ray r = new Ray(player.getPhysicsLocation(), Vector3f.UNIT_X);

CollisionResults res = new CollisionResults();

rootNode.collideWith(r, res);

if(res.size() >= 1){

if(res.getCollision(1).getGeometry().getName() == “Wall”){

rootNode.detachChild(origWall);

rootNode.detachChild(w);

PhysicsSpace.getPhysicsSpace().remove(w);

initWall();

}

}

}[/java]



and it works… to a certain degree. It only works if I pass the X axis (i’m guessing because I set that as the direction… but I need to get it so that if any part of the car hits it it happens)… and then later on I get and IndexOutOfBoundsException… know what’s going on? Thx…

Yeah, use a BoundingVolume: rootNode.collideWith(BoxShape);

So wait, is rootNode.collidWith(BoxShape); supposed to work? Because it doesn’t use the CollisionResults res or the Ray with the car’s location… So how am I supposed to get/recieve the collison results? I’m very confused…



Again, is there a way to convert a Matrix3f to a Vector3f(so I might also be able to use a ray)…



Some better explanation would be nice… Again, if there are any examples out there I would be much appreciative… Thanks!

… All I want is for something to happen when the car hits the wall…

Then use physics collision listeners instead of doing these checks.

I can do that? Hmm… I didn’t know that that was there… Is there a tutorial with it? Or is this one of those things that I’ll have to just figure out? I didn’ think I saw it in the tutorials… Anyways, thanks! I’m sorry for all of my noobish questions… Just trying to figure 3d programming out…





-NomNom

Check out TestCollisionListener

Oooh! Looks good… Except that I can’t seem to get it to work…

[java]

// add ourselves as collision listener

getPhysicsSpace().addCollisionListener(this);

// add ourselves as group collision listener for group 2

getPhysicsSpace().addCollisionGroupListener(this, PhysicsCollisionObject.COLLISION_GROUP_02);[/java]



[java]Box wall = new Box(vt, 4, 4f, 0.25f);

origWall = new Geometry(“Wall”, wall);

origWall.setShadowMode(ShadowMode.CastAndReceive);

origWall.setMaterial(mat);

Wall = (BoundingBox) wall.getBound();



w = new PhysicsNode(origWall, new MeshCollisionShape(origWall.getMesh()), 0);

// tb.attachDebugShape(assetManager);

w.setCollisionGroup(PhysicsCollisionObject.COLLISION_GROUP_02);

rootNode.attachChild(w);

getPhysicsSpace().add(w);[/java]



[java]chasis = findGeom(carNode, “Car”);[/java]



[java]@Override

public void collision(PhysicsCollisionEvent event) {

if (“Car”.equals(event.getNodeA().getName()) || “Car”.equals(event.getNodeB().getName())) {

if (“Wall”.equals(event.getNodeA().getName()) || “Wall”.equals(event.getNodeB().getName())) {

rootNode.detachChild(origWall);

rootNode.detachChild(w);

PhysicsSpace.getPhysicsSpace().remove(w);

initWall();

}

}

}



@Override

public boolean collide(PhysicsCollisionObject nodeA,PhysicsCollisionObject nodeB) {

// TODO Auto-generated method stub

return true;

}[/java]





I can run into the wall (because I set collide as return true), but I can’t get the collision to pick up… I put the correct names of the spatials I have, but I’m not quite sure why it’s not working… Do the objects have to be physics nodes? Thanks again!

You just need to implement the PhysicsCollisionListener. Make sure the spatial you search is really the PhysicsNode.

I did implement it… And what do you mean by “search”? DO you mean the “Wall”.equals bit? Cause yeah, that’s a physics node… I guess it’s the car that I have a problem with, because it’s not a PhysicsNode, but a PhysicsVehicleControl… So, do I need to turn that into a PhysicsNode (is it just like a regular node that you can just add things to)? Thanks again!





-NomNom

YEAH! It now works… somewhat… I should’ve thought of this before, but I didn’t really want to still be able to hit the wall and bounce of… I just want to be able to hit the wall and keep going :/… How would I do that? If I set the collide() to return false, then the collision() doesn’t work… Thanks.

Note that the physics system has changed since alpha-3, physics is now a control. You can “simulate” a PhysicsNode by simply attaching a RigidBodyControl to a normal Node (thats what the now deprecated svn version of PhysicsNode does). So yeah, PhysicsNodes are normal Nodes.

Okay, thanks. So yeah, is there any way to make it so that my car doesn’t bounce off/stops when it hits the wall? I just want it to keep going.