[SOLVED] How to detect the collision between two different object in different AppState

Hi,
I want to get the collision results between two different object in two different AppStat. But I’m no idea how let it work . Can anyone help me ?
Thanks you.

Please describe your problem a little more effectively. What is “object”. Is it a spatial? Does it have a physics rigidbody? Your question has so many different potential situations that would yield completely different responses.

1 Like

Thanks for your reply. :grinning:
The two different object both is geometry.

I’m holding a piece of red fuzzy string. Can you tell me how long it is?

Sorry, I don’t really understand what are you means. Can you explanation more clearly ?

It’s about asking good questions.

Right now, only you have the information you need to solve your problem. If you need our help then you will need to provide more information.

https://www.mikeash.com/getting_answers.html

1 Like

Thanks you very very much. That link you post is really useful :grinning: ,and I also realize I have many things need to learn even asking a question.

I am working at a space ship shooter game. I want to check the collision between the bullet and the enemy ship so that I can know when the bullet are touching the enemy space ship and let it explosion. And here are some code:

The code about bullet emission by player control ship:

Quad quad2 = new Quad(0.05f,0.08f);
fireBall = new Geometry(“FireBall”,quad2);
Material mat2 = new Material(assetManager,“Common/MatDefs/Misc/Unshaded.j3md”);
mat2.setColor(“Color”, ColorRGBA.Blue);
fireBall.setMaterial(mat2);

And this is the code of enemy space ship:

Quad quad = new Quad(0.5f,0.7f);
Texture tex = assetManager.loadTexture(“Textures/EnemySpaceShip/blueship4.png”);
Material mat = new Material(assetManager,“Common/MatDefs/Misc/Unshaded.j3md”);
mat.setTexture(“ColorMap”, tex);
geom = new Geometry(“EnemySpaceShip”,quad);
geom.setMaterial(mat);
geom.setModelBound(new BoundingBox());
rootNode.attachChild(geom);

I already add a boundingBox to the enemy ship, but the BoundginBox didn’t appear at the screen. So I can’t sure the code of adding BoundingBox working or not. I also try to use the BoundingVolume() method to detect the collision , but it seem cannot working between two different object in different AppState.

Does any another way to detect the collision correctly ?

It depends what you mean when you say AppState.

At first It sounded like you were talking about two different BulletAppStates from jmonkey’s built in physics

But if you are not using JME’s Bullet Physics and want to use a standard Collidable like a BoundingBox, Ray, or Spatial, then it would be useful to review these tutorials.
https://wiki.jmonkeyengine.org/jme3/beginner/hello_collision.html
https://wiki.jmonkeyengine.org/jme3/advanced/collision_and_intersection.html

If you prefer to use Jmonkey’s physics system for collisions, then you would benefit from these two tutorials

https://wiki.jmonkeyengine.org/jme3/beginner/hello_physics.html
https://wiki.jmonkeyengine.org/jme3/advanced/physics_listeners.html

I would personally choose to use a standard Collidable for detecting collisions with a bullet.

If your bullet is traveling very fast in a straight line (like a bullet in real life), then you could do a single Ray cast to detect collisions against other objects when the bullet is initially fired.

Otherwise, if you are detecting collisions with a slow moving projectile, you could do a collision check between two Collidables in the update loop every frame, such as a BoundingBox or a BoundingSphere against a Geometry - however you cannot collide two geometries against each other - if you need to have a more precise collision between two irregular shapes that cannot be accurately represented by a BoundingBox or BoundingSphere, then that is when you would want to use a PhysicsListener with a physics system running a BulletAppState.

Thanks you, but I can’t see it appear on the screen. Any way to let it appear at screen so that I can confirm it BoundingBox working or not.

You could use a WireFrameBox debug shape, and set the size of the debug shape to match the size of the BoundingBox. A boundingBox/Sphere or Ray is always invisible, so you instead create a debug shape to represent it

https://wiki.jmonkeyengine.org/jme3/advanced/debugging.html#wireframe-cube

public Geometry attachWireBox(Vector3f pos, float size, ColorRGBA color) {
    Geometry g = new Geometry("wireframe cube", new WireBox(size, size, size));
    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat.getAdditionalRenderState().setWireframe(true);
    mat.setColor("Color", color);
    g.setMaterial(mat);
    g.setLocalTranslation(pos);
    rootNode.attachChild(g);
    return g;
}

To get the x/y/z size of an existing spatial’s bounding box

   BoundingBox boundingBox = spatial.getWorldBound();
   float xSize = boundingBox.getXExtent();
   float ySize = boundingBox.getYExtent();    
   float zSize = boundingBox.getZExtent();

Or you can create a new blank bounding box that is not associated with the visual spatial, and just set the size and location to match the projectile

   BoundingBox boundingBox = new BoundingBox(locationVector, xSize, ySize, zSize);

Ya , thank you. But how do I get the variable in another AppState ? I set a variable like:

private Geometry spaceShip;

But I can’t use it in my another AppState. Does jmonkeyEngine had any method can get this variable ?

You can find an existing app state by calling
app.getStateManager().getState(YourAppState.class);

unless you have more than one instance of that appState attached, like multiple space ship appStates attached at once.

In which case, you should store a reference to your appStates when you create them, and then just access any important variables in those appStates by making getter/setter methods for the important variables such as your spaceShip geoemtry.

1 Like

But how can I store them , and how to access them ? Sorry disturb you for a long time because I just a beginner of JmonkeyEngine and I am no idea how to done it.

In the class where you create the new AppStates, you could also create an ArrayList and add the AppStates to the list, and then also create a getter method for the list of AppStates.

Oh ya , I forgot the ArrayList() method. But what is ‘getter’ ?

Most of JME’s test cases and tutorials are a single class, and allow you to learn a lot with java and JME, but once you begin to work on a larger multi-class project, it also becomes a lot more important to understand object oriented programming concepts.

You may be able to find out more doing a google search on your own, but this is also a decent article that should help you understand the concept of getter/setter methods and also shows examples

1 Like

I have one more question now: How to get the results of the BoundingBox if it touching with another BoundingBox ?

It is only possible to determine the collision’s contact point when one of the Collidables is a Ray.

If you have a collision between two BoundingBoxes using the .collideWith()method, the method will return a value of 0 if there is no collision, and a value of 1 if there is a collision (and will also fill the CollisionResults list with as much data as possible). You can also use the .intersects() method which will return true if there is an intersection.

For collisions between two boundingBoxes, the overlapping boxes will have multiple intersection points, so the CollisionResults list is not filled with data such as collision ContactPoints or ContactNormals. However you can estimate the point where two BoundingBoxes collide by using an extra Ray cast, or with the information you already have such as the center points of the bounding boxes, their x/y/z size, and the distance between the two boundingBoxes.

Hi, I tried add the variable to a ArrayList() and it didn’t work.But I realize I should use the public not the private before the word ArrayList() just now. So I can get the ArrayList() now. But I am no idea how to set the ArrayList() getter and I try to use System.out.println(spaceShipList), it show NullPointerException. I also try to use if(a.collectWith(spaceShip.spaceShipList)) , it also show a error.
I am really confusion now. :confused: