I made a simple method for creating a ball at an x, y location with radius p. My problem is that I want to only create the ball if there is no ball already there. I know about PhysicsPicker, but I want to use PickResults. Anyway, when I don't set the model bounding information, my ball appears in the right location with the right size. However, if I try to put bounding, it doesn't appear at all. Maybe this is because of my frustum/camera settings? The code is pretty simple, I must be missing something…
public void makeBall(float x, float y, float p) {
// check to see if there is a ball already at x,y
PickResults pr = new BoundingPickResults();
Vector3f touchPoint1 = new Vector3f(x, y, -40);
Vector3f touchPoint2 = new Vector3f(x, y, 40);
Ray touchRay = new Ray(touchPoint1, touchPoint2.subtractLocal(
touchPoint1).normalizeLocal());
pr.clear();
rootNode.findPick(touchRay, pr);
// if a ball isn't already there, create one
System.out.println(pr.getNumber());
if (pr.getNumber() < 1) {
Sphere ballVisual = new Sphere( "ball", new Vector3f(), 63, 50, p/2000 );
// if I comment out these next two lines, my example ball appears
ballVisual.setModelBound( new BoundingSphere() );
ballVisual.updateModelBound();
DynamicPhysicsNode ball = getPhysicsSpace().createDynamicNode();
ball.setAffectedByGravity(false);
ball.attachChild( ballVisual );
ball.generatePhysicsGeometry();
ball.setLocalTranslation( new Vector3f( x, y, 0 ) );
ball.setMaterial(Material.RUBBER);
rootNode.attachChild( ball );
// add color stuff here
// update RenderState and ModelBound
rootNode.updateRenderState();
rootNode.updateModelBound();
}
}
protected void simpleUpdate() {
}
protected void simpleInitGame() {
display.setTitle("Sphere Physics Demo");
// Set the correct frustum and camera location
Camera camera = display.getRenderer().getCamera();
camera.setParallelProjection(true);
float aspect = (float) display.getWidth() / display.getHeight();
camera.setFrustum(-100, 100, 0, aspect, 0, 1);
this.makeBall(0.5f, 0.5f, 200);
}
Your code looks ok. Maybe the ball goes off due to some collision? Try setting pause = true and call updateGeometricState a single time after creation of the ball. Then have a look at your scene with physics view - does everything look ok, now? Does the ball go off if you switch pause off again? To sort out culling (frustum/camera problem) change the cull mode to prevent culling completely and check if that helps (if yes you still need to find the reason for it, though).
One general question (nonrelated to the debugging)…
rootNode.updateGeometricState( 0, true );
Should I be calling this every time I create a new sphere?
I tried all of the things that irrisor and basixs suggested -- the thing which helped me the most was turning physics mode on. My ball was definitely there (in physics mode), but not rendering the sphere. I went a little further and increased the size of my ball to ten times its original size and it appears with both the rendering and the physics grid. Here are some screenshots...
Normally sized (200/2000 = 0.1 unit radius)
Ten times sized (2000/2000 = 1 unit radius)
I fiddled a lot with the frustum until I realized that this was happening. Both screenshots were taken with the same code as I posted before -- no changes other than turning physics mode on and (for the second screenshot) changing the size of the sphere. Weeeiirrd. I must be missing something.
I fiddled with this some more and realized that the rendering appears only if the ball is bigger than the screen. Otherwise, it does not render.
This definitely has something to do with my frustum because if I do not fiddle with it, then my balls appear regardless of their size. Is there something wrong with these frustum settings?
Camera camera = display.getRenderer().getCamera();
camera.setParallelProjection(true);
float aspect = (float) display.getWidth() / display.getHeight();
camera.setFrustum(-100, 100, 0, aspect, 0, 1);
Sphere ballVisual = new Sphere( "ball", new Vector3f(), 63, 50, p/2000f );
// if I comment out these next two lines, my example ball appears
// ballVisual.setModelBound( new BoundingSphere() );
// ballVisual.updateModelBound();
final DynamicPhysicsNode ball = getPhysicsSpace().createDynamicNode();
ball.setAffectedByGravity(false);
ball.attachChild( ballVisual );
ball.generatePhysicsGeometry();
ball.setLocalTranslation( new Vector3f( x, y, 0 ) );
ball.setMaterial(Material.RUBBER);
ball.setModelBound( new BoundingSphere() );
ball.updateModelBound();
rootNode.attachChild( ball );
The bounding works -- my debug printf statement tells me that something is picked. However, my sphere does not render except when I use showPhysics = true.
It doesn't really change anything if I swap top and bottom other than my y-axis is flipped (for example, things that have y-coord 0.1f appear at the top of my screen at 0.9f, and vice versa).
I gave up trying to understand why things weren't appearing in my custom frustum (rhymes) and am normalizing the coordinate values that I receive before drawing.