I am making a game in the jMonkey Engine 3 and my world generation code was a bit bulky so I planned to move it to a new class called Generator but when I finally got everything worked out and ran my program, I got a NullPointerException in the place where I tried to use the method from the other class. I have had such bad luck with NullPointerException in the past but this is the worst one yet. The code worked when it was in the main class file. I will give you the code for both classes below as well as the error.
Please note that I am not including the package declaration or the imports to save space.
Class one: Main:
public class Main extends SimpleApplication implements ActionListener {
private static final Logger logger = Logger.getLogger(Main.class.getName());
private BulletAppState bulletAppState;
private CharacterControl playerControl;
private Vector3f walkDirection = new Vector3f();
private boolean[] arrowKeys = new boolean[4];
private CubesSettings cubesSettings;
private BlockTerrainControl blockTerrain;
private Node terrainNode = new Node();
private Generator gen;
public static void main(String[] args){
Logger.getLogger("").setLevel(Level.FINE);
AppSettings s = new AppSettings(true);
Main app = new Main();
try {
s.load("com.bminus");
} catch(BackingStoreException e) {
logger.log(Level.SEVERE, "Could not load configuration settings.",e);
}
try {
s.setIcons(new BufferedImage[]{ImageIO.read(new File("assets/Textures/icon.gif"))});
} catch (IOException e) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "Icon file missing",e);
}
s.setRenderer(AppSettings.LWJGL_OPENGL2);
s.setBitsPerPixel(24);
s.setFrameRate(-1);
s.setFullscreen(false);
s.setResolution(640,480);
s.setSamples(0);
s.setVSync(true);
s.setFrequency(60);
s.setTitle("The Third Power Pre-Alpha 1.1.0");
try {
s.save("com.bminus");
} catch(BackingStoreException e) {
logger.log(Level.SEVERE, "Could not save configuration settings.",e);
}
app.setShowSettings(false);
app.setSettings(s);
app.start();
}
public Main(){}
#Override
public void simpleInitApp(){
setDisplayFps(false);
setDisplayStatView(false);
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
initControls();
gen.initBlockTerrain(); //THIS LINE IS THE ONE THAT IS NULL!!!
initGUI();
initPlayer();
cam.lookAtDirection(new Vector3f(1, 0, 1), Vector3f.UNIT_Y);
}
private void initControls(){
inputManager.addMapping("fps_show", new KeyTrigger(KeyInput.KEY_F3));
inputManager.addMapping("fps_hide", new KeyTrigger(KeyInput.KEY_F4));
inputManager.addMapping("left", new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping("right", new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping("forward", new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping("backward", new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping("jump", new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addMapping("place", new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
inputManager.addMapping("break", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addListener(this, "fps_show");
inputManager.addListener(this, "fps_hide");
inputManager.addListener(this, "left");
inputManager.addListener(this, "right");
inputManager.addListener(this, "forward");
inputManager.addListener(this, "backward");
inputManager.addListener(this, "jump");
inputManager.addListener(this, "place");
inputManager.addListener(this, "break");
}
private void initGUI(){
BitmapText crosshair = new BitmapText(guiFont);
crosshair.setText("+");
crosshair.setSize(guiFont.getCharSet().getRenderedSize() * 2);
crosshair.setLocalTranslation(
(settings.getWidth() / 2) - (guiFont.getCharSet().getRenderedSize() / 3 * 2),
(settings.getHeight() / 2) + (crosshair.getLineHeight() / 2), 0);
guiNode.attachChild(crosshair);
BitmapText instructionsText1 = new BitmapText(guiFont);
instructionsText1.setText("Left Click: Remove");
instructionsText1.setLocalTranslation(0, settings.getHeight(), 0);
guiNode.attachChild(instructionsText1);
BitmapText instructionsText2 = new BitmapText(guiFont);
instructionsText2.setText("Right Click: Place");
instructionsText2.setLocalTranslation(0, settings.getHeight() - instructionsText2.getLineHeight(), 0);
guiNode.attachChild(instructionsText2);
BitmapText instructionsText3 = new BitmapText(guiFont);
instructionsText3.setText("Bottom Layer Cannot Be Broken");
instructionsText3.setLocalTranslation(0, settings.getHeight() - (2 * instructionsText3.getLineHeight()), 0);
guiNode.attachChild(instructionsText3);
}
private void initPlayer(){
playerControl = new CharacterControl(new CapsuleCollisionShape((cubesSettings.getBlockSize() / 2), cubesSettings.getBlockSize() * 2), 0.05f);
playerControl.setJumpSpeed(25);
playerControl.setFallSpeed(140);
playerControl.setGravity(100);
playerControl.setPhysicsLocation(new Vector3f(5, 257, 5).mult(cubesSettings.getBlockSize()));
bulletAppState.getPhysicsSpace().add(playerControl);
}
#Override
public void simpleUpdate(float lastTimePerFrame) {
float playerMoveSpeed = ((cubesSettings.getBlockSize() * 2.5f) * lastTimePerFrame);
Vector3f camDir = cam.getDirection().mult(playerMoveSpeed);
Vector3f camLeft = cam.getLeft().mult(playerMoveSpeed);
walkDirection.set(0, 0, 0);
if(arrowKeys[0]){ walkDirection.addLocal(camDir); }
if(arrowKeys[1]){ walkDirection.addLocal(camLeft.negate()); }
if(arrowKeys[2]){ walkDirection.addLocal(camDir.negate()); }
if(arrowKeys[3]){ walkDirection.addLocal(camLeft); }
walkDirection.setY(0);
playerControl.setWalkDirection(walkDirection);
cam.setLocation(playerControl.getPhysicsLocation());
}
#Override
public void onAction(String actionName, boolean value, float lastTimePerFrame){
if(actionName.equals("forward")) {
arrowKeys[0] = value;
}
else if(actionName.equals("right")) {
arrowKeys[1] = value;
}
else if(actionName.equals("left")) {
arrowKeys[3] = value;
}
else if(actionName.equals("backward")) {
arrowKeys[2] = value;
}
else if(actionName.equals("jump")) {
playerControl.jump();
}
else if(actionName.equals("fps_show")) {
setDisplayFps(true);
}
else if(actionName.equals("fps_hide")) {
setDisplayFps(false);
}
else if(actionName.equals("place") && value){
Vector3Int blockLocation = getCurrentPointedBlockLocation(true);
if(blockLocation != null){
blockTerrain.setBlock(blockLocation, Block_Wood.class);
}
}
else if(actionName.equals("break") && value){
Vector3Int blockLocation = getCurrentPointedBlockLocation(false);
if((blockLocation != null) && (blockLocation.getY() > 0)){
blockTerrain.removeBlock(blockLocation);
}
}
}
private Vector3Int getCurrentPointedBlockLocation(boolean getNeighborLocation){
CollisionResults results = getRayCastingResults(terrainNode);
if(results.size() > 0){
Vector3f collisionContactPoint = results.getClosestCollision().getContactPoint();
return BlockNavigator.getPointedBlockLocation(blockTerrain, collisionContactPoint, getNeighborLocation);
}
return null;
}
private CollisionResults getRayCastingResults(Node node){
Vector3f origin = cam.getWorldCoordinates(new Vector2f((settings.getWidth() / 2), (settings.getHeight() / 2)), 0.0f);
Vector3f direction = cam.getWorldCoordinates(new Vector2f((settings.getWidth() / 2), (settings.getHeight() / 2)), 0.3f);
direction.subtractLocal(origin).normalizeLocal();
Ray ray = new Ray(origin, direction);
CollisionResults results = new CollisionResults();
node.collideWith(ray, results);
return results;
}
public void attachRootChildTerrain(){ //THIS IS CALLED IN THE GENERATOR CLASS!!
rootNode.attachChild(gen.terrainNode);
}
}
Class two: Generator:
public class Generator {
private CubesSettings cubesSettings;
private BlockTerrainControl blockTerrain;
private final Vector3Int terrainSize = new Vector3Int(1000, 256, 1000);
private final Vector3Int bottomLayer = new Vector3Int(1000,1,1000);
private BulletAppState bulletAppState;
public Node terrainNode = new Node();
private SimpleApplication sa;
private Main main;
public void initBlockTerrain(){
CubesTestAssets.registerBlocks();
CubesTestAssets.initializeEnvironment(sa);
cubesSettings = CubesTestAssets.getSettings(sa);
blockTerrain = new BlockTerrainControl(cubesSettings, new Vector3Int(7, 1, 7));
blockTerrain.setBlocksFromNoise(new Vector3Int(), terrainSize, 20f, Block_Grass.class);
blockTerrain.setBlocksFromNoise(new Vector3Int(), bottomLayer, 0.0f, Block_Stone.class);
blockTerrain.addChunkListener(new BlockChunkListener(){
#Override
public void onSpatialUpdated(BlockChunkControl blockChunk){
Geometry optimizedGeometry = blockChunk.getOptimizedGeometry_Opaque();
RigidBodyControl rigidBodyControl = optimizedGeometry.getControl(RigidBodyControl.class);
if(rigidBodyControl == null){
rigidBodyControl = new RigidBodyControl(0);
optimizedGeometry.addControl(rigidBodyControl);
bulletAppState.getPhysicsSpace().add(rigidBodyControl);
}
rigidBodyControl.setCollisionShape(new MeshCollisionShape(optimizedGeometry.getMesh()));
}
});
terrainNode.addControl(blockTerrain);
terrainNode.setShadowMode(RenderQueue.ShadowMode.CastAndReceive);
main.attachRootChildTerrain();
}
}
The error:
java.lang.NullPointerException
at com.bminus.Main.simpleInitApp(Main.java:110)
at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:226)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:130)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:207)
at java.lang.Thread.run(Thread.java:744)
This is the first time I have tried to use multiple classes. I am guessing I forgot to call something from the Generator class or make something public instead of private but I'm not sure. If anyone can figure this out, it would be greatly appreciated. Thanks!
EDIT: I did what you guys suggested (I think) and I got this error:
Exception in thread "main" java.lang.StackOverflowError
at sun.misc.Unsafe.putObject(Native Method)
at java.util.concurrent.ConcurrentLinkedQueue$Node.<init>(ConcurrentLinkedQueue.java:187)
at java.util.concurrent.ConcurrentLinkedQueue.<init>(ConcurrentLinkedQueue.java:255)
at com.jme3.app.Application.<init>(Application.java:94)
at com.jme3.app.SimpleApplication.<init>(SimpleApplication.java:102)
at com.jme3.app.SimpleApplication.<init>(SimpleApplication.java:98)
at com.bminus.Main.<init>(Main.java:88)
at com.bminus.Generator.<init>(Generator.java:31)
at com.bminus.Main.<init>(Main.java:47)
and since it is an overflow error the last two error lines go on FOREVER! The lines in my code that are being called as errors are:
public Main(){}
,
private Main main = new Main();
and
private Generator gen = new Generator(this);
I don't know exactly why this is happening but if any of you guys did, it would be great to know how to fix it. Thanks!
Most important when debugging NullPointerException's is to find the line that throws the exception. A variable that you are trying to use on that line is null.
Possible culprits:
In your Main class, your Generator variable, gen, is never assigned an instance, and thus is null. Give it an instance by calling the Generator constructor.
gen = new Generator();
In your Generator class, the Main field, main, is null since you never assign to it a Main instance. I suggest that you give the constructor a Main parameter so you can pass in the currently used Main instance created in your main method (these names are confusing!) into your Generator class and use this parameter to initialize your main field.
i.e.,
public Generator(Main main) {
//.....
this.main = main; // give the main field an object
}
So change the above code to create Generator to:
gen = new Generator(this);
Related
How can this code be broken down to follow the principles of single responsibility principle? Even though I understand the SOLID principles and have read through many materials especially Uncle Bob's articles on SOLID principles I have unfortunately been unable to split the following code to two different classes to follow Single Responsibility Principle. I would highly appreciate help from StackOverflow
/** The only subclass the fully utilizes the
Entity superclass (no other class requires
movement in a tile based map).
Contains all the gameplay associated with
the Player.**/
package com.neet.DiamondHunter.Entity;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import com.neet.DiamondHunter.Manager.Content;
import com.neet.DiamondHunter.Manager.JukeBox;
import com.neet.DiamondHunter.TileMap.TileMap;
public class Player extends Entity {
// sprites
private BufferedImage[] downSprites;
private BufferedImage[] leftSprites;
private BufferedImage[] rightSprites;
private BufferedImage[] upSprites;
private BufferedImage[] downBoatSprites;
private BufferedImage[] leftBoatSprites;
private BufferedImage[] rightBoatSprites;
private BufferedImage[] upBoatSprites;
// animation
private final int DOWN = 0;
private final int LEFT = 1;
private final int RIGHT = 2;
private final int UP = 3;
private final int DOWNBOAT = 4;
private final int LEFTBOAT = 5;
private final int RIGHTBOAT = 6;
private final int UPBOAT = 7;
// gameplay
private int numDiamonds;
private int totalDiamonds;
private boolean hasBoat;
private boolean hasAxe;
private boolean onWater;
private long ticks;
// player status
private int healthPoints;
private boolean invincible;
private boolean powerUp;
private boolean speedUp;
public Player(TileMap tm) {
super(tm);
width = 16;
height = 16;
cwidth = 12;
cheight = 12;
moveSpeed = 2;
numDiamonds = 0;
downSprites = Content.PLAYER[0];
leftSprites = Content.PLAYER[1];
rightSprites = Content.PLAYER[2];
upSprites = Content.PLAYER[3];
downBoatSprites = Content.PLAYER[4];
leftBoatSprites = Content.PLAYER[5];
rightBoatSprites = Content.PLAYER[6];
upBoatSprites = Content.PLAYER[7];
animation.setFrames(downSprites);
animation.setDelay(10);
}
private void setAnimation(int i, BufferedImage[] bi, int d) {
setAnimation(i, bi, d, false);
}
private void setAnimation(int i, BufferedImage[] bi, int d, boolean slowMotion) {
currentAnimation = i;
animation.setFrames(bi);
animation.setDelay(d);
slowMotion = true;
}
public void collectedDiamond() { numDiamonds++; }
public int numDiamonds() { return numDiamonds; }
public int getTotalDiamonds() { return totalDiamonds; }
public void setTotalDiamonds(int i) { totalDiamonds = i; }
public int getx() { return x; }
public int gety() { return y; }
public int getRow() { return rowTile; }
public int getCol() { return colTile; }
public void gotBoat() { hasBoat = true; tileMap.replace(22, 4); }
public void gotAxe() { hasAxe = true; }
public boolean hasBoat() { return hasBoat; }
public boolean hasAxe() { return hasAxe; }
public int getHealthPoints() { return healthPoints; }
// Used to update time.
public long getTicks() { return ticks; }
// Keyboard input. Moves the player.
public void setDown() {
super.setDown();
}
public void setLeft() {
super.setLeft();
}
public void setRight() {
super.setRight();
}
public void setUp() {
super.setUp();
}
// Keyboard input.
// If Player has axe, dead trees in front
// of the Player will be chopped down.
public void setAction() {
final boolean pressUPKEY = currentAnimation == UP && tileMap.getIndex(rowTile - 1, colTile) == 21;
final boolean pressDOWNKEY = currentAnimation == DOWN && tileMap.getIndex(rowTile + 1, colTile) == 21;
final boolean pressLEFTKEY = currentAnimation == LEFT && tileMap.getIndex(rowTile, colTile - 1) == 21;
final boolean pressRIGHTKEY = currentAnimation == RIGHT && tileMap.getIndex(rowTile, colTile + 1) == 21;
if(hasAxe) {
if(pressUPKEY) {
tileMap.setTile(rowTile - 1, colTile, 1);
}
if(pressDOWNKEY) {
tileMap.setTile(rowTile + 1, colTile, 1);
}
if(pressLEFTKEY) {
tileMap.setTile(rowTile, colTile - 1, 1);
}
if(pressRIGHTKEY) {
tileMap.setTile(rowTile, colTile + 1, 1);
}
JukeBox.play("tilechange");
}
}
public void update() {
ticks++;
boolean current = onWater;
onWater = CheckIfOnWater();
//if going from land to water
if(!current && onWater){
JukeBox.play("splash");
}
// set animation
setAnimationDown();
setAnimationLeft();
setAnimationRight();
setAnimationUp();
// update position
super.update();
}
public void setAnimationUp() {
if(up) {
if(onWater && currentAnimation != UPBOAT) {
setAnimation(UPBOAT, upBoatSprites, 10);
}
else if(!onWater && currentAnimation != UP) {
setAnimation(UP, upSprites, 10);
}
}
}
public void setAnimationRight() {
if(right) {
if(onWater && currentAnimation != RIGHTBOAT) {
setAnimation(RIGHTBOAT, rightBoatSprites, 10);
}
else if(!onWater && currentAnimation != RIGHT) {
setAnimation(RIGHT, rightSprites, 10);
}
}
}
public void setAnimationLeft() {
if(left) {
if(onWater && currentAnimation != LEFTBOAT) {
setAnimation(LEFTBOAT, leftBoatSprites, 10);
}
else if(!onWater && currentAnimation != LEFT) {
setAnimation(LEFT, leftSprites, 10);
}
}
}
public void setAnimationDown() {
if(down) {
if(onWater && currentAnimation != DOWNBOAT) {
setAnimation(DOWNBOAT, downBoatSprites, 10);
}
else if(!onWater && currentAnimation != DOWN) {
setAnimation(DOWN, downSprites, 10);
}
}
}
public boolean CheckIfOnWater(){
int index = tileMap.getIndex(ydest / tileSize, xdest / tileSize);
if(index == 4) {
return true;
}
else {
return false;
}
}
// Draw Player.
public void draw(Graphics2D g)
{
super.draw(g);
}
}
Try to implement your components in a Model View Controller style, for example move all the code related to the update the view in a package yourapp.view for example, and move the code of your current class Player into a new class for example, GameBoard or GameView or whaterver, where the single responsability of this class is updating the model representations drawing the animations/images etc., in the gameboard screen. Another class for example, PlayerMovement and move all the code related to de keyboard events, currenly in your Player class, where the responsability of this class is catching the keys that makes the player move.
Move all the code related with game orders and desitions and move in another package yourapp.controller or actions or whatever, and create new classes for example PlayerController, GameController or whatever, where the single responsability of this class is receive the player requests to update the game models state, addressing commands and saying to the model classes be updated and saying to de view classes that gets the new model state every time that the model changes; for example when the player has a new location in the game board, or the location of some missil or some character die etc.
Put your model classes for example in other package for example yourapp.character or actors or whatever, and move the code related to the model state creating new classes that represent the game characters or actors or live elements that defining the behabior or roles of your game. For example the player or a Ship or a Cannon etc. This classes their responsability is only define the game character and their characteristis and behavior por example the location in the gameboard, their weapons, their powers, if is live or dead etc., and other info neded about their role into the game.
Try to identify and apply in a second stage, GoF pattern this can help you to refactor your code and make it more acurate to SOLID principles.
Another of thinking about SRP is each unit should have a Single Reason for Change.
Take your class, what are the reasons one might change it in future:
The sprites change.
The animation changes.
The gameplay changes.
The player status logic changes.
Each of those you have organised fields for, so you spotted them yourself. Now just go one step further and create classes for each of those. So that you can change, for example, animation without touching code that affects gameplay. This:
Makes changes safer (fewer side effects in the absence of a good test suite)
Makes it easier to work on, you need to read less code, just the part you're interested in
Makes it easier for your team to review changes (they will know you haven't affected gameplay while changing animation, for example)
Makes it easier for a team to work on different parts of the system with less chance of merge conflicts
Makes it easy to swap out one component for another implementation, you could, for example, re-skin your game by replacing the sprites component with another
I want t have some picture above another one and want to utilize PCamera's addLayer() method.
Is this possible?
The following code throws NullPointerException. What's wrong with it?
package test.piccolo;
import java.awt.Color;
import edu.umd.cs.piccolo.PCamera;
import edu.umd.cs.piccolo.PLayer;
import edu.umd.cs.piccolo.nodes.PPath;
import edu.umd.cs.piccolox.PFrame;
public class Try_Cameras_01 {
#SuppressWarnings("serial")
public static void main(String[] args) {
new PFrame() {
private PLayer layer1 = new PLayer();
private PLayer layer2 = new PLayer();
private PLayer layer3 = new PLayer();
private PCamera camera = new PCamera();
{
camera.addLayer(layer1);
camera.addLayer(layer2);
camera.addLayer(layer3);
}
#Override
public void initialize() {
getCanvas().setCamera(camera);
PPath redRectangle = PPath.createRectangle(0, 0, 100, 100);
redRectangle.setStrokePaint(Color.black);
redRectangle.setPaint(Color.red);
PPath greenRectangle = PPath.createRectangle(20, 20, 100, 100);
greenRectangle.setStrokePaint(Color.black);
greenRectangle.setPaint(Color.green);
PPath blueRectangle = PPath.createRectangle(40, 40, 100, 100);
blueRectangle.setStrokePaint(Color.black);
blueRectangle.setPaint(Color.blue);
layer1.addChild(redRectangle);
layer2.addChild(greenRectangle);
layer3.addChild(blueRectangle);
}
};
}
}
The problem is that when you set up a new camera it has no associated root. As a result, PCanvas.getRoot() returns null and there an NPE in one of the painting methods. Here is a basic Piccolo2D runtime structure:
Read more in Piccolo2D Patterns.
In your case you're missing a link to PRoot from a PCamera. Here is a simple fix:
private PCamera camera = new PCamera(); {
PRoot root = new PRoot();
root.addChild(camera);
camera.addLayer(layer1);
camera.addLayer(layer2);
camera.addLayer(layer3);
}
That results in:
For reference here is a copy from PUtil.createBasicScenegraph() that creates a basic camera.
public static PCamera createBasicScenegraph() {
final PRoot root = new PRoot();
final PLayer layer = new PLayer();
final PCamera camera = new PCamera();
root.addChild(camera);
root.addChild(layer);
camera.addLayer(layer);
return camera;
}
How can I shoot in the direction that a cross-hair is pointing at?
Using the JMonkey engine, I am creating a game where I need a ship to shoot other ships.
So, I created cross-hairs that can move on the screen (up, down, left, right) according to user input, so the user can aim on a certain place.
Now I need I to shoot a cannon from my ship, in the direction that the cross-hair is standing at.
How can I shoot at the place that the cross-hair is pointing at?
You can get camera direction with:
directionXYZ=cam.getDirection(); //Vector3f form
and can get position from:
positionXYZ=cam.getLocation(); //Vector3f
You can have a ray casting:
Ray ray = new Ray(directionXYZ, positionXYZ);
then can collect data for collisions:
shootables.collideWith(ray, results)
where shootable is a "Node".
Finally, check for what you want:
for (int i = 0; i < results.size(); i++) {
// For each hit, we know distance, impact point, name of geometry.
float dist = results.getCollision(i).getDistance();
Vector3f pt = results.getCollision(i).getContactPoint();
String hit = results.getCollision(i).getGeometry().getName();
System.out.println("* Collision #" + i);
System.out.println(" You shot " + hit + " at " + pt + ", " + dist + " wu away.");
}
Taken from jmonkey wiki
My reading of this question is that the aim is not to shoot where the camera is facing but where a cursor (not in the centre of the screen) is pointing.
This can be achieved using the cam.getWorldCoordinates(screenPosition, zDepth); command, this returns the 3D point in space that would end up at the point screenPosition on the screen. So if we create a point at zDepth of zero and a point at zDepth of one we can create a ray coming from the cursor position travelling outwards so anything that the curser is "over" is selected. screenPosition is in pixels from the bottom left of the window
An example program that uses this technique is as follows and is based upon the second part of hello picking.
In my example the cursor is moved using the keyboard (H,J,K,U) but a mouse click could also be used (but I'm using the mouse for looking around)
import com.jme3.app.SimpleApplication;
import com.jme3.collision.CollisionResults;
import com.jme3.font.BitmapText;
import com.jme3.input.KeyInput;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Ray;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Box;
public class Main extends SimpleApplication {
public static KeyBindings keyBindings;
public Vector2f cursorPosition=new Vector2f(100,100);
public Node shootables=new Node();
public Node crossHairNode=new Node();
public static void main(String[] args) {
Main app = new Main();
app.start();
}
#Override
public void simpleInitApp() {
//bind keys to move cursor
keyBindings=new KeyBindings(inputManager); //for managing keystrokes
keyBindings.registerKeyBinding(KeyInput.KEY_SPACE, "fire");
keyBindings.registerKeyBinding(KeyInput.KEY_U, "up");
keyBindings.registerKeyBinding(KeyInput.KEY_J, "down");
keyBindings.registerKeyBinding(KeyInput.KEY_H, "left");
keyBindings.registerKeyBinding(KeyInput.KEY_K, "right");
initGui();
Box b = new Box(Vector3f.ZERO, 2, 2, 2);
Geometry geom = new Geometry("BigBox", b);
Box b2 = new Box(Vector3f.ZERO, 1, 1, 1);
Geometry geom2 = new Geometry("SmallBox", b2);
geom2.setLocalTranslation(3, 0, 3);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Blue);
geom.setMaterial(mat);
geom2.setMaterial(mat);
rootNode.attachChild(shootables);
shootables.attachChild(geom);
shootables.attachChild(geom2);
}
#Override
public void simpleUpdate(float tpf) {
updateCrossHairs();
if (keyBindings.getBinding("fire").hasBecomeTrueSinceLastAccess()){
CollisionResults results = new CollisionResults();
Vector3f cursor3dLocation = cam.getWorldCoordinates(
new Vector2f(cursorPosition.x, cursorPosition.y), 0f).clone();
Vector3f dir = cam.getWorldCoordinates(
new Vector2f(cursorPosition.x, cursorPosition.y), 1f).subtractLocal(cursor3dLocation).normalizeLocal();
Ray ray = new Ray(cursor3dLocation, dir);
shootables.collideWith(ray, results);
if (results.size()>0){
resultsText.setText("Hit: " + results.getClosestCollision().getGeometry().getName());
resultsText.setLocalTranslation(settings.getWidth()-resultsText.getLineWidth(),resultsText.getLineHeight(), 0);
}else{
resultsText.setText("Missed");
resultsText.setLocalTranslation(settings.getWidth()-resultsText.getLineWidth(),resultsText.getLineHeight(), 0);
}
}
}
private void updateCrossHairs(){
if (keyBindings.getBinding("up").getValue()==true){
cursorPosition.y+=1;
}
if (keyBindings.getBinding("down").getValue()==true){
cursorPosition.y+=-1;
}
if (keyBindings.getBinding("left").getValue()==true){
cursorPosition.x+=-1;
}
if (keyBindings.getBinding("right").getValue()==true){
cursorPosition.x+=+1;
}
crossHairNode.setLocalTranslation(cursorPosition.x - crossHair.getLineWidth()/2,cursorPosition.y + crossHair.getLineHeight()/2, 0);
}
BitmapText crossHair;
BitmapText instructions;
BitmapText resultsText;
private void initGui() {
setDisplayStatView(false);
guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
crossHair = new BitmapText(guiFont, false);
crossHair.setSize(guiFont.getCharSet().getRenderedSize() * 2);
crossHair.setText("+"); // crosshairs
crossHairNode.setLocalTranslation(cursorPosition.x - crossHair.getLineWidth()/2,cursorPosition.y + crossHair.getLineHeight()/2, 0);
guiNode.attachChild(crossHairNode);
crossHairNode.attachChild(crossHair);
instructions= new BitmapText(guiFont, false);
instructions.setSize(guiFont.getCharSet().getRenderedSize());
instructions.setText("Move cross hairs with U,H,J,K keys, fire with space \n (WSAD moves camera position and look with mouse)");
instructions.setLocalTranslation(0, settings.getHeight(), 0);
guiNode.attachChild(instructions);
resultsText= new BitmapText(guiFont, false);
resultsText.setSize(guiFont.getCharSet().getRenderedSize());
resultsText.setText("Press Space to fire");
resultsText.setLocalTranslation(settings.getWidth()-resultsText.getLineWidth(),resultsText.getLineHeight(), 0);
guiNode.attachChild(resultsText);
}
#Override
public void simpleRender(RenderManager rm) {
//TODO: add render code
}
}
Keybindings, used only to control cursor movement:
import com.jme3.input.InputManager;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import java.util.ArrayList;
import java.util.Locale;
public class KeyBindings implements ActionListener{
private InputManager inputManager;
private ArrayList<String> bindingString=new ArrayList<String>(100);
private ArrayList<KeyBinding> binding=new ArrayList<KeyBinding>(100);
public KeyBindings(InputManager inputManager){
this.inputManager=inputManager;
}
public void registerKeyBinding(int key,String bindingName){
bindingName=preprocess(bindingName);
inputManager.addMapping( bindingName, new KeyTrigger(key));
inputManager.addListener(this, bindingName);
binding.add(new KeyBinding());
bindingString.add(bindingName);
}
public void registerMouseBinding(int button,String bindingName){
bindingName=preprocess(bindingName);
inputManager.addMapping( bindingName, new MouseButtonTrigger(button));
inputManager.addListener(this, bindingName);
binding.add(new KeyBinding());
bindingString.add(bindingName);
}
public KeyBinding getBinding(String bindingName){
//get which binding we're after
bindingName=preprocess(bindingName);
int index=bindingString.indexOf(bindingName);
if (index!=-1){
return binding.get(index);
}else{
return null;
}
}
public void onAction(String bindingName, boolean isPressed, float tpf) {
bindingName=preprocess(bindingName);
//get which binding we're after
int index=bindingString.indexOf(bindingName);
if (index!=-1){
binding.get(index).setValue(isPressed);
}
}
private String preprocess(String string){
return string.toUpperCase();
}
}
public class KeyBinding {
private boolean value;
private boolean changedSinceLastAccess; //to avoid multiclicks etc
private boolean valueTrueSinceLastAccess;
public void setValue(boolean value){
this.value=value;
changedSinceLastAccess=true;
if (value==true){
valueTrueSinceLastAccess=true;
}
}
public boolean hasChangedSinceLastAccess(){
return changedSinceLastAccess;
}
public boolean hasBecomeTrueSinceLastAccess(){
//this collects any trues since the last access, is good for things like mouse clicks,
//which are instantaneous and you want then recorded on the next tick
if (valueTrueSinceLastAccess==true){
valueTrueSinceLastAccess=false;
return true;
}else{
return false;
}
}
public boolean getValue(){
changedSinceLastAccess=false;
valueTrueSinceLastAccess=false;
return value;
}
public boolean getValue_withoutNotifying(){
return value;
}
}
I am using Box2D in AndEngine (for Android).
My purpose is to create a Force joint whenever 2 objects collide with each other.
When I try to create a Mouse Joint between 2 objects (bodies) during ContactListner process. The application will hang for some time then exit, without any error, just a notification of threads ending.
The Joint creating is OK when I call mEnvironment.CreateForceJoint(..) outside the ContactListener - somewhere while app is running in some physics.UpdateHandler().
Please help me to solve the problems, or find out the reason. Thanks for any help!
This is my code:
public class MyActivity extends SimpleBaseGameActivity {
private final String DEBUG_TAG = "MyActivity";
private GEnvironment mEnvironment;
private PhysicsWorld mPhysicsWorld;
private MyFixture FIXTURE_PLANET = GlobalSettings.FIXTURE_PLANET;
private MyFixture FIXTURE_VACUUM = GlobalSettings.FIXTURE_VACUUM;
// CODE TO CREATE RESOURCES and ENGINE OPTIONS....
#Override
protected Scene onCreateScene() {
Scene scene = new Scene();
scene.setBackground(new Background(0.8627f, 0.9020f, 1.0f));
//CODE: creating physic world
//.....
//creating game environment
mEnvironment = new GEnvironment(mPhysicsWorld, scene, mEngine);
//CODE: creating objects, register and attach them into scene
GMediaPlanet sunZone = mEnvironment.CreatePlanet(x1, y1, sunTextureRegion, FIXTURE_PLANET);
GMediaPlanet earthZone = mEnvironment.CreatePlanet(x2, y2, earthTextureRegion, FIXTURE_PLANET);
// enable contact listener, detect collision between bodies
mPhysicsWorld.setContactListener(new PlanetContactHandler());
return scene;
}
// ----------------------------------------------------
// Handling collision between letter cubes
// ----------------------------------------------------
/**
* Handling the collision of GMediaPlanets
*/
public class PlanetContactHandler implements ContactListener {
private final String DEBUG_TAG = "CONTACT";
// if there's one collision, do not handle others or re-handle it
private boolean mIsColliding = false;
#Override
public void beginContact(Contact contact) {
if (mIsColliding)
return;
//-----------------------------------------------
//suppose colliding objects to be sunZone and earthZone
//-----------------------------------------------
Object aTag = contact.getFixtureA().getBody().getUserData();
Object bTag = contact.getFixtureB().getBody().getUserData();
if (aTag != null && bTag != null) {
GMediaPlanet box = null;
GMediaPlanet cube = null;
if (aTag instanceof GMediaPlanet
&& bTag instanceof GMediaPlanet) {
box = (GMediaPlanet) aTag;
cube = (GMediaPlanet) bTag;
}
if (box != null && cube != null)
{
//!!!!!!!-----------------------------------------------------
//This code will HANG the app when called inside contact listner:
MouseJoint mTestJoint = mEnvironment.CreateForceJoint(box, cube);
//!!!!!!!-----------------------------------------------------
Vector2 target = Vector2Pool.obtain(box.GetLocation());
mTestJoint.setTarget(target);
Vector2Pool.recycle(target);
}
}
mIsColliding = true;
}
#Override
public void endContact(Contact contact) {
Log.d(DEBUG_TAG, "end colliding!");
mIsColliding = false;
}
#Override
public void preSolve(Contact contact, Manifold oldManifold) {
}
#Override
public void postSolve(Contact contact, ContactImpulse impulse) {
}
}
}
public class GMediaPlanet
{
protected IAreaShape mSprite = null;
protected Body mBody = null;
public GMediaPlanet()
{ }
public Vector2 GetLocation()
{
mBody.getPosition();
}
}//end
public class GEnvironment
{
private PhysicsWorld mWorld;
private Scene mScene;
private org.andengine.engine.Engine mEngine;
public GEnvironment(PhysicsWorld pWorld, Scene pScene, org.andengine.engine.Engine pEngine)
{
mWorld = pWorld;
mScene = pScene;
mEngine = pEngine;
}
/** the constructor is hidden, available within Appearances packet only */
public GMediaPlanet CreatePlanet(float pX, float pY, ITextureRegion pTextureRegion, MyFixture pFixture)
{
GMediaPlanet entity = new GMediaPlanet();
entity.mSprite = new Sprite(pX, pY, pTextureRegion, mEngine.getVertexBufferObjectManager());
entity.mBody = PhysicsFactory.createCircleBody(mWorld, entity.mSprite, BodyType.DynamicBody,
pFixture.GetDef(), GlobalSettings.PIXEL_2_METER);
mScene.attachChild(entity.mSprite);
entity.mSprite.setUserData(entity.mBody);
entity.mBody.setUserData(entity);
mWorld.registerPhysicsConnector(new PhysicsConnector(entity.mSprite, entity.mBody, true, true));
return entity;
}
// -----------------------------
// Creating JOINTS
// -----------------------------
/**
* Creating a force joit based on type of MouseJointDef
*
* #param anchorObj
* the static object in the mTestJoint (anchor base)
* #param movingObj
* object to move forward the target
*/
public MouseJoint CreateForceJoint(GMediaPlanet anchorObj, GMediaPlanet movingObj)
{
ChangeFixture(movingObj, GlobalSettings.FIXTURE_VACUUM);
MouseJointDef jointDef = new MouseJointDef();
jointDef.dampingRatio = GlobalSettings.MOUSE_JOINT_DAMP;
jointDef.frequencyHz = GlobalSettings.MOUSE_JOINT_FREQ;
jointDef.collideConnected = true;
Vector2 initPoint = Vector2Pool.obtain(movingObj.mBody.getPosition());
jointDef.bodyA = anchorObj.mBody;
jointDef.bodyB = movingObj.mBody;
jointDef.maxForce = (GlobalSettings.MOUSE_JOINT_ACCELERATOR * movingObj.mBody.getMass());
// very important!!!, the initial target must be position of the sattelite
jointDef.target.set(initPoint);
MouseJoint joint = (MouseJoint) mWorld.createJoint(jointDef);
Vector2Pool.recycle(initPoint);
// very important!!!, the target of the joint then changed to target anchor object
Vector2 targetPoint = Vector2Pool.obtain(anchorObj.mBody.getWorldCenter());
joint.setTarget(targetPoint);
Vector2Pool.recycle(targetPoint);
return joint;
}
public void ChangeFixture(GMediaPlanet entity, MyFixture pFixture)
{
Filter fil = new Filter();
fil.categoryBits = pFixture.categoryBit;
fil.maskBits = pFixture.maskBits;
if(entity.mBody != null)
{
entity.mBody.getFixtureList().get(0).setFilterData(fil);
}
}
}
You can not modify the world in Step()-Call of Box2D because the World is locked! you should get an exception. You have to Remember which objects are colliding and do stuff after beginContact.. for example in an update function.
I'm trying to do a game with andengine library.
When the Sprite Enemy1Sprite reach the top of the camera, and I detach it, this exception is thrown :
java.lang.IndexOutOfBoundsException Invalid Index 12 size is 12
I have to detach the enemy1Sprite because it keep creating Sprites of bullets out of the camera.
This is the code.
Class enemy1 :
package es.uah.juegomentos;
import org.anddev.andengine.engine.handler.timer.ITimerCallback;
import org.anddev.andengine.engine.handler.timer.TimerHandler;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
public class Enemy1 extends Sprite {
boolean abajo = true;
public Enemy1(TextureRegion pTextureRegion) {
super(0, 0, pTextureRegion);
this.setPosition(JuegoMentosActivity.RANDOM.nextInt(JuegoMentosActivity.CAMERA_WIDTH), -10);
TimerHandler Enemy1fire = new TimerHandler(0.75f, true, enemigo1fireCallback);
JuegoMentosActivity.getmGameScene().registerUpdateHandler(Enemy1fire);
}
#Override
protected void onManagedUpdate(float pSecondsElapsed) {
super.onManagedUpdate(pSecondsElapsed);
float y = getY();
if (y >= 275) {abajo = false;}
if (abajo) {y = y + pSecondsElapsed * 125.0f;}
else {y = y - pSecondsElapsed * 125.0f;}
this.setPosition(getX(), y);
if (getY()<-10){this.getParent().detachChild(this);}
}
ITimerCallback enemigo1fireCallback = new ITimerCallback(){
#Override
public void onTimePassed(TimerHandler pTimerHandler) {
bala1 mbala1;
mbala1 = new bala1(getX()+(64*1/2),getY()+64,JuegoMentosActivity.getMbala1Texture().getTextureRegion(),true);
JuegoMentosActivity.getmGameScene().attachChild(mbala1);
}
};
}
Create new enemy in the scene :
//Creamos el sprite del enemigo uno
ITimerCallback enemigo1CreatorCallback = new ITimerCallback(){
#Override
public void onTimePassed(TimerHandler pTimerHandler) {
mEnemy1Sprite = new Enemy1(mEnemy1Texture.getTextureRegion());
mGameScene.attachChild(mEnemy1Sprite);
}
};
TimerHandler Enemy1Creator = new TimerHandler(3.0f, true, enemigo1CreatorCallback);
mGameScene.registerUpdateHandler(Enemy1Creator);
Thanks
You've answered your own question really - Marcelo is correct, the issue is not in the code you posted, it's where you are doing the detachChild call - you need to call that on the Update Thread, as in
runOnUpdateThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
yourScene.detachChild(yourEnemySprite);
}
});
This says that your actual array size is 12 so your last index is 11. But you are trying to access index 12 which does not exist. Try to find out the line which throws this error. Make a condition there that if size of index is >= size of array brake.
Also you may try using try{}catch(IndexOutOfBondException e){} and continue the process.
detach entity in onManagedUpdate of scene.