[solved] Issues with spheres, textures and physics nodes

Hi Guys, First post here so be gentle. I’ve just started playing with JME3 over the past couple of days, and am very impressed so far with how easy it makes things that I thought would be quite hard.



I’m attempting to create two spherical objects. One within the other. The outer sphere (approx 1/5 larger) will also have an associated physics node so that the player can’t pass through it.



I’ve managed to create both just fine and so both are visible nicely (inner solid, out translucent green), but when I add a physics node to the outer sphere, it behaves as I would want it (i.e. blocks path through) but the texture disappears completely from it.



Any ideas why this would be so?



Code for inner sphere:

[java]

Sphere test = new Sphere(32, 32, radius);

surface = new Geometry(“test2”,test);

surface.setLocalTranslation(position);

surface.setMaterial(createSurfaceMat());



// attach to node

main.getRootNode().attachChild(surface);

[/java]



Code for outer sphere:

[java]

Float limitRadius = (radius+(radius*0.2f));

Sphere test = new Sphere(32, 32, limitRadius);

limit = new Geometry(“test”,test);

limit.setLocalTranslation(position);

limit.setMaterial(createLimitMat());

limit.setQueueBucket(Bucket.Transparent);

main.getRootNode().attachChild(limit);



limitSphere = new SphereCollisionShape(limitRadius);

limitSpherePN = new PhysicsNode(limit, limitSphere, 0);

limitSpherePN.setKinematic(true);

main.bulletAppState.getPhysicsSpace().add(limitSpherePN);

[/java]



createSurfaceMat():

[java]

Material mat = new Material(main.getAssetManager(), “Common/MatDefs/Misc/SolidColor.j3md”);

mat.setColor(“m_Color”, ColorRGBA.Red);



return mat;

[/java]



createLimitMat():

[java]

Material mat = new Material(main.getAssetManager(), “Common/MatDefs/Misc/SolidColor.j3md”);

mat.setColor(“m_Color”, new ColorRGBA(0,1,0,0.3f));

mat.getAdditionalRenderState().setDepthTest(true);

mat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);

return mat;

[/java]

Try setting the material on the geometry and not on the physicsNode (line 11)

Normally setting the material on a node should set it n all contained geometries but try if it works this way

normen said:
Try setting the material on the geometry and not on the physicsNode (line 11)
Normally setting the material on a node should set it n all contained geometries but try if it works this way


Hi Normen,

I actually was (line 05 outer sphere). The line 11 was a typo when I copied/pasted to browser. Confirmed this in code and still no luck.

Hm, I cant see anything funny right now, I will have to check this. Sure its got nothing to do with the change to a Node (in the rest of your code)? One thing I see in your code that is bound to produce problems though, you should give your PhysicsNode a weight if you want to use it as a kinematic node. PhysicsNodes with weight 0 should not be moved at all. If you dont plan to move it theres no need to set it to kinematic, a zero weight will do.

normen said:
Hm, I cant see anything funny right now, I will have to check this. Sure its got nothing to do with the change to a Node (in the rest of your code)?


Nothing else changes it. I'll continue to debug and see if I can figure out what's going on. Like I said, I'm new to JME3 so I've probably done something stupid. :)

normen said:
One thing I see in your code that is bound to produce problems though, you should give your PhysicsNode a weight if you want to use it as a kinematic node. PhysicsNodes with weight 0 should not be moved at all. If you dont plan to move it theres no need to set it to kinematic, a zero weight will do


I see. My spheres represent a planet in my scene, I might eventually want it to move a little, but not interact with other physical objects apart from stopping them being able to fly through it. For this scenario would I be right in saying I would make it kinematic and change it's position in the update() loop whilst keeping it's mass 0?

For now I'll remove that line, as I'm still not sure if that's what I'll be doing.

Cheers for all your help!

Update:



I added a third sphere like this in createLimit():



[java]

// visible sphere

Sphere visi = new Sphere(32,32,limitRadius);

Geometry show = new Geometry(“show”,visi);

show.setLocalTranslation(position);

show.setMaterial(createLimitMat());

show.setQueueBucket(Bucket.Transparent);

main.getRootNode().attachChild(show);

[/java]



and this showed up fine, so I figured it was the association within the PhysicsNode constructor that was causing the issue, I modified my call to the PhysicsNode constructor to omit the inclusion of the child node (limit) and now it shows up just fine:



[java]

limitSphere = new SphereCollisionShape(limitRadius);

limitSpherePN = new PhysicsNode(limitSphere, 0); // overloaded constructor

[/java]



Looking in PhysicsNode, it attaches the child to itself:



[java]

/**

  • Creates a new PhysicsNode with the supplied child node or geometry and
  • uses the supplied collision shape for that PhysicsNode

  • @param child
  • @param shape

    /

    public PhysicsNode(Spatial child, CollisionShape shape, float mass) {

    if (child != null) {

    this.attachChild(child);

    }

    this.mass = mass;

    this.collisionShape = shape;

    rebuildRigidBody();

    }

    [/java]



    Presumably, this means it un-attaches the sphere from the rootNode ? (I’m not familiar with the internals). To test this I changed createLimit() to the following, so that sphere attaches to the rootNode after being used in the PhysicsNode constructor - it now behaves as expected with just the two spheres again. Both are now visible and the physics works as expected.



    [java]

    protected void createLimit() {

    Float limitRadius = (radius+(radius
    0.2f));

    Sphere test = new Sphere(32, 32, limitRadius);

    limit = new Geometry(“test”,test);

    limit.setLocalTranslation(position);

    limit.setMaterial(createLimitMat());

    limit.setQueueBucket(Bucket.Transparent);



    // physics

    limitSphere = new SphereCollisionShape(limitRadius+1f);

    limitSpherePN = new PhysicsNode(limit, limitSphere, 0);



    main.getRootNode().attachChild(limit);

    main.bulletAppState.getPhysicsSpace().add(limitSpherePN);

    }

    [/java]
  1. No, a kinematic node needs a mass, otherwise you get “impossible” values when the kinematic force etc. of the node is computed when its moved.
  2. Yes, a Spatial can only be attached to one Node and when its attached to another is detached from the old one.

Cheers Normen! :smiley: