Collision box vs box

If I have 2 boxes, how do I detect when they collide each other ?

ive read the tutorials but i cant figure it out :frowning:

Not sure how much more information is needed the tutorial comments the code very well



http://www.hub.jmonkeyengine.org/wiki/doku.php/jme3:beginner:hello_collision

That tutorial explain how to make a collidable(player capsule) to react(stopdon`t pass through) when hit other collidable(level scene), but doesn t explain how to detect the moment when a collidable(a box) intersect other collidable(other box).

Then you would want this tutorial here:



http://www.hub.jmonkeyengine.org/wiki/doku.php/jme3:beginner:hello_physics



Have you read it, if so what part don’t you understand?

I fully understand the tutorial, but, again is not what I need…this one explain how spatials react to physics forces, but don`t tell you how to detect the moment when a spatial(a cube geometry) intersect other spatial. For example, when a rocket intersect a ship, then the ship I must apply something like this ship.hp-=rocket.damage;

Thats hello picking.

Well, I`ve try with the ray method but is not cost effective. For example I need to detect when the player is in the front of an enemy, so to make the enemy to fire only if it have a target in the front of him and not everytime.

Well, I`ve try with the ray method but is not cost effective. For example I need to detect when the player is in the front of an enemy, so to make the enemy to fire only if it have a target in the front of him and not everytime.

A CollisionShape is a Collidable as well.

This page would help you:

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:physics_listeners#physics_collision_listener

Where u get 2 boxes?

After Ive follow this example(with do what I want) <a href='http://code.google.com/p/jmonkeyengine/source/browse/trunk/engine/src/test/jme3test/bullet/TestCollisionListener.java'>http://code.google.com/p/jmonkeyengine/source/browse/trunk/engine/src/test/jme3test/bullet/TestCollisionListener.java</a><br /> I tried to do my own but I dont know what is wrong with my code



[java]

import com.jme3.app.SimpleApplication;

import com.jme3.bullet.BulletAppState;

import com.jme3.bullet.collision.PhysicsCollisionEvent;

import com.jme3.bullet.collision.PhysicsCollisionListener;

import com.jme3.bullet.collision.shapes.CollisionShape;

import com.jme3.bullet.control.RigidBodyControl;

import com.jme3.bullet.util.CollisionShapeFactory;

import com.jme3.input.KeyInput;

import com.jme3.input.controls.AnalogListener;

import com.jme3.input.controls.KeyTrigger;

import com.jme3.light.AmbientLight;

import com.jme3.light.DirectionalLight;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Vector3f;

import com.jme3.scene.Geometry;

import com.jme3.scene.Node;

import com.jme3.scene.shape.Box;





public class TestCollision extends SimpleApplication implements AnalogListener,PhysicsCollisionListener

{

private Node cube1, cube2;

static BulletAppState bulletAppState;

private RigidBodyControl controlCube1, controlCube2;



public static void main(String args[])

{

TestCollision testCollision=new TestCollision();

testCollision.start();

}

@Override

public void simpleInitApp()

{

AmbientLight al=new AmbientLight();

al.setColor(ColorRGBA.White);

rootNode.addLight(al);

DirectionalLight dl=new DirectionalLight();

dl.setDirection(new Vector3f(0, -1, -2));

flyCam.setMoveSpeed(10);

Geometry cube1=makeBox(new Vector3f(1, 0, 1));

Geometry cube2=makeBox(new Vector3f(-5, 0, 1));

this.cube1=new Node(“cube1”);

this.cube2=new Node(“cube2”);

this.cube1.attachChild(cube1);

this.cube2.attachChild(cube2);

rootNode.attachChild(this.cube1);

rootNode.attachChild(this.cube2);



bulletAppState=new BulletAppState();

stateManager.attach(bulletAppState);

bulletAppState.getPhysicsSpace().enableDebug(assetManager);

bulletAppState.getPhysicsSpace().addCollisionListener(this);

initControl(controlCube1, this.cube1);

initControl(controlCube2, this.cube2);

initKeys();

}

private Geometry makeBox(Vector3f loc)

{

Box box=new Box(loc, 0.5f, 0.5f, 0.5f);

Geometry cube=new Geometry(“Cube”, box);

Material matLightning=new Material(assetManager, “Common/MatDefs/Light/Lighting.j3md”);

cube.setMaterial(matLightning);

return cube;

}

private void initKeys()

{

inputManager.addMapping(“left”, new KeyTrigger(KeyInput.KEY_NUMPAD4));

inputManager.addMapping(“right”, new KeyTrigger(KeyInput.KEY_NUMPAD6));

inputManager.addMapping(“forward”, new KeyTrigger(KeyInput.KEY_NUMPAD8));

inputManager.addMapping(“back”, new KeyTrigger(KeyInput.KEY_NUMPAD5));



inputManager.addListener(this, new String[]{“left”,“right”,“forward”,“back”});

}

@Override

public void onAnalog(String name, float value, float tpf)

{

final float SPEED=2;

if(name.equals(“left”))

{

cube1.move(-SPEEDtpf, 0, 0);

}

if(name.equals(“right”))

{

cube1.move(SPEED
tpf, 0, 0);

}

if(name.equals(“forward”))

{

cube1.move(0, 0, -SPEEDtpf);

}

if(name.equals(“back”))

{

cube1.move(0, 0, SPEED
tpf);

}

}

@Override

public void collision(PhysicsCollisionEvent e)

{

System.out.println(“cube 1 intersect cube 2”);

}

private void initControl(RigidBodyControl control, Node cube)

{

CollisionShape collisionShape=CollisionShapeFactory.createDynamicMeshShape((Node)cube);

control=new RigidBodyControl(collisionShape, 0);

cube.addControl(control);

bulletAppState.getPhysicsSpace().add(control);

}

}

[/java]

I have test file like this on my pc but I’m not currenty at home. I remember that i used GhostControl instead of RigidBodyControl.

you keep going back and forth between physics and the geometry collision system, what do you want to do? Just do rootNode.collideWith(myBoundingBox).

Not sure why you use dynamicmeshshape, use a box collisionshape, also set it to kinematic if u dont move them with forces :P. Or if u don’t want physics listen to normen :slight_smile:

I think he wants to have collision listener. He could put collideWith in update loop, but wouldnt it be too costly?

I’ll post my test case as soon as i get home.

@ivokosir said:
He could put collideWith in update loop, but wouldnt it be too costly?

No, continuous raycasts for example are the most basic way to have a character follow the ground.
@normen said:
No, continuous raycasts for example are the most basic way to have a character follow the ground.

Interesting, is that the way simple fighting games with hitboxes work (e.g. head is one hitbox, than arm, body)?
@ivokosir said:
Interesting, is that the way simple fighting games with hitboxes work (e.g. head is one hitbox, than arm, body)?

Well to get the information you have to compute it, it doesn't come from thin air ^^ Sure that has to be evaluated in some way each frame. Physics engines also mostly consist of collision checking algorithms.