Hello,
I adapted the HelloPhysics demo (drastically), but none of my objects are visible, and I’m at a loss for why. I added a sky to see if i could get anything to show up, and i can see the sky fine. nstead of trying to explain what I did and didn’t do, showing my code would be faster for you i think. I really appreciate it if someone has time to glance it over and see if they can see the problem.
Main.java
[java]
package mygame;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.nodes.PhysicsNode;
import com.jme3.font.BitmapText;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.material.Material;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.queue.RenderQueue.ShadowMode;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.Control;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Sphere;
import com.jme3.shadow.BasicShadowRenderer;
import com.jme3.texture.Texture;
import com.jme3.util.SkyFactory;
import mygame.control.ControlFactory;
import mygame.control.RigidBodyControlFactory;
import mygame.object.Ball;
import mygame.object.Player;
public class Main extends SimpleApplication {
private Sphere sphere;
private Player[] players;
private Ball ball;
public static void main(String args[]) {
Main app = new Main();
app.start();
}
/** Activate custom rendering of shadows /
BasicShadowRenderer shadowRenderer;
/* geometries and collisions shapes for bricks and cannon balls. /
private BulletAppState bulletAppState;
@Override
public void simpleInitApp() {
/* Set up Physics /
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
/* Set up camera /
this.cam.setLocation(new Vector3f(0, 6f, 3f));
cam.lookAt(Vector3f.ZERO, new Vector3f(0, 0, -3f));
cam.setFrustumPerspective(45.0f, (float) settings.getWidth() / settings.getHeight(), 0.1f, 100f);
/* Add shooting action /
inputManager.addMapping(“up”, new KeyTrigger(keyInput.KEY_I));
inputManager.addMapping(“down”, new KeyTrigger(keyInput.KEY_K));
inputManager.addMapping(“left”, new KeyTrigger(keyInput.KEY_J));
inputManager.addMapping(“right”, new KeyTrigger(keyInput.KEY_L));
/* Initialize the scene and physics space /
initSky();
initFloor();
ball = new Ball(rootNode, new Vector3f(0f, 3f, 0f), assetManager);
players = new Player[] {
new Player(rootNode, new Vector3f(0f, 3f, -1f), assetManager, 0, new ControlFactory() {
public Control createControl(CollisionShape collisionShape, float mass) {
return new CharacterControl(collisionShape, 0.1f);
}
public Control createControl(CollisionShape collisionShape) {
return createControl(collisionShape, 0f);
}
}),
new Player(rootNode, new Vector3f(0f, 0f, -3f), assetManager, 1, new RigidBodyControlFactory())
};
//// Added this to try to get SOMETHING
sphere = new Sphere(8, 8, 2f);
Material mat = assetManager.loadMaterial(“Materials/ballMaterial.j3m”);
for (int i = 0; i < 100; i ++) {
Geometry geom = new Geometry(“thing” + i,
sphere);
geom.setMaterial(mat);
geom.setLocalTranslation(
(float)Math.random() * 20f - 10f,
(float)Math.random() * 20f - 10f,
(float)Math.random() * 20f - 10f);
rootNode.attachChild(geom);
}
bulletAppState.getPhysicsSpace().setAccuracy(0.005f);
/* Activate custom shadows */
rootNode.setShadowMode(ShadowMode.Off);
shadowRenderer = new BasicShadowRenderer(assetManager, 256);
// TODO: PssmShadowRenderer ?
shadowRenderer.setDirection(new Vector3f(-1, -1, -1).normalizeLocal());
viewPort.addProcessor(shadowRenderer);
}
/**
- Every time the shoot action is triggered, a new cannon ball is produced.
- The ball is set up to fly from the camera position in the camera direction.
/
private ActionListener actionListener = new ActionListener() {
private static final float PLAYER_CONTROL_FORCE = 0.1f;
public void onAction(String name, boolean keyPressed, float tpf) {
if (name.equals("up") && !keyPressed) {
players[0].getBody().applyImpulse(
new Vector3f(0f, 0f, -PLAYER_CONTROL_FORCE), Vector3f.ZERO);
} else if (name.equals("down") && !keyPressed) {
players[0].getBody().applyImpulse(
new Vector3f(0f, 0f, PLAYER_CONTROL_FORCE), Vector3f.ZERO);
} else if (name.equals("left") && !keyPressed) {
players[0].getBody().applyImpulse(
new Vector3f(-PLAYER_CONTROL_FORCE, 0f, 0f), Vector3f.ZERO);
} else if (name.equals("right") && !keyPressed) {
players[0].getBody().applyImpulse(
new Vector3f(PLAYER_CONTROL_FORCE, 0f, 0f), Vector3f.ZERO);
}
}
};
/* Make a solid floor and add it to the scene. */
public void initFloor() {
Box floorBox = new Box(Vector3f.ZERO, 10f, 0.1f, 5f);
floorBox.scaleTextureCoordinates(new Vector2f(3, 6));
Geometry floor = new Geometry("floor", floorBox);
Material floorMat = assetManager.loadMaterial("Materials/groundMat.j3m");
floor.setMaterial(floorMat);
floor.setShadowMode(ShadowMode.Receive);
PhysicsNode floorNode = new PhysicsNode(
floor,
new BoxCollisionShape(new Vector3f(10f, 0.1f, 5f)),
0);
floorNode.setLocalTranslation(0, -0.1f, 0);
this.rootNode.attachChild(floorNode);
bulletAppState.getPhysicsSpace().add(floorNode);
}
private void initSky() {
Texture west = assetManager.loadTexture("Textures/sb2_0001.bmp");
Texture east = assetManager.loadTexture("Textures/sb2_0002.bmp");
Texture north = assetManager.loadTexture("Textures/sb2_0003.bmp");
Texture south = assetManager.loadTexture("Textures/sb2_0004.bmp");
Texture up = assetManager.loadTexture("Textures/sb2_0005.bmp");
Texture down = assetManager.loadTexture("Textures/sb2_0006.bmp");
Spatial sky = SkyFactory.createSky(assetManager, west, east, north, south, up, down);
rootNode.attachChild(sky);
}
}
[/java]
Ball.java
[java]
package mygame.object;
import com.jme3.asset.AssetManager;
import com.jme3.bullet.collision.shapes.SphereCollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.material.Material;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh.Mode;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Sphere;
import com.jme3.scene.shape.Sphere.TextureMode;
/**
*
-
@author Joel
*/
public class Ball {
private static Material material;
private final Sphere sphere;
private final SphereCollisionShape sphereShape;
private final Geometry geometry;
public Ball(Node parent, Vector3f pos, AssetManager assetManager) {
if (material == null) {
material = assetManager.loadMaterial("Materials/ballMaterial.j3m");
}
sphere = new Sphere(32, 64, 0.25f);
sphereShape = new SphereCollisionShape(0.25f);
// sphere.setTextureMode(TextureMode.Projected);
geometry = new Geometry("ball", sphere);
geometry.setMaterial(material);
geometry.setLocalTranslation(pos);
geometry.addControl(new RigidBodyControl(sphereShape));
parent.attachChild(geometry);
}
public RigidBodyControl getBody() {
return geometry.getControl(RigidBodyControl.class);
}
}
[/java]
Player.java
[java]
package mygame.object;
import mygame.control.ControlFactory;
import com.jme3.asset.AssetManager;
import com.jme3.bullet.collision.shapes.SphereCollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
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.control.Control;
import com.jme3.scene.shape.Sphere;
/**
*
-
@author Joel
*/
public class Player {
private final Material material;
private final Sphere shape;
private final SphereCollisionShape collShape;
private final Geometry geometry;
public Player(Node parent, Vector3f pos, AssetManager assetManager, int playerNum, ControlFactory controlFactory) {
if (playerNum < 0 || playerNum > 1) {
throw new IllegalArgumentException("playerNum must be 0 or 1");
}
String matName = "Materials/playerMaterial" + (playerNum%2 + 1) + ".j3m";
material = assetManager.loadMaterial(matName);
shape = new Sphere(16, 16, 1f);
collShape = new SphereCollisionShape(1f);
geometry = new Geometry("player", shape);
geometry.setMaterial(material);
geometry.setLocalTranslation(pos);
Control control = controlFactory.createControl(collShape, -1f);
geometry.addControl(control);
}
public RigidBodyControl getBody() {
return geometry.getControl(RigidBodyControl.class);
}
}
[/java]
And in case you need them, ControlFactory.java
[java]
package mygame.control;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.scene.control.Control;
/**
*
-
@author Joel
*/
public interface ControlFactory {
Control createControl(CollisionShape collisionShape, float mass);
Control createControl(CollisionShape collisionShape);
}
[/java]
RigidBodyControlFactory.java
[java]
package mygame.control;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.scene.control.Control;
/**
*
-
@author Joel
*/
public class RigidBodyControlFactory implements ControlFactory {
public Control createControl(CollisionShape collisionShape, float mass) {
return new RigidBodyControl(collisionShape, mass);
}
public Control createControl(CollisionShape collisionShape) {
return new RigidBodyControl(collisionShape);
}
}
[/java]