[SOLVED] Collision Detection

Is there a simple way to check if a spatial is colliding with a CharacterControl?
Like if(spatial.isColliding(characterControl) == true)
do whatever i want to do;
Thanks :slight_smile:

Use a PhysicsCollisionListener


I did but it always had event.getNodA() equal to null and event.getNodB() equals to my scene whenever i touch it. i want event.getNodeA() or B() to be the player so i know that it is only the player that is touching it.

Is your character attached to a Node? and does it have a name?

Sorry for long response, my internet failed for a weird unknown reason.
[java]CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1);
player = new CharacterControl(capsuleShape, 0.05f);
player.setPhysicsLocation(new Vector3f(0, 50, 0));
My player is attached to a bulletAppState and my teleporterSpatial is attached to a node.

then getNode is going to return null for the character control as theres no Node.

If you don’t want to add a Node, you can also have a look at GetObjectA / B which returns a PhysicsCollisionObject

Ok different approach how do i make a Spatial check if he collides with anithing? like a characterController

for spatial - bounding volume, or ray/spatial collisions:


You can’t check collisions between a spatial and a physics collision shape

Then how do i check collision between collision shape and spatial?

I see there are some issues with the JME scene composer , so you have to have each .obj file separately in your Models folder & create each Spatial instance(object) separately in your code context , then attach them individually to the rootNode of your context or game,then you can check for the collision of each one separately…

sun = assetManager.loadModel(“Models/sphere.j3o”);
//declare physics for that spatial
RigidBodyControl rigidSun=new RigidBodyControl(CollisionShapeFactory.createMeshShape(sun),0f);
rigidSun.setPhysicsLocation(new Vector3f(10f,10f,10f));
//add that physiscs
sun.setLocalTranslation(new Vector3f(10f,10f,10f));
sun.setLocalScale(new Vector3f(10f,10f,10f));
//attach the spatial physics to the physics space

then check for it:

    public void collision(PhysicsCollisionEvent event) {     
                    fpsText.setText("You hit the sun ☀");

Hello @Pavl_G

No you dont need have each .obj separatly(and anyway .obj is jsut one of formats you can use, there are .GLTF, .FBX .ogre, .obj and converted .j3o), you can attach physics control to any Spatial in the scene Node.

If you will have j3o converted from your .obj in this case, you can already add some controls(via sdk or code) into it and re-save as j3o. You can also make it in code like you did, but no need load each model separately anyway.

This topic was about Different collision compare as i see.

because if someone use “isColliding” its not using same physics as PhysicsCollisionListener. your method is using proper physics that require PhysicsCollisionEvent, while isColliding no need it, but its not real bullet physics.(since it only check boundings)

If you mean SDK scene composer issues, then JME scene composer = JME SDK Scene composer and you should describe exactly what is problematic in SDK (please note some people dont use JME SDK here).

1 Like

Thanks :blush: for replying!

I have tried to add user data & names to each node in my scene that have many nodes,but right off the bat the physics collision event always detects the Scene Root which is the parent node right there …the scene composer doesnot detect spatials or nodes spearately(child nodes) but when I create j3o file for each spatial and add them to my scene rootNode programmatically it works :smiley:…yeh I know we can use blender to create scenes ,but if you know any another easier method to do collision between spatials let me know
Thank you

if it detects Root Node as getA() or getB() then it looks like you added rootNode as CollisionObject that is wrong.

simple example is you load “2 rabbits” as your scene, and you add them both as single CO. It would be fine if you do not want them collide each other, but you want, so its wrong.(in this case each rabbit should have own CO)

another example:
you have Character with body and head, you dont want make one CO for head and and CO for body, you want add one for both to just have character CO, then you add it for node that have both.

CO - collision object

1 Like

Oh so , I got it now ,so you mean that each spatial or node in my scene should have it’s own RigidBodyControl attached to the physicsSpace individually & actually I have attached the whole scene as one RigidBodyControl :sweat_smile::+1::+1:
Thanks in advance , I will post the code when I am done with it :grin:

    private void addRigidShape(String nodeName,float mass){
            //create a spatial from Mars Node from the main Root Scene (NB Mars Node is a child Node of Scene Root Node in my scene)
            Spatial shapeNode = rootNode.getChild(nodeName);
            //create a rigidbodycontrol for that particular Spatial instance
            RigidBodyControl NodeControls=new RigidBodyControl(CollisionShapeFactory.createMeshShape(shapeNode),mass);
            //add the physics to the mesh Spatial
            //add the mesh controls to the PhysicsSpace

eg. of calling it is addRigidShape(“Mars Node”,0.0f); in simpleInitApp();

Finally, it works i have made a simple method for simple customization .

NB: we can also use setRigidity(String NodeName); to create a Spatial from w/in the main scene & set its physics separately as a CO & getRigidity(int id); in a custom context(class) to retain the name of node using id for a collision listener.

CollisionListener Code

    public void collision(PhysicsCollisionEvent event) {
                    fpsText.setText("You hit the sun !");

Thanks @oxplay2

1 Like